• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Bit shifting internals

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
can anyone please explain the internal logic of the bit shifting operators as there are a few doubts like the bit -5<<1 why gives the resulyt of -3 when it shud give -1
this site gives few details but not the whole picture
www.jchq.net/tutorial/BitShift.htm
Thanx in advance
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dont worry too much about this. if you understand the basics of >>,<<, and >>> you will have no problem with any question about bit shifting. they dont try to trick you.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gayathri,
To answer your question if we shift any integer to the left once we actually double the number. Your example answers are both wrong! The answer we would expect is -10.
Here's why:
-5: 0xFFFFFFFB
1111 1111 1111 1111 1111 1111 1111 1011
<< 1: (add one zero to right side) FFFFFFF6
1111 1111 1111 1111 1111 1111 1111 0110
The final result is -10. For negatives it is easier to see value by taking twos complement and just add negative sign:
~: 0000 0000 0000 0000 0000 0000 0000 1001
+1: 0000 0000 0000 0000 0000 0000 0000 1010
0xA = 10 decimal.
Whoa,
Manfred.
[This message has been edited by Manfred Leonhardt (edited January 19, 2001).]
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic