How would a computer represent -6886707 using 24 bits?
I need someway to verify this: 100101101110101011001101.
First I converted 6886707 to binary, which is 11010010001010100110011. Then I put one zero at the left in order to have 24 bits, then I switched the zeros into ones, and ones into zeros, and added 1.
3
The 2’s complement way to express -6886707 using 24 bits is, acording to:
100101101110101011001101 <- you
100101101110101011001101 <- exploringbinary.com
11111111100101101110101011001101 <- win10 calculator in Programmer-DWORD (32 bit) mode
The win10 calculator agrees with you. It just doesn’t happen to have a 24 bit mode. Chop off the extra 8 bits and you get 24 bits that match. Those extra 1’s come from something called sign extension.
So you didn’t do any math wrong. Flipping the bits and adding 1 is exactly how to do two’s complement negation.
Don’t trust all base 10 to binary converters to do 2’s complement. exploringbinary.com does it, as I confermed with a -1 test. -1 should give you 1’s in every bit.
However, binaryhexconverter.com doesn’t and google doesn’t. They do positive numbers fine but don’t trust them with your negatives.
Walking through the steps:
Base 10 24 bits in Base 2
6886707 = 011010010001010100110011
Not (flip the bits)
-6886708 = 100101101110101011001100
Add 1
-6886707 = 100101101110101011001101
Negate (change sign ±)
6886707 = 011010010001010100110011
Add we’re back where we started. As far as I can tell your math is fine.
The lesson of 2’s complement is that the two operations: Not
and Add 1
done in order give the same result as Negate
.
It also works this way:
Base 10 Base 2 in 24 bits
-6886707 = 100101101110101011001101
Not (flip the bits)
6886706 = 011010010001010100110010
Add 1
6886707 = 011010010001010100110011
Negate (change sign ±)
-6886707 = 100101101110101011001101
Said in a more functional composition way:
Negate(x) == AddOne(BitWiseNot(x)) is always true.
Which means you can use a bitwise not and addition to define negation.
Keep in mind 100101101110101011001101 is only equal to -6886707 when you take it as signed. When you don’t it’s 9890509.
1