When using the shift operator, be very careful not to repeat the common mistake!
As follows from the following SO post , the author of the accepted answer mentions:
"In some languages, applying shift operators to any data type smaller than int automatically changes the size of the operand, INT."
It is absolutely important to remember, for example, when working with bytes, otherwise you may get unexpected results (like me).
The specified byte with the following bit pattern:
1001 0000
When I tried to shift the bit by 4 and assign an int, for example:
int value = byteValue >>> 4;
I would expect:
0000 1001 (or a value of 9)
But I would get a HUGE number! This is because byteValue is passed to the bit offset operation in int BEFORE , which leads to something like this:
1111 1111 1111 1111 1111 1111 1001
Jeach Nov 07 2018-12-12T00: 00Z
source share