I'm reading The C Programming Language by K & R 2nd edition and got to the bitwise operators. Why, on C, the Complement (~) Operator applied to a N number, returns -(N + 1) ? When it normally just flips the 1 bits to 0 and the 0 bits to 1.
I'm reading The C Programming Language by K & R 2nd edition and got to the bitwise operators. Why, on C, the Complement (~) Operator applied to a N number, returns -(N + 1) ? When it normally just flips the 1 bits to 0 and the 0 bits to 1.
Share Improve this question edited Mar 6 at 14:11 nerdpanda asked Mar 6 at 8:57 nerdpandanerdpanda 112 bronze badges 3 |2 Answers
Reset to default 1Consider some bit string b, and let u be the number we obtain by interpreting b as an unsigned binary numeral. So, with bit string 10110001, u = 128 + 32 + 16 + 1 = 177.
Two’s complement interprets a fixed-length bit string of n bits:
- If the first bit is 0, the bit string represents the value u.
- If the first bit is 1, the bit string represents the value u−2n.
So, in eight-bit two’s complement, 10110001 represents 177−28 = 177−256 = −79.
Now consider the ~
operator. This operator inverts each bit in a bit string. 10110001 becomes 01001110.
There is another operation that inverts each bit. If we subtract the bit string as a binary numeral from another bit string that is all 1s, of the same length, it inverts all the bits: 111111112 − 101100012 becomes 010011102. This is because, in the subtracting 1−x, where x is a single bit, produces 1 when x is 0 and 0 when x is 1, with no borrow.
Now observe that 111111112 = 1000000002 − 12. In general, for n bits, a binary numeral of n 1s equals 2n − 1.
So the result of ~
applied to an unsigned value u of n bits is 2n − 1 − u.
Now let’s consider applying ~
to a signed value s of n bits:
- If s is non-negative, its bits interpreted as an unsigned binary numeral yield the same value: u = s. Then
~
produces 2n − 1 − u. That bit string starts with 1, so its interpretation in two’s complement is 2n − 1 − u − 2n = −1−u = −(u+1) = −(s+1). - If s is negative, its two’s complement interpretation s equals u−2n. So u = s + 2n. Applying
~
to that gives us 2n − 1 − u = 2n − 1 − (s + 2n) = −1 − s = −(s+1). That bit string starts with 0, so its interpretation in two’s complement is −(s+1).
Thus, when ~
is applied to a signed value s, the result is −(s+1).
(Historical note: The 2024 version of the C standard requires two’s complement for signed integer types. Long ago, some C implementations used one’s complement or sign-and-magnitude, and this was permitted in earlier versions of the standard.)
Basically you are working with bits.
The ~ inverts the bits of a number
5 = 00000101
~5 = 11111010
The ~5 is -6 because of the form -(N+1):
~5 = -(5+1) = -6 = 11111010
In terms of the left-bit, if you have a left-bit that is a 0 it means that your N is a positive number or 0. If you have a 1 in the left-bit than it means that your N is a negative number.
Ex.:
5 = 00000101 // starts with a 0 -> positive
~5 (-6) = 11111010 // starts with a 1 -> negative
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744988085a4604732.html
n
in C,-n
is the negative equivalent. Regardless of if 2's complement or some other signed system is used. Perhaps you mean~N + 1
? – Lundin Commented Mar 6 at 10:39