I have this statement:
Assume the bit value of byte
x
is 00101011. what is the result ofx>>2
?
How can I program it and can someone explain me what is doing?
See Question&Answers more detail:osI have this statement:
Assume the bit value of byte
x
is 00101011. what is the result ofx>>2
?
How can I program it and can someone explain me what is doing?
See Question&Answers more detail:osFirstly, you can not shift a byte
in java, you can only shift an int
or a long
. So the byte
will undergo promotion first, e.g.
00101011
-> 00000000000000000000000000101011
or
11010100
-> 11111111111111111111111111010100
Now, x >> N
means (if you view it as a string of binary digits):
00000000000000000000000000101011 >> 2
-> 00000000000000000000000000001010
11111111111111111111111111010100 >> 2
-> 11111111111111111111111111110101