EngineerPlay

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

FormulaWhat it givesSymbols
b₇ × 128 + b₆ × 64 + … + b₁ × 2 + b₀ × 1Binary to decimalb₇ … 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₀ = NA two-digit hex number in decimalh₁, 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 = 11The rules for adding binary numbersAdd 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 binaryN = 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 hexDivide 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 = 1010One hex digit as 4 bitsEach 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 = −nA negative 8-bit two's complement numberU = 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 = 01010111The size of a negative two's complement numberFlip 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 = 11110101Bitwise AND, OR and XORApply 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.

Binary 00111111Labels shown, in drawing order: 128, 0, 64, 0, 32, 1, 16, 1, 8, 1, 4, 1, 2, 1, + 01011001.128064032116181412111+ 01011001
Show the working
  1. Write down what is given: 00111111, 01011001.
  2. 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).
  3. The carries, written over the columns they go into: 11111110.
  4. Work it out: 00111111 + 01011001 = 10011000.
  5. 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.

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.