Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Shift operators !

Shafkat Talli
Ranch Hand
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

venu gopal
Ranch Hand
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.
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
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
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
wanna brush up my memory...
How do you convert hex to decimal (0x80000000 into -2147483648??)
How to convert octal to decimal and vice-versa?