Sub topic 8
Karnaugh map layout and Gray code
This chapter assumes you can convert between binary and decimal, read a truth table, and write a function as a sum of minterms. It covers how to draw a Karnaugh map (K-map), why its axes use Gray code, and how to place minterms correctly. Grouping the cells comes in the next chapter.
What a K-map is
A K-map is a truth table rearranged into a grid. It has one cell per input combination, so a function of n variables has 2^n cells. The rearrangement makes logical adjacency show up as physical adjacency. Two cells that are neighbours on the grid differ in exactly one variable. Two product terms that differ in one variable can merge by the identity xA + xA' = x, so a K-map lets you spot those merges by eye.
For this to work, every step from one cell to a horizontal or vertical neighbour must change exactly one input bit. Plain binary counting does not do this, so the axes use Gray code.
Gray code
A Gray code is an ordering of n-bit values in which consecutive values differ in exactly one bit. The version used on K-map axes is the reflected binary Gray code. You build it by listing the (n-1)-bit code, then listing it again in reverse order, and prefixing 0 to the first half and 1 to the second half.
| Position | Binary order (wrong) | Gray order (correct) |
|---|---|---|
| 0 | 00 | 00 |
| 1 | 01 | 01 |
| 2 | 10 | 11 |
| 3 | 11 | 10 |
In binary order, the step from 01 to 10 flips two bits. Cells in those positions would look adjacent but could not be merged. In Gray order every step flips one bit. The last code, 10, also differs from the first, 00, in one bit. So the sequence is a cycle, and the map's edges connect to each other.
You can compute the Gray code of a number i with i ^ (i >> 1).
Layouts for two, three and four variables
The variables are split between the rows and the columns. Each axis is labelled in Gray order.
- 2 variables: rows are A (0, 1), columns are B (0, 1). The grid is 2 by 2.
- 3 variables: rows are A (0, 1), columns are BC (00, 01, 11, 10). The grid is 2 by 4.
- 4 variables: rows are AB (00, 01, 11, 10), columns are CD (00, 01, 11, 10). The grid is 4 by 4.
The minterm number of a cell is the binary number formed by the row label followed by the column label. In the 4-variable map, the cell in row 11, column 01 is 1101, which is minterm 13.
The 4-variable map with minterm numbers:
| AB \ CD | 00 | 01 | 11 | 10 |
|---|---|---|---|---|
| 00 | 0 | 1 | 3 | 2 |
| 01 | 4 | 5 | 7 | 6 |
| 11 | 12 | 13 | 15 | 14 |
| 10 | 8 | 9 | 11 | 10 |
Notice that the numbers are not in reading order. Row 11 comes before row 10, so the third row holds 12 to 15 and the fourth holds 8 to 11. Most layout mistakes come from forgetting this.
Which cells belong to which variable
Each variable is 1 over half of the map, and these halves have a pattern you can learn.
- In the 4-variable map, A = 1 in the bottom two rows (
11and10). - B = 1 in the middle two rows (
01and11). - C = 1 in the right two columns (
11and10). - D = 1 in the middle two columns (
01and11).
This is why a pair of adjacent cells loses one variable. The two cells share all variables except the one that changes between them. Larger blocks lose more variables, which is the subject of the next chapter.
Adjacency includes the edges
Because the Gray sequence is a cycle, the left and right edges are neighbours, and the top and bottom edges are neighbours. In the 4-variable map:
- Cell 0 (
0000) is adjacent to 1, 2 (right edge wrap), 4, and 8 (bottom edge wrap). - The four corners 0, 2, 8, and 10 are all mutually close, since they form a block once you wrap in both directions.
A rule that works for any layout: the neighbours of minterm m in an n-variable function are the numbers m XOR 2^k for k = 0 to n-1. Every cell has exactly n neighbours. Diagonal cells are never adjacent.
Generating and checking a layout
This program builds the layout for any number of variables from 2 to 6 using the Gray formula. It then checks that every horizontal and vertical neighbour, including wraparound, differs by exactly one bit.
def gray(n):
return [i ^ (i >> 1) for i in range(1 << n)]
def build(n):
r = n // 2
c = n - r
rows = [format(g, f'0{r}b') for g in gray(r)]
cols = [format(g, f'0{c}b') for g in gray(c)]
grid = [[int(rb + cb, 2) for cb in cols] for rb in rows]
return rows, cols, grid
def show(n):
rows, cols, grid = build(n)
print(' ' * (len(rows[0]) + 1) + ' '.join(f'{x:>2}' for x in cols))
for rb, line in zip(rows, grid):
print(rb + ' ' + ' '.join(f'{m:2d}' for m in line))
def one_bit(a, b):
return bin(a ^ b).count('1') == 1
def check(n):
_, _, g = build(n)
R, C = len(g), len(g[0])
return all(one_bit(g[i][j], g[i][(j + 1) % C]) and
one_bit(g[i][j], g[(i + 1) % R][j])
for i in range(R) for j in range(C))
show(4)
print(all(check(n) for n in range(2, 7)))The output is:
00 01 11 10
00 0 1 3 2
01 4 5 7 6
11 12 13 15 14
10 8 9 11 10
TrueFilling in a map from a function
To fill a map, put a 1 in each cell whose minterm number is in the function, and a 0 in every other cell. For F(A,B,C) = Σm(0,1,2,5,6,7), the 3-variable map is:
| A \ BC | 00 | 01 | 11 | 10 |
|---|---|---|---|---|
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 1 | 1 |
The zeros are in cells 3 and 4, which are the maxterms. The same grid serves for both forms. SOP reads from the 1s, and POS reads from the 0s.
If the function is given as an expression instead of minterms, fill it product term by product term. A term with all n variables marks one cell. A term with fewer variables marks every cell where those literals hold. For example, A'B in a 3-variable map marks cells 2 and 3 (A = 0, B = 1). Wait, check this against the layout: A = 0 and B = 1 gives 01x, which is minterms 2 and 3, in row 0, columns 11 and 10. These are neighbours, which is what you would expect from a term with one missing variable.
Common errors
- Writing a Gray axis but copying minterm numbers in plain reading order.
- Treating the map as a flat rectangle and ignoring the wraparound.
- Swapping the row and column variables halfway through a problem. Mark which variables are on which axis before you start.
- Reading a cell
1101as minterm 11 because you misread rows 11 and 10. Always take the row label, then the column label.
Exercise
In a 4-variable K-map with rows AB and columns CD, both in Gray order, which minterms are adjacent to minterm 6? Name the cell positions you used.
Show answer
Minterm 6 is 0110. Its neighbours are 6 XOR 1 = 7, 6 XOR 2 = 4, 6 XOR 4 = 2, and 6 XOR 8 = 14. On the grid, 6 is in row 01, column 10. Its left neighbour is 7. Its right neighbour wraps around to 4. The row above is 2, and the row below is 14.
Exercise
A 3-variable map has rows A and columns BC in Gray order. Which cell holds minterm 6, and which two minterms are in the same column as minterm 1?
Show answer
Minterm 6 is 110, so it is in row A = 1, column BC = 10, which is the last column of the second row. Minterm 1 is 001, in column 01. The other cell in that column is 101, which is minterm 5.