Check out Manning's Countdown to 2014. Use discount code crdotd14 all month for 50% off every deal.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# shift operator on negative number

Cindy Yang
Greenhorn

Joined: Aug 24, 2001
Posts: 7
can anybody explain in detail how to do following code? I have trouble when using shift operator on negative.
int i = -8, j = -8;
i >>= -4;
j >>= -34;
i=?
j=?
Thanks.
Cindy
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Hi Cindy,
Please make the question clear as to what do you want?
Bill Tripper
Greenhorn

Joined: May 30, 2001
Posts: 24
Originally posted by Cindy Yang:
can anybody explain in detail how to do following code? I have trouble when using shift operator on negative.
int i = -8, j = -8;
i >>= -4;
j >>= -34;
i=?
j=?
Thanks.
Cindy

Both i and j end up as -1.
i >>= -4 is equivalent to i >>= 28.
j >>= -34 is equivalent to j >>= -2 is equivalent to j >>= 30.
Note that the >> shift operator will fill in the sign bit, that is, as values are shift to the right, the leftmost bits are all filled with ones.
In your example, i = -8 which is binary 11111111111111111111111111111000. Then >> -4 is the same as >> 28 which yields binary 11111111111111111111111111111111, or decimal -1. Next j = -8 which is binary 11111111111111111111111111111000. Then >> -34 is the same as >> -2 which is the same as >>30 which yields binary 11111111111111111111111111111111, or decimal -1.
Your example would be more interesting if you did

which yields the result:
i = 15, j = 1073741822
by the following logic.
i = -8 is binary 11111111111111111111111111111000.
Shift it -4 is the same as shift it 28 which is binary 00000000000000000000000000001111 which is 15.
j = -8 which is binary 11111111111111111111111111111000
Shift it -30 is the same as shift it 2 which is binary 00111111111111111111111111111110 which is 1073741822.
Bill
Cindy Yang
Greenhorn

Joined: Aug 24, 2001
Posts: 7
Thanks, Bill.
i almost got it. But I am still not clear about " i<<=-4 is same as i<<=28". Could you explain that in detail?
is it because int is 32 bits, therefore, -4+32 = 28. Is it right?
For data type long, it is 64 bits, so, l<<=-4 is same to l<<=60. Am I right? correct me.
Can I use this as a formula when shifting negative bits?
Thanks a lot.
Cindy
Originally posted by Bill Tripper:
[B]
which yields the result:
i = 15, j = 1073741822
by the following logic.
i = -8 is binary 11111111111111111111111111111000.
Shift it -4 is the same as shift it 28 which is binary 00000000000000000000000000001111 which is 15.
j = -8 which is binary 11111111111111111111111111111000
Shift it -30 is the same as shift it 2 which is binary 00111111111111111111111111111110 which is 1073741822.
Bill
[/B]

Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The correct answer acording to the JLS is "If the left side is an int then the right side is anded & with 0x1F and if the left side is a long it is & with 0x3F"
So -4 & 0x1F = 28
-34 & 0x1F = 30
------------------
Hope This Helps
Carl Trusiak, SCJP2

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD

subject: shift operator on negative number