-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 .
-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 .
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.
Source: https://ru.stackoverflow.com/questions/736162/
All Articles