Advertisement

Hello,

I'm reviewing some problems, and I need some help. I have a program that has some code like the following:

byte y = 10; // 00001010 in binary

byte result = (byte) (y << 1);

System.out.println("result: " + result); // 20. Ok.

result = (byte) (y << 7);

System.out.println("result: " + result); // 0. Ok.

result = (byte) (y << 8);

System.out.println("result: " + result); // 0. Why???

// I was expecting a shift of 0 bits because the

// right-hand operand is equal to the number of

// bits for the size of the result type--in this case

// 8 bits for a byte.

// 8 % 8 = 0 number of bits for the shift.

result = (byte) (y << 6);

System.out.println("result: " + result); // -128. Ok.

result = (byte) (y << 10);

System.out.println("result: " + result); // 0. Why???

// Shouldn't it be 2 bits for the shift?

// That is, 10 % 8 = 2.

// I was expecting 40 as the the answer for this one.I understand that for binary operations that the operands will be promoted to at least int types before execution occurs, but I still don't see how it would make a difference for the left-shift operator. Any help and clarification on this will be appreciated. It would be helpful to see the binary representation of the the "result" variable for the ones that I'm asking about. Thanks in advance.

I'm reviewing some problems, and I need some help. I have a program that has some code like the following:

byte y = 10; // 00001010 in binary

byte result = (byte) (y << 1);

System.out.println("result: " + result); // 20. Ok.

result = (byte) (y << 7);

System.out.println("result: " + result); // 0. Ok.

result = (byte) (y << 8);

System.out.println("result: " + result); // 0. Why???

// I was expecting a shift of 0 bits because the

// right-hand operand is equal to the number of

// bits for the size of the result type--in this case

// 8 bits for a byte.

// 8 % 8 = 0 number of bits for the shift.

result = (byte) (y << 6);

System.out.println("result: " + result); // -128. Ok.

result = (byte) (y << 10);

System.out.println("result: " + result); // 0. Why???

// Shouldn't it be 2 bits for the shift?

// That is, 10 % 8 = 2.

// I was expecting 40 as the the answer for this one.I understand that for binary operations that the operands will be promoted to at least int types before execution occurs, but I still don't see how it would make a difference for the left-shift operator. Any help and clarification on this will be appreciated. It would be helpful to see the binary representation of the the "result" variable for the ones that I'm asking about. Thanks in advance.

System.out.println("result: " + result); // 0. Why???

// I was expecting a shift of 0 bits because the

// right-hand operand is equal to the number of

// bits for the size of the result type--in this case

// 8 bits for a byte.the result of (y << 8) is an int, not a byte. the byte "y" is promoted to int for the bit shift. so the int result of the bit shift is 00000000 00000000 00001010 00000000. when you cast that back to byte, the 24 leftmost bits get lopped off, and you're left with zero.

hth,

p