#separator:tab
#html:true
#notetype:Basic
#tags column:3
#deck:cracked.guide::TAOCP Vol 4A::7.1.3 Bitwise Tricks and Techniques (part 1)
#columns:Front	Back	Tags
TAOCP §7.1.3 Bitwise Tricks and Techniques (part 1): what does it cover?	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.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::about
TAOCP §7.1.3 (part 1): why does it matter?	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.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::why
MMIX: what is it?	MMIX is a 64-bit RISC computer designed by Donald Knuth to replace MIX in The Art of Computer Programming.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::definition
MMIX: Should I learn MIX or MMIX first?	For Volumes 1 to 3 as printed today, MIX, because that is what the programs use. Pick up MMIX when you reach Volume 4A.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::definition
MMIX: Is there an MMIX simulator?	Yes. Knuth's own MMIXware package includes an assembler and simulator, and there are browser-based ones made by others.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::definition
SWAR (broadword computing): what is it?	SWAR, SIMD within a register, is the technique of using ordinary integer instructions on a full machine word to operate on many small packed fields at once; Knuth calls it broadword computing.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::definition
SWAR (broadword computing): Is SWAR still useful with real SIMD instructions?	Yes. It needs no special instructions, works in any language with 64-bit integers, and the same ideas apply inside SIMD lanes.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::definition
TAOCP §7.1.3 (part 1), a common trap: “Forgetting that Knuth's identities are stated for infinite-precision 2-adic integers.” What's the catch?	Check each one at your actual word size before trusting it.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::trap
TAOCP §7.1.3 (part 1): what traps should you watch for?	<ul><li>Undefined behavior in C when shifting by 64 or shifting signed negative values.</li></ul>	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::traps
TAOCP §7.1.3 (part 1), exercise 1 (rated 15, recommended): what is it asking?	Work out what three XOR and mask steps do. Short, and the core of the classic swap and field-merge tricks.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::exercise
TAOCP §7.1.3 (part 1), exercise 20 (rated 21, recommended): what is it asking?	Explain Gosper's hack for the next number with the same popcount. Lets you loop over all k-subsets of a word.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::exercise
TAOCP §7.1.3 (part 1), exercise 25 (rated 15, recommended): what is it asking?	The bookworm puzzle. A quick lesson in which end of a word is the start.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::exercise
TAOCP §7.1.3 (part 1), exercise 31 (rated 20, recommended): what is it asking?	Find the flaw in a plausible average-case argument for a bit-at-a-time loop.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::exercise
TAOCP §7.1.3 (part 1), exercise 39 (rated 20, recommended): what is it asking?	Remove the leftmost run of 1 bits, the mirror image of a trick from the text. Harder than the rightmost version.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::exercise
TAOCP §7.1.3 (part 1), exercise 44 (rated 23, recommended): what is it asking?	Compute the weighted bit sum (each bit times its position) quickly with masks.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::exercise
TAOCP §7.1.3 (part 1), exercise 87 (rated 20, recommended): what is it asking?	Uppercase all eight ASCII letters in a word with three bitwise operations. A real SWAR trick.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::exercise
TAOCP §7.1.3 (part 1), exercise 35 (rated M26, recommended): what is it asking?	Compute the non-adjacent signed-digit form of an integer with a few bitwise operations. The stretch.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::exercise
TAOCP §7.1.3 (part 1): how many exercises, and how are they rated?	218 exercises, rated 10 to 47.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::exercises
TAOCP §7.1.3 (part 1): what should you be comfortable with first?	<ul><li>7.1.1 Boolean Basics</li><li>Two's complement</li></ul>	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::prereqs
TAOCP §7.1.3 (part 1): how should you work through 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.	cracked.guide cracked.guide::phase::taocp-4a cracked.guide::section::7.1.3 cracked.guide::kind::approach
