## backtobasics

architecture# 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

Hi Sukhvinder,very good wrap up.looking forward for more.

Whats up, This is a great summation, I located your blog checking google for any similar subject and discovered this. I couldnt find too much other tips and information on this posting, so it was wonderful to discover this one. I will possibly be returning to look at a number of other articles or blog posts that you just have written an additional time.

My partner and I stumbled over another web address plus thought I may as well check things out. We like your article and what We discover thus now I am following you. Looking forward to looking over ones blog page backToBasics : Java BitWise Shift operators, back to basics, Sukhvinder Singh, | Sukhvinder Singh yet again… By the way my thoughts go with people at atomic plant we hope you are healthy along with comfortable too ðŸ™‚ Thx ! Easy Web Site Builder

Thank you for influencing so many readers with your great information. Your article has sparked a lot of thought for me. I share your views. I will shurely insert a link to http://www.sukhvinder.co.uk/articles/standard-java/backtobasics-java-bitwise-shift-operators on my site.

Stimulating article. I recognize Iâ€™m a little bit late in posting the comment however the article would have been on the purpose in addition to merely the details I became looking for. I canâ€™t say i trust all you could mentioned however it was undoubtedly fascinating! BTWâ€¦I found your internet site through a new Google seek. Iâ€™m the frequent visitor in your blog which enable it to return again soon.

You probably make this seem simple with your own presentation yet I discover this topic to get actually an issue that I think I might never fully grasp. It appears to be too complex and incredibly broad to me. I am excited for the next post, Iâ€™ll try to find the hang of computer!

Wow, great blog article.Really thank you! Want more.