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:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# 2's compliment

Ranch Hand
Posts: 63
• Number of slices to send:
Optional 'thank-you' note:
for byte

-2 --> 1111 1110

(-2 >>> 1) gives -1

Can anyone explain?

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

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

ranger
Posts: 17346
11
• Number of slices to send:
Optional 'thank-you' note:
And three's a crowd?

Mark

Mark Spritzler
ranger
Posts: 17346
11
• Number of slices to send:
Optional 'thank-you' note:
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 ]

Ranch Hand
Posts: 7729
• Number of slices to send:
Optional 'thank-you' note:
Groan......

Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
bla ha ha! :roll:

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.