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
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Shift operators !

Ranch Hand
Posts: 30
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 40
• Number of slices to send:
Optional 'thank-you' note:
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.
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
• Number of slices to send:
Optional 'thank-you' note:
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 !

Ranch Hand
Posts: 82
• Number of slices to send:
Optional 'thank-you' note:
if you don't want the sign bit propagated but instead want 0 to fill on the left use the >>> operator

Ranch Hand
Posts: 112
• Number of slices to send:
Optional 'thank-you' note:
wanna brush up my memory...
How do you convert hex to decimal (0x80000000 into -2147483648??)
How to convert octal to decimal and vice-versa?