Right and Left shift operators

The right and left shift operators are binary operators.One operand represents the number in which bits are to be shifted and the other operand represents the shift positions.The shift operators are applied on integer data type variables.

Right shift operator

The right shift operator shifts all the bits of a binary number in the right direction.

Syntax:

operand>>number

Example:

6>>2

Binary representation of 6 in bits.

0000       0110

The operator shifts the bits of this binary number to right by 2 positions,the two rightmost bits are discarded.

0000      0001 6>>2=1

The formula is                 operand / 2(power)number.

6/2power2=6/4=1

Left shift operators

The left shift operator shifts all the bits of a binary number in the left direction.

Syntax:

operand<

Example:

4<<2

Binary representation of 4 in bits.

0000       0100

The operator shifts the bits of this binary number to left by 2 positions,the two leftmost bits are discarded.

0001    0000

The formula is                 operand *2(power)number.

4*2power2=4*4=16

Unsigned shift operator

unsigned shift operator is used to shift the bits of a binary number to right.The operator fills leftmost bits of a binary value with 0 irrespective of whether the number has 0 or 1 at the leftmost bit.

example:

6>>>24

Binary representation of 6in bits.(64 bit representation)

00000000 00000000 00000000 00000110

output:

00000000 00000000 00000000 00000000

 

 

 

 

 

 

 

 

 


Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet