• Post Reply Bookmark Topic Watch Topic
  • New Topic

2's compliment  RSS feed

 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16049
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
Campbell Ritchie
Marshal
Posts: 56227
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
,i know binary cannot be input
Yes it can.OR

CR
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!