Binary Arithmetic: Adding, Subtracting and Multiplying Bits

Every calculation a processor performs comes down to arithmetic on binary digits. The methods are the same column-by-column procedures you use for decimal numbers. The difference is that each column can hold only 0 or 1, so a column spills over at 2 instead of 10. This lesson works through unsigned addition, subtraction and multiplication by hand. It then shows what happens when a result is too large for the fixed number of bits a computer provides.

Adding column by column

Start at the rightmost column, the LSB, and move left. When a column total reaches 2, you write a 0 and pass a 1 to the next column. That 1 is the carry: the value sent into the next column whenever a column's total is 2 or more. Each column obeys


ai+bi+ci=2 ci+1+sia_i + b_i + c_i = 2\,c_{i+1} + s_i


where aia_i and bib_i are the bits in column ii, cic_i is the carry coming in, sis_i is the sum bit you write and ci+1c_{i+1} is the carry going out. Only four column totals are possible:


Column total

Sum bit written

Carry out

0

0

0

1

1

0

2

0

1

3

1

1


Example: to add 10112 and 01102, column 0 gives 1 + 0 = 1. Column 1 gives 1 + 1 = 2, so write 0 and carry 1. Column 2 gives 0 + 1 + 1 = 2, so write 0 and carry 1. Column 3 gives 1 + 0 + 1 = 2, so write 0 and carry 1, which becomes a new leftmost digit. The sum is 100012, and the decimal check 11 + 6 = 17 confirms it.

Subtracting with borrows

Subtraction also runs right to left. When the top bit in a column is smaller than the bit being subtracted plus any borrow already owed, take a borrow from the next column. A borrow is worth 2 in the current column, because the column to the left has twice the place value.


Example: for 11002 − 01012, column 0 is 0 − 1, so borrow: 2 + 0 − 1 = 1. Column 1 is 0 − 0 − 1 for the owed borrow, so borrow again: 2 + 0 − 0 − 1 = 1. Column 2 is 1 − 1 − 1, which is negative, so borrow once more: 2 + 1 − 1 − 1 = 1. Column 3 is 1 − 0 − 1 = 0. The difference is 01112, matching 12 − 5 = 7.



Many processors subtract by adding a complemented value instead of borrowing; that technique belongs with complement representations. By hand, borrowing is the direct method.

Multiplying by shifting and adding

Single-bit products are simple: 1×1=11 \times 1 = 1 and every other product is 0. So each bit of the multiplier contributes either a copy of the multiplicand or nothing. The copy produced by bit kk is shifted left kk places, and shifting left by kk places multiplies by 2k2^k.


A×B=∑k : bk=1A⋅2kA \times B = \sum_{k \,:\, b_k = 1} A \cdot 2^k


Example: for 1012 × 112, bit 0 of the multiplier contributes 1012 and bit 1 contributes 10102. Adding the two partial products gives 11112, which is 5 × 3 = 15. Add partial products two at a time so every column total stays at 3 or less.

Fixed width and overflow

A computer stores numbers in a register with a fixed number of bits nn. An unsigned nn-bit register holds values from 0 to 2n−12^n - 1, so 4 bits cover 0 to 15 and 8 bits cover 0 to 255. Overflow occurs when the true result of an operation falls outside that range. For unsigned addition, the register keeps only the lowest nn bits:


stored value=(A+B) mod 2n\text{stored value} = (A + B) \bmod 2^n


In the earlier addition, 10112 + 01102 = 100012 needs five bits. A 4-bit register keeps 00012, which is 17 − 16 = 1, and the extra 1 leaves as a carry out of the MSB. Unsigned subtraction overflows in the matching way: a borrow is still owed after the leftmost column, because the true result is negative.



What to remember

  • Work right to left; a column total of 2 writes 0 and carries 1, and a total of 3 writes 1 and carries 1.
  • A borrow adds 2 to the current column and owes 1 to the next column on the left.
  • Multiply by adding shifted copies of the multiplicand, one for each 1 in the multiplier.
  • An unsigned nn-bit register keeps the result modulo 2n2^n; a carry out of the leftmost bit signals overflow.
  • Check every result by converting the inputs and the answer to decimal.

Example: Arithmetic in an 8-bit microcontroller register

A small microcontroller stores whole numbers in 8-bit unsigned registers, so each register can hold any value from 0 to 255. Work each part by hand in binary before checking it in decimal.


a) Add 10110110210110110_2 and 01011101201011101_2. Give the full binary sum, the 8 bits the register keeps, and state whether overflow occurs.


Add column by column from bit 0, carrying 1 whenever a column total reaches 2.

  • Bits 0 and 1: 0 + 1 = 1 and 1 + 0 = 1, so write 1 in each with no carry.
  • Bit 2: 1 + 1 = 2, so write ________ and carry 1.
  • Bit 3: 0 + 1 + 1 = 2, so write 0 and carry 1.
  • Bit 4: 1 + 1 + 1 = 3, so write ________ and carry 1.
  • Bits 5, 6 and 7: each column holds one 1 plus the incoming carry, a total of 2, so each writes 0 and carries 1.

The carry out of bit 7 becomes a ninth digit:


101101102+010111012=100010011210110110_2 + 01011101_2 = 100010011_2


In decimal the inputs are ________ and ________, and 182 + 93 = 275 = 256 + 19. The register keeps only the lowest 8 bits, 000100112=1900010011_2 = 19. Because a carry left bit 7, the addition overflows: the true sum of 275 exceeds the register's maximum of 255.


b) Subtract 01101011201101011_2 from 11010010211010010_2 and give the 8-bit difference.


Subtract column by column from bit 0. When a column cannot pay, borrow, which adds 2 to that column and owes 1 to the next column on the left.

  • Bit 0: 0 − 1 is negative, so borrow: 2 + 0 − 1 = 1.
  • Bit 1: 1 − 1 − 1 for the owed borrow is negative, so borrow: 2 + 1 − 1 − 1 = ________.
  • Bit 2: 0 − 0 − 1 is negative, so borrow: 2 + 0 − 0 − 1 = 1.
  • Bit 3: 0 − 1 − 1 is negative, so borrow: 2 + 0 − 1 − 1 = ________.
  • Bit 4: 1 − 0 − 1 = 0, and nothing is owed to bit 5.
  • Bit 5: 0 − 1 is negative, so borrow: 2 + 0 − 1 = 1.
  • Bit 6: 1 − 1 − 1 is negative, so borrow: 2 + 1 − 1 − 1 = 1.
  • Bit 7: 1 − 0 − 1 = 0, with no borrow left over.


110100102−011010112=01100111211010010_2 - 01101011_2 = 01100111_2


Check in decimal: the inputs are ________ and ________, and the result 011001112=64+32+4+2+1=10301100111_2 = 64 + 32 + 4 + 2 + 1 = 103, which equals 210 − 107. No borrow is owed after bit 7, so the difference fits the register.


c) Multiply 110121101_2 by 101121011_2 and decide whether the product fits in one 8-bit register.


Each 1 in the multiplier 101121011_2 contributes a copy of 110121101_2 shifted left by that bit's position; the 0 in bit 2 contributes nothing.

  • Bit 0 contributes 110121101_2.
  • Bit 1 contributes 11010211010_2.
  • Bit 3 contributes 110100021101000_2.

Adding the first two partial products gives ________ in binary, which is 13 + 26 = ________. Now add the third partial product. Bits 0 to 4 never hold two 1s, so they copy down without carries. Bit 5 is 1 + 1 = 2, so write 0 and carry 1; bit 6 is 0 + 1 + 1 = 2, so write 0 and carry 1 into bit 7.


1001112+11010002=100011112100111_2 + 1101000_2 = 10001111_2


Check in decimal: 13 × 11 = 143 = 128 + 15, which matches 10001111210001111_2. The product has exactly 8 bits and 143≤255143 \le 255, so it fits in one register without overflow.

When you add two unsigned binary numbers by hand, suppose one column contains a 1 in each number and also receives an incoming carry of 1. The column total is three, so you write a sum bit of in that column and send a carry of into the next column to the left.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Match each term used in unsigned binary arithmetic with the description of what it means or does when you add, subtract or multiply binary numbers column by column.

Match each term
A.

A 1 sent to the next column on the left when a column's total reaches two or more

B.

A copy of the multiplicand, moved left by the position of a multiplier bit that equals 1

C.

A value of two taken from the next column on the left so that a 0 minus 1 column can be completed

D.

The condition in which the correct unsigned result needs more bits than the register can hold

E.

The single bit written in a column's result position after every complete two has been passed to the left

F.

Moving every bit one place toward the most significant end, which doubles the value when no 1 is lost

Carry

Left shift

Borrow

Overflow

Sum bit

Partial product

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Add the 4-bit unsigned numbers 011120111_2 and 010120101_2 by hand. For each bit position, starting from bit 0 (the rightmost bit), enter the sum bit written in that column and the carry passed to the next column. Use 0 or 1 in every cell.

Bit positionSum bitCarry out
Bit 0
Bit 1
Bit 2
Bit 3

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice