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.
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
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
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
Free, earth friendly heat - from the CodeRanch trailboss