I have a sample code snippet like this:
byte a = -0b00001111;
byte b = a;
byte c = a;
System.out.println("a=" + a );
System.out.println("b=" + (b >> 1) );
System.out.println("c=" + (c >>> 1) );
and, it prints:
a=-15
b=-8
c=2147483640
I don't quite understand how b and c became those 2 values respectively, could someone demonstrate me in steps how those 2 values were calculated please?
For byte a
, you have the literal 0b00001111
, which is binary for 15
, so a
is -15
. The bit representation of -15
for a byte
is:
11110001
In Java, unary numeric promotion occurs on the operands of bit-shift operators <<
, >>
, and >>>
.
Unary numeric promotion (§5.6.1) is performed on each operand separately.
This means that the value is promoted to int
before shifting.
The code (b >> 1)
promotes b
to an int
, then shifts the value with sign extension. This means that if the value was already negative, then a 1
bit is shifted to ensure it's still negative.
11110001
is promoted to
11111111 11111111 11111111 11110001
which is -15
as an int
. After shifting to the right one bit, with sign extension:
11111111 11111111 11111111 11111000
which is -8
.
However, for the code (c >>> 1)
, the >>>
unsigned right shift operator does not perform sign extension, even if the promotion to int
does maintain the sign and the value.
11110001
is promoted to
11111111 11111111 11111111 11110001
which is -15
as an int
as before. After unsigned shifting to the right one bit:
01111111 11111111 11111111 11111000
The first bit is now 0
. Without the most significant bit set, the value is now 231 - 8, or 2147483640.