-7 >> 1 

It turns out -4.

It was 1000 0111 , moved to the right. Like it should turn out 1000 0011 . How did it happen that it turned out 1000 0100 ? What did I miss?
For simplicity, let it be byte .

  • 2
    And why did you decide that -7 is exactly what is written down?) Read about the additional code. - pavel

1 answer 1

Actually, -7 in the additional code will be not 1000 0111 , but 1111 1001 . After a shift to the right, 1111 1100 obtained, which is equal to -4. That's right, the computer was not wrong.

PS be careful with such shifts. In C ++, they give UB.

  • The question now is why the sign bit multiplies and how to avoid it. - pavel
  • And what exactly is UB here? - Flowneee
  • @Flowneee bit shift according to the standard only in unsigned apply. - pavel
  • @Flowneee negative number shift - Pavel Mayorov
  • @PavelMayorov and pavel thank you (sorry you can’t normally have 2 participants put in the message normally) - Flowneee