Negative Bitwise Operations

I read a book in C where some section says, "Bitwise operations are usually used with unsigned types."

Question: why?

+4
source share
4 answers

Just because it is not immediately clear what bit operations on the sign bit of a signed number should mean.

  • Unsigned types have no special bits, everything works straight forward.
  • Signed types have a special sign bit and can interpret this using three different encodings to represent negative values ​​(one and two additions or a sign and a value).
+5
source

, . , .

, 8- 8 , 0..255 ( , -128 +127).

0

.

, .

0

.

C 3 : , .

, -1:

  • |1000|0000|0000|0001| 16- .
  • |1111|1111|1111|1110| 16- .
  • |1111|1111|1111|1111| 16- .

, -1 & 3 3 :

  • 1 ,
  • 2 ,
  • 3 ,

, << >>, DS9K.

, , ( ).

0

Source: https://habr.com/ru/post/1523312/


All Articles