• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

2's compliment

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How the 2's copmliment is performed while doing signed left shifting and
and arithmatic operations when number is in negative.?

say for example,
(in binary,)

int x=-1;

so x=1000 0000 0000 0000 0000 0000 0000 0001; (this is in binary, for
understanding,i know binary cannot be input)

x=x>>30;

here x is printed as -1. why and how is this?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, -1 is not 1000 ... 0001, it is all 1's.

So you have x = 1111 1111 1111 1111 1111 1111 1111 1111 (binary) and you shift right with sign extension - all the bits will still be 1's, and the result is still -1.

The pattern 1000 ... 0001 is -2147483647, not -1.

Two's complement
[ July 31, 2006: Message edited by: Jesper Young ]
 
Marshal
Posts: 77291
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

,i know binary cannot be input

Yes it can.OR

CR
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic