1

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.

  • 4
    Most likely using 2's complement. But the 24 bits might just point to this question :). – candied_orange Oct 22 '16 at 17:31
  • Where did I go wrong? – lmc Oct 22 '16 at 17:43
  • 1
    You didn't. Google is ignoring the minus sign in a deceptive and unhelpful way. – candied_orange Oct 22 '16 at 18:20
5

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 Oct 23 '16 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.