This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hope i am not irritating you with this answer. 1) Eventhough you have provided the negative number after the shift operator, it is getting considered as positive(considered as unsigned). In a 8-bit signed integer representation -1 is (1111 1111). In unsigned it becomes 256. So if we give -6 >> -1 is equal to -6 >> 256(consider the equivalent in 32 bit system i.e. 65536). So in a 8 bit system, if i shift it by 256 times on the right side(with sign preserved), then it becomes -1 (for -ve) and 0(for +ve). So the formula may be written like this.
(In a 32 bit representation)
x >> y = (x / 2^y) (if y < 32) = 0 (if x is +ve and y >=32 or y<0) = -1 (if x is -ve and y>=32 or y<0)
the point to be remembered is that y will be considered always positive. So any negative number in signed system will become a positive number on the higher end. I haven't checked my observation with programs. Please inform me, if you find some mistakes.
Joined: Jul 17, 2005
Sorry, i made a mistake . Only in 16 bit system -1(signed) becomes 65536 (unsigned).