aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift operators ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift operators !" Watch "Shift operators !" New topic
Author

Shift operators !

Shafkat Talli
Ranch Hand

Joined: Aug 01, 2003
Posts: 30
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


---------------------------<br />Shafkat Talli<br />SCJP 1.4, August 2003.
venu gopal
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
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

Joined: Aug 01, 2003
Posts: 30
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

Joined: Jun 16, 2003
Posts: 82
if you don't want the sign bit propagated but instead want 0 to fill on the left use the >>> operator


SCJP 1.4
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
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
 
wood burning stoves
 
subject: Shift operators !
 
Similar Threads
left shift operation
Help Me Out With Shift Operators...
bit shift operators
Problem in Shifting Operators
operators