Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shift operators !

 
Shafkat Talli
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont get shift operators, plz help,
example 1
--------------------------------------------------------
8 >> 1
binary representation:
---------------------
0000 1000
after bit shift
---------------
0000 0100
Comment: This is ok !

example 2
----------------------------------------------
0x80000000 >> 4

binary representation
--------------------
1000 0000 0000 0000 0000 0000 0000 0000
1111 1000 0000 0000 0000 0000 0000 0000 <--- what happened here ?
how is this possible, why did we save the sign bit in the left end in example 2, if u look at tha example above one didnt save the first bit(2^3) when inserting a new bit into (2^2).
Hope u understand my question. I really dont understand this, these examples are from Kathy and Bert book
 
venu gopal
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shafkat,
The left most bit is always sign bit. If it is 0 the number is positive and if it is 1 the number is negative.
I will take 8 first
The binary represention is
0000 0000 0000 0000 0000 0000 0000 1000
Here sign bit is 0
I will take 0x80000000 i.e -2147483648
The binary represention is
1000 0000 0000 0000 0000 0000 0000 0000
Here sign bit is 1.
Let us discuss about your doubts.
example 1
--------------------------------------------------------
8 >> 1
binary representation:
---------------------
0000 0000 0000 0000 0000 0000 0000 1000 int is 32 bits long
after bit shift
---------------
0000 0000 0000 0000 0000 0000 0000 0100 Shifted by 1 bit
I know it's clear to you

example 2
----------------------------------------------
0x80000000 >> 4

binary representation
--------------------
1000 0000 0000 0000 0000 0000 0000 0000
1111 1000 0000 0000 0000 0000 0000 0000 <--- what happened here ?
Here we shifted 4 bits to right. The sign bit is 1. In right shifting left bits will be filled by sign bits.( Here 1 ).
If you perform right shift on 4
0000 0000 0000 0000 0000 0000 0000 1000
0000 0000 0000 0000 0000 0000 0000 0000 <--- what happened here ?
Here we shifted 4 bits to right. The sign bit is 0. In right shifting left bits will be filled by sign bits.( Here 0 ).
The result is
0000 0000 0000 0000 0000 0000 0000 0000 ----> Zero

Hope you understand???
Thanks,
Venu Gopal.
 
Shafkat Talli
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i get it, its da sign bit one follows, not the bit one stand on, thanx Venu, u eased my life now )))))))))) Ur da man !
 
Greg Neef
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you don't want the sign bit propagated but instead want 0 to fill on the left use the >>> operator
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wanna brush up my memory...
How do you convert hex to decimal (0x80000000 into -2147483648??)
How to convert octal to decimal and vice-versa?
Any good links?
Thanks
-PC
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic