Below is the C code for most of the programs that appear in HD, with test drivers.

To save this code on your machine using Netscape (4.77 - 7.0), click on a line below and then click File - Save As... (or Save Page As ...). Then set the directory and file name and click Save.
Using IE (5.50), right-click a line below, click Save Target As ..., set directory and file name and set "Save as type:" to All Files, and then click Save.

Fig. 2-1. Next higher number with same number of 1-bits.
Fig. 2-2. Determination of overflow of unsigned multiplication.
Fig. 3-1. Greatest power of 2 less than or equal to x, branch free.
Fig. 3-3. Least power of 2 greater than or equal to x.
Fig. 4-1. Propagating unsigned bounds through addition operations.
Fig. 4-1. Propagating unsigned bounds through subtraction operations.
Fig. 4-2. Propagating signed bounds through an addition operation.
Fig. 4-3. Minimum value of x | y with bounds on x and y.
Fig. 4-4. Maximum value of x | y with bounds on x and y.
Fig. 4-5. Minimum value of x & y with bounds on x and y.
Fig. 4-6. Maximum value of x & y with bounds on x and y.
P. 62. Minimum value of x ^ y with bounds on x and y.
P. 62. Maximum value of x ^ y with bounds on x and y.
P. 62. Minimum value of x | y with bounds on x and y (signed).
P. 62. Maximum value of x | y with bounds on x and y (signed).
P. 66-72. Population count algorithms.
Fig. 5-5. Counting 1-bits in an array. Superior methods are given in the new code section below.
P. 74. Indexing a moderately sparse array.
P. 75-76. Various parity algorithms.
P. 77-82. Number of leading zeros algorithms.
P. 84-86. Number of trailing zeros algorithms.
Fig. 5-17. Gosper's loop-detection algorithm.
P. 92-96. Find first 0-byte or first value in a given range.
---. Find rightmost 0-byte.
P. 97-98. Find first string of 1-bits of a given length.
P. 101-104. Reversing bits. New code here.
P. 105-106. Incrementing a reversed integer.
P. 107-108. Shuffling bits.
P. 109-111. Transposing an 8x8 bit matrix (revised).
P. 113-116. Transposing a 32x32 bit matrix.
P. 117-119. Compress, or generalized extract.
P. 122. Compress left.
P. 124. Permuting by sheep and goats operation, little-endian.
P. 124. Permuting by sheep and goats operation, big-endian.
Fig. 8-1. Multiword integer multiplication, signed.
P. 129. Multiword integer multiplication, unsigned.
Fig. 8-2. Multiply high signed.
P. 132. Multiply high unsigned.
---. Multiply, 32x32 ==> 64, signed.
---. Multiply, 32x32 ==> 64, unsigned.
---. Multiply, 64x64 ==> 128, unsigned.
Fig. 9-1. Multiword integer division, unsigned.
Fig. 9-2. Divide long unsigned, shift-and-subtract algorithm.
Fig. 9-3. Divide long unsigned, using fullword division instruction.
Fig. 9-4. Divide long signed, using divide long unsigned.
Fig. 10-1. Computing the magic number for signed division.
Figs. 10-2 and 10-3. Computing the magic number for unsigned division.
Figs. 10-4 and 10-5. Multiplicative inverse modulo 232.
Figs. 11-1 to 11-4. Integer square root.
Fig. 11-5. Integer cube root, hardware algorithm.
Fig. 11-6. Computing xn by binary decomposition of n.
Figs. 11-7 to 11-13. Integer log base 10.
Fig. 12-1. Division in base −2.
Fig. 14-3. Hilbert curve generator.
Fig. 14-4. Driver program for Hilbert curve generator.
Fig. 14-5. Program for computing (x, y) from s.
Fig. 14-6. Lam and Shapiro method for computing (x, y) from s.
P. 249. Variation of Fig. 14-6 that avoids a branch.
Fig. 14-7. Code to verify the logic circuit of Fig. 14-7. NB error in first and second printings: The title of this figure should be "Logic circuit for computing (x, y) from s."
Fig. 14-8. Parallel prefix method for computing (x, y) from s.
Fig. 14-9. Program for computing s from (x, y).
Fig. 14-10. Lam and Shapiro method for computing s from (x, y).
---. Variation of Fig. 14-10 that avoids a branch.
Fig. 14-11. Program for taking one step on the Hilbert curve.
Fig. 14-12. Code to verify the logic circuit of Fig. 14-12.
---. Right-to-left algorithm for computing s from (x, y).

Below is some new code, not in Hacker's Delight.

Convert base −1 + i integer to a + bi, a and b in decimal (Python).
Cyclic Redundancy Check (CRC-32).
Inverse of the compress (right) function.
SEC-DED Hamming code for 32 information bits.
64/64 ==> 64 division, unsigned and signed.
Test for overflow of signed "long division."
Signed division by constants.
Unsigned division by constants.
Exact division method of division by constants.
Find longest string of 1's in a word.
Find shortest string of 1's in a word (optionally, at least as long as a given integer n).
Signed remainder of division by constants.
Unsigned remainder of division by constants.
Determines which word has the larger population count.
Computes pop(x) − pop(y).
Counting 1-bits in an array.
Reciprocal sqrt of an IEEE float.

Home