# 2's compliment

shri mon
Ranch Hand
Posts: 63
for byte

-2 --> 1111 1110

(-2 >>> 1) gives -1

Can anyone explain?

Kristof Janssens
Greenhorn
Posts: 23
I can't explain that because the answer should be(have you tested that code?):

-2 >>> 1 = 2147483647

-2: 1111 1111 1111 1111 1111 1111 1111 1110

>>>1 (move 1 to the right): 0111 1111 1111 1111 1111 1111 1111 1111

this is the max_value of an int, you lose the -

-2 >> 1 = -1
>>1: (move 1 to the right and keep the sign-bit): 1111 1111 1111 1111 1111 1111 1111 1111

Balazs Borbely
Ranch Hand
Posts: 33
For byte, shri mon is right.

Try this one:

The reason is the promotion to int before of the expression 'b1>>>1' is evaluated.

so, b1 promoted to int
b1: 1111 1111 1111 1111 1111 1111 1111 1110
'b1>>>1': 0111 1111 1111 1111 1111 1111 1111 1111

and after 'b1>>>1' narrowed to byte 0111 1111 1111 1111 1111 1111 1111 1111
-> 1111 1111 what is -1

Mark Spritzler
ranger
Sheriff
Posts: 17278
6
And three's a crowd?

Mark

Mark Spritzler
ranger
Sheriff
Posts: 17278
6
Sorry for hijacking here, but here's a good joke.

A guy walks into a bar and sits at the bar. He orders a drink, as the bartender is on the otherside of the bar, the guy hears. "Nice Tie".

He looks around and sees no one around. A couple of seconds later, he hears "You have great looking eyes". Again he looks around and sees nothing.

So the bartender returns with they guys order, and the guy asks, "I am sitting here and I keep hearing 'nice tie', 'nice eyes', but there's no one around, am I just hearing voices or something?"

So the bartender looks down and points on the bar and says, "Complimentary nuts"

Mark
[ March 11, 2005: Message edited by: Mark Spritzler ]

Barry Gaunt
Ranch Hand
Posts: 7729
Groan......

Louie van Bommel
Ranch Hand
Posts: 76
bla ha ha! :roll: