It seems that you're confusing the concepts of 2's complement and sign-bit, and bitwise-or and addition in C.
(And who would expect 6 + (-5) ≠ 1…)
(1) 2's complement and sign-bit
On most architectures negative numbers are implemented as a 2's complement of its magnitude. While the C standard does not mandate 2's complement being used, it is the most popular one. In 2's complement, the negative of x
is constructed as ~x + 1
, e.g.
5 = 0000 0101
~5 = 1111 1010 # ~x means 1's complement, a.k.a. bitwise-NOT
~5+1 = 1111 1011 # +1 means add 1 to the integer.
This is the binary representation of -5. The highest bit of the integer is called the sign bit because it determines the sign of the integer. All negative integers in 2's complement has sign bit = 1. But just flipping the sign bit doesn't negate the number. It is merely an indicator that the number is negative or not.
(2) Bitwise-or and addition in C
+
means integer addition in C and all other practical languages, because integer addition much more useful than bitwise-or. So although OR is widely written as +
in logical proofs, no programming languages recognize +
for bitwise-OR.
In C a char
is just a signed integer which is exactly 1-byte long. The behavior is same as all other kinds of signed integers. You have to use |
for bitwise-OR.
0000 0110 0000 0110
+ 1000 0101 | 1000 0101
——————————— ———————————
1000 1011 1000 0111
^^--- note the carry ---^^