How does bitshifting work in Java?

Viewed 127838

I have this statement:

Assume the bit value of byte x is 00101011. what is the result of x>>2?

How can I program it and can someone explain me what is doing?

10 Answers

Shift Operators

The binary 32 bits for 00101011 is

00000000 00000000 00000000 00101011, and the result is:

  00000000 00000000 00000000 00101011   >> 2(times)
 \\                                 \\
  00000000 00000000 00000000 00001010

Shifts the bits of 43 to right by distance 2; fills with highest(sign) bit on the left side.

Result is 00001010 with decimal value 10.

00001010
    8+2 = 10
Related