Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Shift operators

Arathi Rajashekar
Ranch Hand
Posts: 112
Hi,
I want to know how does
4<<-5
results in 1342177280.
I read from previous posting that
If the lefthand operand is of type long (64 bits) then only the last 6 bits of the right hand side are used. If the lefhand operand is an int (32 bits) then only the last 5 bits of the right hand operator are used.
The answer will be right if we won't take two's complement of negative number i.t 5
We are suppose to take two compliement of negative number right. Do any one clear my doubt
Say 4<<-5 without two's complement to represent -ve number
00000000 00000000 00000000 00000100<<11111111
when we take 5 bits of right hand side
00000000 00000000 00000000 00000100<<11100
4<<28
this results in above value

but if we take two's comlement of -ve number
00000000 00000000 00000000 00000100<<11111111 11111111 11111111 11111011
00000000 00000000 00000000 00000100<<11011
4<<27
this results in different value other than above.
Here i am confused. Do we have to take -ve complement or not

Shivaji Marathe
Ranch Hand
Posts: 203
I read from previous posting that
If the lefthand operand is of type long (64 bits) then only the last 6 bits of the right hand side are used. If the lefhand operand is an int (32 bits) then only the last 5 bits of the right hand operator are used.

If the left hand operator is an int then only the last 4 bytes NOT Bits of the right hand operator are used. ( the same length as an int )
HTH

Tony reedy
Greenhorn
Posts: 21
Hi Arthi,
Your logic of 5 bits with 2's compliment is working fine for me.

just try out this simple code.

both are giving the same number.
I hope this helps
-Tony.

Arathi Rajashekar
Ranch Hand
Posts: 112
Originally posted by Tony reedy:
Hi Arthi,
Your logic of 5 bits with 2's compliment is working fine for me.

just try out this simple code.

both are giving the same number.
I hope this helps
-Tony.

Hi Tony.
It worked fine for me. But will you compile and will you clarify this for me again.
Change in the above code 4<<-5 to
16<<-12.
I am getting different answer when I calculte manually and when I run the program.

Rob Ross
Bartender
Posts: 2205
Originally posted by Shivaji Marathe:

If the left hand operator is an int then only the last 4 bytes NOT Bits of the right hand operator are used. ( the same length as an int )
HTH

Actually, this is wrong.
JLS 15.19

If the promoted type of the left-hand operand is an int, only the five lowest-order bits of the right-hand operand are used as the shift distance....
If the promoted type of the left-hand operand is a long, only the six lowest-order bits of the right-hand operand are used as the shift distance....

Rob

Shivaji Marathe
Ranch Hand
Posts: 203
OOPS. Of course Rob is right . The low order 5 bits can define any number from 0 to 31. And that is the range of numbers by which an int can be shifted. In other words, the right hand operator always has a value between 0 and 31.
By the same logic a long can be shifted aby any number between 0 and 63.
I don't know what I was smoking when I posted earlier

Rob Ross
Bartender
Posts: 2205

I don't know what I was smoking when I posted earlier

I don't either, but make sure you save it for AFTER your SCJP test!!
(and save me some!)

Rob :roll:
[ January 15, 2002: Message edited by: Rob Ross ]