TAOCP Vol 4A: Combinatorial Algorithms, Part 1 · Week 123
TAOCP 7.1.3 Bitwise Tricks and Techniques (part 1)
Updated
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.
This week's steps
3 stepsRead §7.1.3 Bitwise Tricks and Techniques (part 1)
Keep a file of tricks and test every one in C with 64-bit integers.
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.
Sign in to track this: tick each step off and it counts toward your week.
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
Exercise 115▶ Recommended
Work out what three XOR and mask steps do. Short, and the core of the classic swap and field-merge tricks.
Exercise 2021▶ Recommended
Explain Gosper's hack for the next number with the same popcount. Lets you loop over all k-subsets of a word.
Exercise 2515▶ Recommended
The bookworm puzzle. A quick lesson in which end of a word is the start.
Exercise 3120▶ Recommended
Find the flaw in a plausible average-case argument for a bit-at-a-time loop.
Exercise 35M26▶ Recommended
Compute the non-adjacent signed-digit form of an integer with a few bitwise operations. The stretch.
Exercise 3920▶ Recommended
Remove the leftmost run of 1 bits, the mirror image of a trick from the text. Harder than the rightmost version.
Exercise 4423▶ Recommended
Compute the weighted bit sum (each bit times its position) quickly with masks.
Exercise 8720▶ Recommended
Uppercase all eight ASCII letters in a word with three bitwise operations. A real SWAR trick.
Your log
Sign in to log the exercises you work: number, rating, how far you got.
Your notes
Only youSign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.
Discussion
0 postsQuestions about the reading go here; each exercise has its own thread below. Hints over answers.
No posts yet. Ask a question or share a tip.
Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise threads
One thread per exercise. Open one to ask for a nudge or check your thinking.
Exercise 115No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 2021No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 2515No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3120No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 35M26No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3920No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 4423No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 8720No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Reference
Why it matters
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
- 7.1.1 Boolean Basics
- Two's complement
How to approach it
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
- 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.