• 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

Shifting Negative Numbers

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. In the RHE exist a topic about shifting negative numbers but I can't understand it. Anyone can help me??? Anyone have a code example???
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following link may be of help:
http://www.javaranch.com/ubb/Forum24/HTML/013567.html
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well there are three different shift operators:
<< left shift<br /> >> signed right shift
>>> unsigned right shift
Left shifting a negative number can cause the number to change its sign depending on which bit gets shifted into the leftmost position.
int i = i << 1;<br /> 1010 0000 0000 0000 0000 0000 0000 0100 <br /> 0100 0000 0000 0000 0000 0000 0000 1000<br /> Sign changed to positive.<br /> int i = i << 2;<br /> 1010 0000 0000 0000 0000 0000 0000 0100 <br /> 1000 0000 0000 0000 0000 0000 0001 0000<br /> Sign remains negative.<br /> Signed Right Shift will always retain the same sign. Because the leftmost bit (which determines the sign) is what is shifted in. So a negative number remains negative.<br /> int i = i >> 1;<br /> 1010 0000 0000 0000 0000 0000 0000 0100<br /> 1101 0000 0000 0000 0000 0000 0000 0010<br /> Unsigned Right shift may or may not change the sign depending on the right operand of the shift operator.<br /> int i = i >>> 1;
1010 0000 0000 0000 0000 0000 0000 0100
0101 0000 0000 0000 0000 0000 0000 0010
In this case the sign is changed.
int i = i >>> 32;
1010 0000 0000 0000 0000 0000 0000 0100
1010 0000 0000 0000 0000 0000 0000 0100
In this case there is no shifting so the sign remains negative.
 
reply
    Bookmark Topic Watch Topic
  • New Topic