• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Diifference in Bitwise Operators

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the basic difference in >> and >>>?? I got that >> is for signed & >>> for unsigned. But what i did not get is, in >> sign get copied & in >>> no sign get copied.
What exactly it means by Sign get copied.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integers are stored in two's complement format in Java (as in almost every other programming language). The leftmost bit of an int is the sign bit - if it's set, the number is negative, if it's cleared, the number is positive.

The >> operator shifts all the bits of the operand to the right and fills in the new bits on the left by copying the sign bit of the operand.

The >>> operator shifts all the bits of the operand to the right and sets the new bits on the left to zero.

For example: Suppose you have the number 11000101.

11000101 >> 2 = 11110001 note that the leftmost bits are set to 1

11000101 >>> 2 = 00110001 note that the leftmost bits are set to 0
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:Integers are stored in two's complement format in Java (as in almost every other programming language). The leftmost bit of an int is the sign bit - if it's set, the number is negative, if it's cleared, the number is positive.

The >> operator shifts all the bits of the operand to the right and fills in the new bits on the left by copying the sign bit of the operand.

The >>> operator shifts all the bits of the operand to the right and sets the new bits on the left to zero.

For example: Suppose you have the number 11000101.

11000101 >> 2 = 11110001 note that the leftmost bits are set to 1

11000101 >>> 2 = 00110001 note that the leftmost bits are set to 0



Ohk. Please correct me if i am wrong. by the example i understood as, we will delete the two right bits & shift all remaining towards right & for deleting two bits, we will be adding two 1s in case of >> and two 0s in case of >>> to the left most side.
Please respond.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishnu Sharma wrote:
Ohk. Please correct me if i am wrong. by the example i understood as, we will delete the two right bits & shift all remaining towards right & for deleting two bits, we will be adding two 1s in case of >> and two 0s in case of >>> to the left most side.
Please respond.




Almost.... Given this...

X0101010 where X could be either 1 or 0.


X0101010 >> 2 will yield XXX01010

and

X0101010 >>>2 will yield 00X01010


So, if X is zero, the two expressions will yield the same result (ie. >> doesn't always add 1's on the left).

Henry
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote about that a long time ago: here.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome thread... I just bookmarked it
 
reply
    Bookmark Topic Watch Topic
  • New Topic