# >>> operator

Sean Walker

Ranch Hand

Posts: 64

posted 13 years ago

I encoutered the following question:

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?

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?

Sean Walker<br />PMP, SCEA, SCWCD, SCJP

Adam Altmann

Greenhorn

Posts: 21

posted 13 years ago

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.

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.

SCJP 1.4

Bert Bates

author

Sheriff

Sheriff

Posts: 8919

11

posted 13 years ago

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!

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.)

Davy Kelly

Ranch Hand

Posts: 384

posted 13 years ago

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

A is:

B is:

converts the byte to an int then does the shifting, then casts back to a byte so bits look like this:

byte

C is:

D is:

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 ]

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:

**1**1111111 11111111 11111111 11111111 >>> 8 gives you**0**0000000 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

**1**1111111 promoted to 11111111 11111111 11111111 11111111 then shift to get**0**0000111 11111111 11111111 11111111 but we convert back to get the last 8 bits on the right hand side which is**1**1111111 byte -1C is:

**1**1111111 11111111 11111111 11111111 then shift with which ever the left most bit was which is 1 to get the same resultD is:

**1**1111111 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 ]

How simple does it have to be???