backToBasics : Java BitWise Shift operators

Have you designed a bitboard? Yes..Skip the reading..:)

Guys I am happy to start “backToBasics series” for java and related technologies. The bitwise operators mostly used for setting individual flag bits in value, prerequisite for following explanation is binary numbers and 2’s complement representation.

The bitwise shift operators are : >> , << , >>>

  • Operands of byte, char, or short type are implicitly upcast to int before the operation.
  • Bitwise shift operators are applicable only to int and long, we can not use with floating point, boolean, array or any java object.
  • If the left operand of a shift operator is a long, the result is a long; otherwise, the result is an int.

[tip]leftOperandType*[>> or >>> or <<]*rightOperandType = leftoperandType

here the ‘leftOperandType’ or ‘rightOperandType’ could be only ‘int’ or ‘long'[/tip]

  • The bitwise operator could only shift the bits of already defined bits size of ‘int’ and ‘long’.

The ‘int’ defined size is 32, so only right most 5 bits of an int right operand are used. effective range would be only 0 to 31.

The ‘long’ defined size is 64, so only right most 6 bits of an long right operand are used. effective range would be only 0 to 63.

  • Bitwise right shift operator “>>” preserves the sign bit(left most bit), the left most bits are filled with original value.
  • Bitwise right shift operator “>>>” is unsigned which fills ‘0’s into the left most bits.
  • Bitwise left shift operator “<<” preserves the sign bit(left most bit), the right most bits are filled with original value.
  • If the left operand is long (greater then Integer.MAX_VALUE i.e. 2147483647) then right operand’s right most 6 bits will be considered(otherwise 5 bits, as stated in above point 4)

Lets explore BitWise operators with examples:

“>>” shift right operator

Example 1: positive integers 20>>2 = 5

20 in binary is: 00000000000000000000000000010100
after shift all bits 2 positions to right 00000000000000000000000000000101
equivalant to 5 in binary form

Example 2: negative integers -10>>2 = -3

-10 in binary is: 11111111111111111111111111110110
after shift all bits 2 positions to the right AND insert 1’s to left = 11111111111111111111111111111101
equivalant to -3 in binary form

“>>>” unsigned shift right operator

Example 3: negative integers -10>>>2 = 1073741821

-10 in binary is: 11111111111111111111111111110110
after shift all bits 2 positions to the right AND insert 0’s to left = 00111111111111111111111111111101
equivalant to 1073741821 in binary form

Example 4: positive integers 10>>>2 = 2

10 in binary is: 00000000000000000000000000001010
after shift all bits 2 positions to the right AND insert 0’s to left = 00000000000000000000000000000010
equivalant to 2 in binary form

“<<” shift left operator

Example 5: positive integers 20<<2 = 80

20 in binary is: 00000000000000000000000000010100
after shift all bits 2 positions to left =00000000000000000000000001010000
equivalant to 80 in binary form

Example 6: negative integers -10<<2 = -40

-10 in binary is: 11111111111111111111111111110110
after shift all bits 2 positions to the left AND insert 1’s to left= 11111111111111111111111111011000
equivalant to -40 in binary form

boundary condition showing point 8,

Example 7:

If rightOperandValue is 32 ( binary value 100000)
The Integer.MAX_VALUE is 2147483647

int value, 2147483647>>32 = 2147483647, its taking right most 5 bits
long value, 2147483647L>>32 = 0, its taking right most 6 bits

reference:
1.http://en.wikipedia.org/wiki/Bitwise_operation
2.http://onjava.com/pub/a/onjava/2005/02/02/bitsets.html?page=2
3.Various web articles

Related Posts

Leave a Reply

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