This week's book giveaway is in the Servlets forum. We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line! See this thread for details.

Which of the following results in a negative value of a? A: int a = -1; a = a >>> 8; B: byte a = -1; a = (byte)(a >>> 5); C: int a = -1; a = a >> 5; D: int a = -1; a = a >>> 32

The answer is supposed to be A, B and D. I don't see this at all. I would think the only negative one would B. I believe that A, C and D are incorrect because any time an int is unsigend-right-shifted, a positive number results. In the case of B above though things are sort of different. As I recall when a non-int is acted on by any shift operation, it is first cast to an int and then the shifting is performed. This converts the byte of -1 (11111111) into the int form of -1 (11111111111111111111111111111111). This is unsigned-right-shifted by 5, which results in some positive number (00000111111111111111111111111111). Which is then cast back into the form of a byte which leaves us with -1 again (11111111 that is). Am I right here?

I wrote the following program: public class Test{ public static void main(String[] args){ int A = -1; A = A >>> 8;

byte B = -1; B = (byte)(B >>> 5);

int C = -1; C = C >> 5;

int D = -1; D = D >>> 32;

System.out.println("A = " + A); System.out.println("B = " + B); System.out.println("C = " + C); System.out.println("D = " + D); } } And got these results: A = 16777215 B = -1 C = -1 D = -1

Honestly, this makes no sense to me. I would think that C is the only right answer, since >>> is the unsigned right shift (it fills the bits to the left with 0) and >> fills the left with whatever the sign bit is. In Two's Complement, negative numbers have a 1 as their Most Significant Bit. Since each of these start as negative (with MSB of 1), only choice C should stay negative, since it will be the only one left with a MSB of 1 (because A, C, D fill with 0). Can someone please explain this? I'm thoroughly confused.

Just taking a quick look without getting out my pencil and paper... B *could* be correct since you're doing a cast after the shift and just grabbing the rightmost 8 bits - the 8th bit might be a 1 C is using the signed shift operator - so the sign bit will be replicated D - The right hand shift operator can only be, in practice, from 0-31 for ints and from 0-63 for longs. Think about it like using the remainder operator - in this case the right operand (32) becomes 0, so no shifting occurs! good luck in your studies!

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)

The signed bit is the left most bit so >>> changes a 1 to 0 but not from 0 to 1, the >> keeps the sign bit so if it was 1 all bit shifted will be 1's and 0 with 0's, hopefully the bold has worked below

Which of the following results in a negative value of a? A: int a = -1; a = a >>> 8; B: byte a = -1; a = (byte)(a >>> 5); C: int a = -1; a = a >> 5; D: int a = -1; a = a >>> 32

A is: 11111111 11111111 11111111 11111111 >>> 8 gives you 00000000 11111111 11111111 11111111 this results in a big poistive int number. B is: converts the byte to an int then does the shifting, then casts back to a byte so bits look like this: byte 11111111 promoted to 11111111 11111111 11111111 11111111 then shift to get 00000111 11111111 11111111 11111111 but we convert back to get the last 8 bits on the right hand side which is 11111111 byte -1 C is: 11111111 11111111 11111111 11111111 then shift with which ever the left most bit was which is 1 to get the same result D is: 11111111 11111111 11111111 11111111 by 32 bits, I needed the pencil first time round, but then i noticed that it can only shift 31 times to give you +1 but 32 times is shifting 0 times so hence the same result of -1. I normally find it easier writing out the bits first then trace out what will happen to the bits. I hope this helps guys Davy [ February 18, 2004: Message edited by: Davy Kelly ]