Number base converter and two's complement

Your data

A leading 0b, 0o or 0x is accepted and ignored, and so are spaces and underscores between the digits.

Results

What those bits are worth, read the way you chose

The very same bits, read the other way
In binary
In hexadecimal
In octal
Everything this width can hold

Negating it, step by step

Start from the bits
Flip every one of them
Add one, and that is the negative
Which comes to

Flipping the bits and adding one is the same thing as subtracting from two to the power of the width, and it was checked against all two hundred and fifty six patterns of eight bits. Doing it twice always gives back what you started with, which is the property that makes the whole scheme work.

This page counts in whole numbers of any size rather than in the usual floating point, because past nine quadrillion or so the ordinary kind starts skipping values: two to the fifty third plus one comes out as two to the fifty third. At sixty four bits that would quietly give wrong answers.

Reactions

0

0 Comments

User profile image

Be the first to comment

Why does 200 come out as minus 56?

Because 200 and minus 56 are the same eight bits: 11001000. Nothing in those bits says which of the two you meant. The width and the reading are decided elsewhere, by whoever wrote the program, and the bits go along with either answer.

The whole difference is the leftmost bit. While it is off, the two readings agree exactly, which is why the disagreement stays invisible until a value crosses the halfway point.

Why is the range minus 128 to 127 instead of minus 127 to 127?

Because zero has to live somewhere, and it lives on the positive side. Eight bits give 256 different patterns; one of them is spent on zero, leaving 255 to share. The split is 127 positive and 128 negative, so the negative side gets one more.

It is the same at every width: 16 bits run from minus 32768 to 32767, and 64 bits from minus 9223372036854775808 to 9223372036854775807.

Is there a number with no opposite?

Yes, exactly one per width: the most negative. Minus 128 in eight bits has no positive twin, because 128 would need a slot the positive side does not have.

Flip the bits of 10000000 and you get 01111111; add one and you are back at 10000000. Negating it hands you the same number. This page shows that happening rather than hiding it, and the property was checked at 8, 16, 32 and 64 bits.

What happens when the number does not fit?

It wraps, and this page shows the wrap instead of refusing. Writing 300 into eight bits stores 44, and writing minus 129 stores 127.

That is not the page being lenient: it is what the hardware does. Showing an error would hide the exact behaviour that causes the bug you are probably here to understand.