# TAOCP 7.1.3 Bitwise Tricks and Techniques (part 1)

> Study guide to TAOCP 7.1.3 Bitwise Tricks and Techniques (part 1). Part 1 covers the first 20 pages of 7.1.3 (book pages 133 to 153): bitwise operations on…

Updated October 20, 2026.

## Summary {#summary}

TAOCP 7.1.3 Bitwise Tricks and Techniques (part 1) covers the first 20 pages of 7.1.3 (book pages 133 to 153): bitwise operations on 2-adic integers and their algebra (with a detour into nim and Conway's nim field), packing and unpacking fields, and more. Budget about 8 hours; do exercises 1, 20 and 25.

Part 1 covers the first 20 pages of 7.1.3 (book pages 133 to 153): bitwise operations on 2-adic integers and their algebra (with a detour into nim and Conway's nim field), packing and unpacking fields, big-endian versus little-endian, isolating and counting the rightmost and leftmost bits (the ruler function and binary logarithm), sideways addition (popcount), bit reversal and swapping, general bit permutations and the permutation networks behind them, scattered bit fields, and operating on several bytes at once in one word.

## Key facts {#key-facts}

- Week 123 of 145 on the cracked.guide path, in [TAOCP Vol 4A: Combinatorial Algorithms, Part 1](https://cracked.guide/taocp/volume-4a).
- Time budget: about 8 hours.
- 218 exercises in the section, rated 10 to 47; the guide picks 8.
- Picked exercises with Knuth's triangle for recommended: 1, 20, 25, 31, 39, 44, 87 and 35.
- Before you start: 7.1.1 Boolean Basics and two's complement.
- Terms it teaches: [MMIX](https://cracked.guide/glossary/mmix), [SWAR / broadword](https://cracked.guide/glossary/swar).

## Week 123 goals {#objectives}

By the end of week 123 you can:

- Use x & −x and related tricks to isolate and remove rightmost bits.
- Compute the ruler function, binary logarithm and sideways sum of a word.
- Implement the zero-byte test that checks eight bytes at once.

## This week's steps {#steps}

- Read §7.1.3 Bitwise Tricks and Techniques (part 1): Keep a file of tricks and test every one in C with 64-bit integers.
- Work the exercises (8 picked): Start with Ex 1, Ex 20, Ex 25; log what you solve below.
- Write down one thing you'd explain to someone: A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.

## Exercises {#exercises}

Section 7.1.3 has one exercise set of 218 exercises, rated 10 to 47, and Knuth marks 74 of them with the triangle for recommended. This guide splits at the start of broadword computing: this week covers exercises 1 to 106 (106 exercises rated 10 to 47, 34 recommended): 2-adic algebra and nim 1 to 19, packing and Gosper's hack 20 to 30, rightmost and leftmost bits 31 to 42, sideways addition, reversal and swaps 43 to 61, permutation networks and scattered fields 62 to 86, and bytewise tricks 87 to 106. Test every answer in code with 64-bit integers.

Worth your time:

- 1 (rated 15, recommended): Work out what three XOR and mask steps do. Short, and the core of the classic swap and field-merge tricks.
- 20 (rated 21, recommended): Explain Gosper's hack for the next number with the same popcount. Lets you loop over all k-subsets of a word.
- 25 (rated 15, recommended): The bookworm puzzle. A quick lesson in which end of a word is the start.
- 31 (rated 20, recommended): Find the flaw in a plausible average-case argument for a bit-at-a-time loop.
- 39 (rated 20, recommended): Remove the leftmost run of 1 bits, the mirror image of a trick from the text. Harder than the rightmost version.
- 44 (rated 23, recommended): Compute the weighted bit sum (each bit times its position) quickly with masks.
- 87 (rated 20, recommended): Uppercase all eight ASCII letters in a word with three bitwise operations. A real SWAR trick.
- 35 (rated M26, recommended): Compute the non-adjacent signed-digit form of an integer with a few bitwise operations. The stretch.

## Why it matters {#why}

This is the most directly useful part of Vol 4 for systems programmers: bitboards, compact sets, hashing, compression and fast parsers all rely on these tricks.

## Before you start {#before}

- 7.1.1 Boolean Basics
- Two's complement

## How to approach it {#approach}

Keep a file of tricks and test every one in C with 64-bit integers. Compare with what your compiler emits for popcount and count-trailing-zeros. Vol 4 uses MMIX (Knuth's 64-bit RISC machine) instead of MIX where it shows machine code, and much of it is written in plain algorithm steps. You will want MMIX basics (from Vol 1 Fascicle 1 or the MMIX page) for 7.1.3 especially. Code the zero-byte SWAR trick (the one fast strlen uses) this week; it belongs to this half.

## Common traps {#traps}

- Undefined behavior in C when shifting by 64 or shifting signed negative values.
- Forgetting that Knuth's identities are stated for infinite-precision 2-adic integers. Check each one at your actual word size before trusting it.

## Links {#links}

- [Knuth's TAOCP page (editions, errata, MIX/MMIX)](https://www-cs-faculty.stanford.edu/~knuth/taocp.html)
- [Vol 4A errata (plain text, from Knuth)](https://www-cs-faculty.stanford.edu/~knuth/err4a.textxt)
- [Knuth's MMIX page](https://www-cs-faculty.stanford.edu/~knuth/mmix.html)
- [Wikipedia: Bit manipulation](https://en.wikipedia.org/wiki/Bit_manipulation)
- [Wikipedia: Hamming weight](https://en.wikipedia.org/wiki/Hamming_weight)
- [Bit Twiddling Hacks (Sean Anderson, Stanford)](https://graphics.stanford.edu/~seander/bithacks.html)
- [Hacker News: Bit Twiddling Hacks](https://news.ycombinator.com/item?id=25300310)

---

Source: https://cracked.guide/guide/knuth-w105-0
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
