LogicGates.org Open the simulatorSimulator

Binary converter

Decimal, binary, hex and octal at a fixed width, with the two's complement and BCD forms alongside. The width is the point: a number only means something once you say how many bits are holding it.

Click any bit to flip it. Bit 7 is the most significant, and the sign bit when the value is read as signed.

Binary 1100 1010
Decimal · as entered 202
Hex CA
Octal 312
Signed -54 two's complement
Gray code 1010 1111 one bit changes at a time
BCD 0010 0000 0010 one nibble per decimal digit

8 bits hold 0 to 255 unsigned, or -128 to 127 signed.

How 54 is stored as -54

  1. Start with 540011 0110
  2. Invert every bit1100 1001
  3. Add one1100 1010

Why the width comes first

On paper a number can be as long as it likes. In a circuit it cannot: it lives in a fixed row of flip-flops, and that row has no way to grow. Everything awkward about machine arithmetic follows from that one fact.

  • It wraps. An eight bit register holding 255 and asked for one more gives 0, because the ninth bit has nowhere to go. Nothing is lost by accident — the carry out records that it happened — and it is the same wrap a counter relies on to start again at zero.
  • The same pattern means two things. Nothing in the register records whether the top bit is worth +128 or −128. The circuit reading it decides, which is why you have to declare whether an integer is signed.
  • Hex is not another number system. It is four bits written as one character, which is why a byte is always exactly two hex digits and converting is a lookup rather than arithmetic.

Two's complement, and why subtraction is free

To write a negative number, invert every bit and add one. That looks arbitrary until you notice what it buys: a number plus its complement wraps the register to zero. Which means a − b is just a + (−b), and the adder you already built does subtraction with no extra circuit, only a row of inverters and a carry in tied high. The two's complement page works through the conversion, the arithmetic and the overflow rule with every table generated.

The one asymmetry is worth knowing: an n bit register reaches −2n−1 but only +2n−1−1. The patterns split evenly, half with the sign bit clear and half with it set, and zero sits in the clear half, which leaves that half one pattern short for the positive numbers. In eight bits that is −128 to 127, and −(−128) has no answer.

Two different things can go wrong when a sum does not fit, and they are worth keeping apart. Carry out is the bit that falls off the top, and it says the unsigned answer was too big. Overflow says the signed answer was too big, and the circuit spots it when the carry into the top bit differs from the carry out of it — which is exactly the case where adding two positives lands on a negative. The same adder produces both flags, and which one you look at depends on how you decided to read the register.

BCD: paying bits for a simpler display

Binary coded decimal gives every decimal digit its own four bits. It is wasteful — six of the sixteen patterns per digit are never used — but it means each digit drives its own seven segment decoder directly, with nothing having to divide by ten. Clocks and meters take that trade every time.

The six patterns BCD throws away
PatternUnsigned valueAs a digit
1010 10 not a decimal digit
1011 11 not a decimal digit
1100 12 not a decimal digit
1101 13 not a decimal digit
1110 14 not a decimal digit
1111 15 not a decimal digit

Questions

How do I convert decimal to binary?

Repeatedly halve the number and write down the remainders, then read them backwards. Or work down from the largest power of two that fits: for 202 in eight bits that is 128, leaving 74, then 64 leaving 10, then 8 and 2, which gives 11001010. The converter above does it either way round and shows the bits you can click.

What is two's complement, and why invert and add one?

It is how a fixed-width register holds a negative number: invert every bit and add one. The reason is that it makes subtraction free. Adding a number to its two's complement wraps the register to zero, so the same adder circuit that computes a + b also computes a - b if you feed it the complement of b. No separate subtractor is needed, which is why almost every processor uses it.

Why does the same bit pattern show two different values?

Because a bit pattern has no sign of its own. 11001010 is 202 read as unsigned and -54 read as two's complement, and nothing in the register says which is meant. The width and the interpretation are decisions made by the circuit reading it, which is exactly why a language makes you declare whether an integer is signed.

What is BCD and why would anyone waste bits like that?

Binary coded decimal gives each decimal digit its own four bits, so 42 is 0100 0010 rather than 101010. It wastes six of the sixteen patterns per digit, but each digit can drive its own seven segment decoder directly, with no division by ten anywhere. That is why clocks, meters and calculator displays use it.

Why is hex used instead of binary?

Because one hex digit is exactly four bits, so the two line up perfectly and converting is a lookup rather than arithmetic. A byte is always two hex digits. Octal does the same job for three bits, which is why it survives in Unix file permissions, where the bits come in threes.