0

I know the binary representation of -127 is 10000001 (complement).

Can any body tell me why I right shift it by 1 digit, then I get 11000000 ?

(-127) = 10000001

(-127>>1) = 11000000 ???

Thanks.

user1481096
  • 307
  • 1
  • 8

5 Answers5

3

Right-shifting in some languages will pad with whatever is in the most significant bit (in this case 1). This is so that the sign will not change on shifting a negative number, which would turn into a positive one if this was not in place.

Emil Vikström
  • 84,510
  • 15
  • 132
  • 168
3

If your programming language does a sign-extending right shift (as Java does), then the left-most 1 comes from extending the sign. That is, because the top bit was set in the original number it remains set in the result for each shift (so shifting by more than 1 has all 1's in the top most bits corresponding to the number of shifts done).

This is language dependent - IIRC C and C++ sign-extend on right shift for a signed value and do not for an unsigned value. Java has a special >>> operator to shift without extending (in java all numeric primitive values are signed, including the misleadingly named byte).

Lawrence Dol
  • 59,198
  • 25
  • 134
  • 183
  • C and C++ *do* sign-extend when shifting `signed int`'s. This makes the fact that the default sign of C/C++ `char`'s is *unspecified* a bit annoying... – comingstorm Jun 25 '12 at 21:59
  • Thanks. This make sense. I code it in Java, and the type of value is BYTE. – user1481096 Jun 25 '12 at 21:59
  • @AlKepp No. In java, BYTE is signed. Therefore, the range is -128~127. check this : [link](http://stackoverflow.com/questions/3621067/why-is-the-range-of-bytes-128-to-127-in-java) – user1481096 Jun 26 '12 at 01:39
  • @comingstorm: Actually C/C++ are implementation defined when right-shifting signed values, so different compilers might do different things. Even worse, they'e UNDEFINED when left-shifting negative signed values. – Chris Dodd Oct 09 '12 at 18:18
2

-127 as a WORD (2 bytes) is 1111111110000001. If you right shift this by 1 bit, and represent it as a single byte the result is 11000000 This is probably what you are seeing.

Jon
  • 3,080
  • 1
  • 13
  • 26
2

Because, if you divide -127 (two's-complement encoded as 10000001) by 2 and round down (towards -infinity, not towards zero), you get -64 (two's-complement encoded as 11000000).

Bit-wise, the reason is: when right-shifting signed values, you do sign-extension -- rather than shifting in zeroes, you duplicate the most significant bit. When working with two's-complement signed numbers, this ensures the correct result, as described above.

Assembly languages (and the machine languages they encode) typically have separate instructions for unsigned and signed right-shift operations (also called "logical shift right" vs. "arithmetic shift right"); and compiled languages typically pick the appropriate instruction when shifting unsigned and signed values, respectively.

comingstorm
  • 23,012
  • 2
  • 38
  • 64
1

It's sign extending, so that a negative number right shifted is still a negative number.

user1277476
  • 2,787
  • 10
  • 8