Binary, Hexadecimal, and Two's Complement Representation
How unsigned and signed integers are encoded in bits, why hexadecimal maps 1-to-4 with binary, and how two's complement unifies addition and subtraction.
1. Bits, Bytes, and Positional Notation (Base 2)
Digital hardware stores all information as bits (binary digits, 0 or 1). A group of 8 bits is a byte, and a group of 4 bits is a nibble.
In decimal (base 10), each digit position represents a power of 10 (10⁰ = 1, 10¹ = 10, 10² = 100). In binary (base 2), each bit position i (counting from 0 on the right, the Least Significant Bit / LSB, to n − 1 on the left, the Most Significant Bit / MSB) has weight 2ⁱ:
Bit index i: 7 6 5 4 3 2 1 0
Weight 2ⁱ: 128 64 32 16 8 4 2 1
Bits: 0 0 1 0 1 1 0 1
Value: 32 + 8 + 4 + 1 = 45
An n-bit unsigned integer adds up all active weights and can represent values from 0 to 2ⁿ − 1:
- 8-bit unsigned:
0to2⁸ − 1 = 255 - 16-bit unsigned:
0to2¹⁶ − 1 = 65,535 - 32-bit unsigned:
0to2³² − 1 = 4,294,967,295
2. Hexadecimal (Base 16)
Reading 32-bit binary strings is error-prone, so systems programming uses hexadecimal (base 16), written with the prefix 0x and digits 0–9 and A–F (where A = 10, B = 11, C = 12, D = 13, E = 14, F = 15).
Because 16 = 2⁴, one hex digit maps to exactly 4 bits (one nibble). To convert between binary and hex, group the bits by fours from right to left and convert each 4-bit group independently:
Binary (grouped by 4): 1101 0110 1010 1111
Decimal of each group: 13 6 10 15
Hexadecimal: D 6 A F → 0xD6AF
Type 0xFF into the Hexadecimal input in the simulator above: two F digits immediately light up two 4-bit groups of ones (1111 1111 = 255).
3. Signed Integers and Two's Complement
Set the simulator to 8-bit, Unsigned, and enter 251 (1111 1011). Now switch Representation to Signed (Two's Complement). Without changing a single bit, the decimal value becomes −5.
How does two's complement represent negative numbers? Every bit keeps its positive weight 2ⁱ, except the Most Significant Bit (MSB), which has a negative weight −2ⁿ⁻¹ (−128 in 8-bit):
Weight: -128 64 32 16 8 4 2 1
Bits: 1 1 1 1 1 0 1 1
Value: -128 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = -128 + 123 = -5
To negate any two's complement number x: invert all bits (~x) and add 1:
+5 = 0000 0101
~5 = 1111 1010 (bitwise NOT)
+1 = 1111 1011 (-5 in two's complement)
Why every modern CPU uses two's complement:
- There is only a single representation of zero (
0000 0000), unlike older sign-magnitude or ones' complement systems that had both+0and−0. - Unified addition hardware: The CPU's binary adder circuit produces the exact same bit pattern whether the operands are interpreted as signed or unsigned.
- The range of an
n-bit signed integer is−2ⁿ⁻¹to2ⁿ⁻¹ − 1(for 8-bit:−128to+127). Notice−1is always all ones (1111 1111).
4. Bitwise Operations and Integer Overflow
| Operation | Symbol | Behavior & Mathematical Effect |
|---|---|---|
| Bitwise NOT | ~x | Inverts every bit (0 ↔ 1). In two's complement, ~x = −x − 1. |
| Left Shift | x << k | Shifts bits left by k positions, filling with 0 on the right. Multiplies by 2ᵏ (if no bits overflow). |
| Right Shift | x >> k | Shifts bits right by k positions, dividing by 2ᵏ (rounding down). Logical shift fills with 0; arithmetic shift copies the sign bit. |
Overflow: When an arithmetic result exceeds the fixed bit width, the high bits are discarded. In 8-bit unsigned, 255 + 1 wraps around to 0. In 8-bit signed, adding 1 to the maximum positive value 127 (0111 1111) produces 1000 0000, which wraps around to −128.
Exercises
1. Convert decimal 200 to 8-bit binary and hexadecimal by hand, then verify in the simulator.
Show solution
200 = 128 + 64 + 8 → binary 1100 1000. Grouping by 4 bits: 1100 = 12 (C) and 1000 = 8 → 0xC8.2. What decimal value does the 8-bit pattern 1100 1000 (0xC8) represent in Signed (Two's Complement) mode?
Show solution
−128: −128 + 64 + 8 = −56 (or 200 − 256 = −56).Quiz
What is the hexadecimal representation of the 8-bit binary number 0011 1111?
0011 = 3 and 1111 = 15 (F), giving 0x3F (decimal 63).
In 8-bit two's complement (signed), what decimal value does 1000 0001 represent?
The MSB weight is −128 and bit 0 is +1: −128 + 1 = −127. (All ones 1111 1111 is −1.)
What is the range of a signed 8-bit two's complement integer?
An n-bit signed integer ranges from −2ⁿ⁻¹ (−128) to 2ⁿ⁻¹ − 1 (+127).
Why do CPUs use two's complement instead of sign-magnitude for signed integers?
Two's complement unifies signed and unsigned addition/subtraction in hardware and eliminates negative zero.