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.
-
4Most likely using 2's complement. But the 24 bits might just point to this question :).candied_orange– candied_orange2016年10月22日 17:31:58 +00:00Commented Oct 22, 2016 at 17:31
-
Where did I go wrong?lmc– lmc2016年10月22日 17:43:14 +00:00Commented Oct 22, 2016 at 17:43
-
1You didn't. Google is ignoring the minus sign in a deceptive and unhelpful way.candied_orange– candied_orange2016年10月22日 18:20:21 +00:00Commented Oct 22, 2016 at 18:20
1 Answer 1
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.
-
Nice answer. To add to that, the way to represent negative numbers in binary was not always so clear. In the old early days, some used 1's complement representation.Erik Eidt– Erik Eidt2016年10月23日 23:44:11 +00:00Commented Oct 23, 2016 at 23:44