Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shift Operators

 
Shivani Prajapati
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn�t understand the operators >> and >>>. Can anyone help in it?

Thanks.
 
Nikhil Bansal
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shivani,

Right Shift Operaor >> is used to shift bits to the right for a given number.This operator will hold the sign of the number.

For example, if you have

int x = 0x80000000
x = x >> 4;

Here 0x80000000 equals -2147483648 in decimal and
1000 0000 0000 0000 0000 0000 0000 0000 in binary.

Since the most significant bit is 1 which represents the sign bit, so the right shift operator will fill four 1's from the left.

hence the number will be

1111 1000 0000 0000 0000 0000 0000 0000 or -134217728 in decimal.


>>> is the unsigned right shift operator which will not preserve the sign or the sign of the resultant number will always be positive.

Hence x >>> 4 will give you 0000 1000 0000 0000 0000 0000 0000 0000 or 134217728 in decimal.

Hope it helps.

Nikhil Bansal
 
wise owen
Ranch Hand
Posts: 2023
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic