Shift operators in java , a tutorial

A shift operator will shift the bits by a number of positions . In java we have three shift operators , they are

  • The right shift signed operator >> , which shift the bits to the right , while keeping the sign bit. So if the sign bit is 1 , it is kept as 1 and if the sign bit is 0 it is kept as 0 .
  • the right shift unsigned operator >>> , which shift the bits to the right keeping a sign bit of 0 , as such it is called unsigned .
  • the left shift operator << , which shift the bits to the left and it doesn’t care what the sign bit becomes . So it can become 0 or 1 .

we can shift by either a positive number of by a negative number.

the number we shift by is converted to bits , and we take

  • the 5 lowest bit in case the type before the shift operator is an int or is promoted to an int .
  • the lowest 6 bit in case the type before the shift operator is a long .

So the maximum number that we can shift by for an int or for a type that can be promoted to an int is 31 bits , and the maximum number of bits that we can shift by for a long is 61 bits .

The right signed shift operator >>

the right signed shift operator keeps the sign bit .

  • If the number is positive , its bits are shifted to the right by the provided number of bits . The shifted bits are replaced by zero .
  • if the number is negative ,its bits are shifted to the right by the provided number of bits. The shifted bits are replaced by 1 .

To understand this better let us see some examples . we will do shifting by using 4 bits .

  • in the first example , we are shifting a positive number 7 . Its bits are shifted by 1 position . So bits at position 3 , 2 , 1 , 0 are shifted by 1 . Bit 3 become bit 2 , bit 2 become bit 1 , bit 1 become bit 0 , and bit 0 is dropped . The shifted bits are replaced by zero , as such bit 3 or the sign bit becomes 0 and The result is 3 . Shifting a positive number is equivalent to dividing by 2 and keeping only the integral part of the division .
  • in the second example , we are shifting the bits of 7 by two position . So position 3 becomes position 1 , and positions 2 becomes position 0 and position 1 and 0 are dropped . In the place of the shifted position we place zeros . Hence the result is 1.
  • in the fifth example , we have a negative number which is -1 . We shift its bits by 1 . As such bits at position 3, 2 , 1 , 0 are shifted to the right . Position 0 is dropped , and the shifted positions are replaced by 1 , so we end up with -1 .
  • in the six example , the negative number -1 is shifted by 2 positions . So position 0 and 1 are dropped and position 3 and 2 will take the position 1 and 0 respectively . The shifted bits are replaced by 1 , as such the result of the shifting is -1

The right unsigned shift operator >>>

The right unsigned shift operator will shift the bits of a number by the specified value , and it will replace the shifted bits by a zero . So it is just as if we are keeping a sign bit of zero, as such it is called unsigned .

  • in the first example we are shifting the number 7 by one position . As such bits 3,2,1,0 are shifted by 1 position . The bit at position 0 is dropped , and the shifted bits are replaced by 0 . hence 7 will become 3 . When shifting a positive number the unsigned shift operator act the same as the signed shift operator .
  • in the second example , the result is 1 .
  • in the third example , the result is 0 .
  • in the fifth example we are shifting the negative number -1 by one position . so bits at position 3 , 2 ,1 ,0 are shifted by one position . Bit at position zero is dropped and the shifted bits are replaced by 0 . As such the result of the shifting is 7 .

The left shift operator <<

The left shift operator will just shift the bits of a number to the left by the specified value . The shifted bits are replaced by a zero .

  • In the first example , we have a positive number 7 which is shifted by 1 place , as such bits in position 3 , 2 , 1 , 0 are shifted to the left by 1 position . Hence bit 3 is dropped , and replaced by bit 2 , bit 2 by bit 1 , bit 1 by bit 0 and bit 0 is replaced by a zero . hence the result is -2 .
  • in the six example , we are shifting the bits of the number -1 by 2 position . Hence bits 3 and 2 are dropped and replaced by bits 1 and 0 . Bits 1 and 0 are replaced by a zero , and the result is -4 .

shifting by a number larger or equal to the number of bits , and by a negative number

When we are shifting the bits , we specify the value that we want to shift by . The value that is actually taken is

  • the last five bits of the specified value , when the number to shift is an integer or can be promoted to an integer . As such we can shift by a value between 0 and 31 inclusive .
  • the last six bits of the specified value , when the number to shift is a long . A such we can shift by a value between 0 and 63 .

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *