Binary, hex and two's complement formulas
To convert binary to decimal, multiply each bit by its place value (128, 64, 32, 16, 8, 4, 2, 1) and add: b₇ × 128 + b₆ × 64 + … + b₀ × 1. A two-digit hex number is h₁ × 16 + h₀, and each hex digit is exactly 4 bits. In 8-bit two's complement, a leading 1 means subtract 256: U − 256.
The formulas
| Formula | What it gives | Symbols |
|---|---|---|
b₇ × 128 + b₆ × 64 + … + b₁ × 2 + b₀ × 1 | Binary to decimal | b₇ … b₀ = the bits of an 8-bit number from left to right (0 or 1); 0 bits add nothing, so only the places that hold a 1 count. The sum is the decimal value |
h₁ × 16 + h₀ = N | A two-digit hex number in decimal | h₁, h₀ = the two hex digits as numbers (A = 10 … F = 15), h₁ in the 16s place; N = the decimal value |
1 + 0 = 1, 1 + 1 = 10, 1 + 1 + 1 = 11 | The rules for adding binary numbers | Add column by column from the right. 1 + 1 = 10 means write 0 and carry 1; 1 + 1 + 1 = 11 means write 1 and carry 1 |
N − p = N′ | Decimal to binary | N = what is left, p = the biggest place value (128, 64 … 1) that fits, N′ = what is left after it. Repeat until 0; write 1 under each place used and 0 under the others |
N ÷ 16 = h₁ | Decimal to hex | Divide N by 16: the whole-number quotient is the 16s digit h₁ and the remainder is the 1s digit h₀, each written as one hex digit (10 = A … 15 = F) |
1110 = 8 + 4 + 2 = 14 → E; A = 1010 | One hex digit as 4 bits | Each hex digit is exactly 4 bits with place values 8 4 2 1: 1110 = 14 = E, and A = 10 = 8 + 2, so A = 1010. Convert a byte one 4-bit group at a time |
U − 256 = −n | A negative 8-bit two's complement number | U = the number read as ordinary binary (0 to 255). When the top bit is 1, the number is negative: that bit counted 128 but is worth −128, so subtract 256; n = its size |
01010110 + 1 = 01010111 | The size of a negative two's complement number | Flip every bit of the number (here 10101001 becomes 01010110) and add 1: 01010111 = 87, so 10101001 is −87 |
11010101 AND 10110100 = 10010100; 01110010 OR 01001000 = 01111010; 00001111 XOR 11111010 = 11110101 | Bitwise AND, OR and XOR | Apply the gate to each column of bits: AND keeps a 1 only where both bits are 1, OR gives 1 where at least one is 1, XOR gives 1 where the bits differ |
Worked example
This example is made by the same code as the practice questions. Open the working to see every step.
Add the binary numbers 00111111 + 01011001.
Answer: 10011000. Add column by column from the right: where both bits are 1, 1 + 1 = 10 (write 0, carry 1). Check in decimal: 63 + 89 = 152 = 10011000.
Show the working
- Write down what is given: 00111111, 01011001.
- Add column by column from the right: 1 + 0 = 1, 1 + 1 = 10 (write 0, carry 1) and 1 + 1 + 1 = 11 (write 1, carry 1).
- The carries, written over the columns they go into: 11111110.
- Work it out: 00111111 + 01011001 = 10011000.
- Check in decimal: 63 + 89 = 152. Answer: 10011000.
Common mistakes
These are the wrong answers the practice questions offer, and the slip that leads to each.
- Reading the bits in reverse order, or counting the 1s instead of adding their place values, gives the wrong decimal.
- Using 10 instead of 16 for the first hex digit, or swapping the two hex digits, gives the wrong value.
- Adding with OR or XOR instead of carrying (1 + 1 = 10) gives a number that is too small.
- Reading a leading 1 as an ordinary positive number skips the subtract-256 step of two's complement.
Questions
What is the formula for binary to decimal?
b₇ × 128 + b₆ × 64 + … + b₁ × 2 + b₀ × 1. Symbols: b₇ … b₀ = the bits of an 8-bit number from left to right (0 or 1); 0 bits add nothing, so only the places that hold a 1 count. The sum is the decimal value.
What is the formula for a two-digit hex number in decimal?
h₁ × 16 + h₀ = N. Symbols: h₁, h₀ = the two hex digits as numbers (A = 10 … F = 15), h₁ in the 16s place; N = the decimal value.
What is the formula for the rules for adding binary numbers?
1 + 0 = 1, 1 + 1 = 10, 1 + 1 + 1 = 11. Symbols: Add column by column from the right. 1 + 1 = 10 means write 0 and carry 1; 1 + 1 + 1 = 11 means write 1 and carry 1.