• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for byte

-2 --> 1111 1110


(-2 >>> 1) gives -1

Can anyone explain?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And three's a crowd?

Mark
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Groan......
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bla ha ha! :roll:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic