C7 · Lesson 2 · Binary, hex and two's complement
Writing numbers the way hardware stores them
A digital circuit stores numbers as rows of bits: wires and flip-flops that are each 0 or 1. Eight of them make a byte, which can mean 0 to 255, or −128 to +127, depending only on how you choose to read it. Engineers write bit patterns in hexadecimal, where each digit stands for four bits, so a whole byte fits in two characters. And negative numbers use two's complement, which lets the same adder circuit subtract.
Play
- The dots are the eight bits of a byte, most significant on the left. Slide n to set its value.
- Watch the readouts as you cross 127 to 128. What happens to the value read as a signed number?
- Find a value whose hex is all letters. How many bits does each hex digit cover?
Predict
In 8-bit two's complement, what pattern represents −1?
- Add 1 to it and it rolls over to 00000000 (the carry out of the top bit is dropped), which is exactly what −1 + 1 should give. In general, an 8-bit pattern with value n means if its top bit is set. 10000000 is −128, the most negative; 01111111 is +127, the most positive.
Name it
Number formats
Binary. Bit k is worth : . An n-bit unsigned number spans 0 to .
Hexadecimal. Base 16, digits 0 to 9 then A to F (10 to 15). Each hex digit is exactly four bits, so converting is just grouping:
It's written 0xD6 in C and Verilog's 8'hD6.
Two's complement. To represent in n bits, store . Equivalently, invert every bit of m and add 1. The top bit then acts as a sign bit, worth :
- range: to (−128 to 127 for a byte);
- the pattern for plus the pattern for m gives , which overflows to 0: addition just works;
- subtraction is adding the two's complement, so one adder circuit does both.
Overflow. If two numbers of the same sign add to a result of the other sign, the true answer didn't fit: 100 + 100 in signed 8 bits gives −56.
Sign extension. To widen a signed number, copy its top bit into the new bits: −3 is 1101 in 4 bits and 1111 1101 in 8.
Worked example
What does the 8-bit pattern 0xB6 mean, unsigned and in two's complement?
Show the solution
- Unsigned value.
- Two's-complement value. Its top bit is 1, so subtract 256:
Answer 182 unsigned, and −74 in two's complement.
Worked example
Write −45 as an 8-bit two's-complement number, in binary and hex.
Show the solution
- Write 45 in binary.
- Invert every bit.
- Add 1.
- Check. 0xD3 is 211 unsigned, and
Answer −45 is 1101 0011 in binary, 0xD3 in hex.
Worked example
Add 0x64 and 0x50 as 8-bit two's-complement numbers. What's the result, and has it overflowed?
Show the solution
- Convert to decimal. 0x64 = 100 and 0x50 = 80.
- Add. which fits in 8 bits with no carry out.
- Read it as signed. Its top bit is 1, so as a signed number it reads
- Spot the overflow. Two positive numbers gave a negative result: signed overflow, since +180 is beyond the 8-bit range of −128 to +127.
- The rule. Overflow happens exactly when two operands of the same sign give a result of the other sign; the carry out of the top bit tells you only about unsigned overflow.
Answer the result is 0xB4, which reads as −76 signed: the addition has overflowed.
Check
Write 92 as an 8-bit binary number.
Check
What is the binary number 01011100 in decimal?
Check
Write 102 in hexadecimal.
Check
Write −23 in 8-bit two's complement.
Check
The 8-bit pattern 10101101 is read as a two's-complement signed number. What value is it?
Check
Two positive 8-bit two's-complement numbers, 110 and 30, are added.
(a)
What signed value does the 8-bit result read as?
(b)
What does the same 8-bit pattern read as unsigned?
Check
Numbers are stored in n bits.
Step 1
What's the largest unsigned value?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
The largest two's-complement value?
Step 3
The most negative?
Explain why
Why does two's complement let the same adder circuit handle both positive and negative numbers?
In two's complement, −m is stored as . An n-bit adder naturally works modulo : any carry out of the top bit is simply lost. So adding the pattern for −m to a number x gives , which modulo is exactly . The adder doesn't need to know whether the numbers are signed; the arithmetic comes out right as long as the result fits in the range.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.