O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hacker's Delight

Book Description

"This is the first book that promises to tell the deep, dark secrets of computer arithmetic, and it delivers in spades. It contains every trick I knew plus many, many more. A godsend for library developers, compiler writers, and lovers of elegant hacks, it deserves a spot on your shelf right next to Knuth."

--Josh Bloch

"When I first saw the title, I figured that the book must be either a cookbook for breaking into computers (unlikely) or some sort of compendium of little programming tricks. It's the latter, but it's thorough, almost encyclopedic, in its coverage."

--Guy Steele

These are the timesaving techniques relished by computer hackers--those devoted and persistent code developers who seek elegant and efficient ways to build better software. The truth is that much of the computer programmer's job involves a healthy mix of arithmetic and logic. In Hacker's Delight, veteran programmer Hank Warren shares the tricks he has collected from his considerable experience in the worlds of application and system programming. Most of these techniques are eminently practical, but a few are included just because they are interesting and unexpected. The resulting work is an irresistible collection that will help even the most seasoned programmers better their craft.

Topics covered include:

  • A broad collection of useful programming tricks

  • Small algorithms for common tasks

  • Power-of-2 boundaries and bounds checking

  • Rearranging bits and bytes

  • Integer division and division by constants

  • Some elementary functions on integers

  • Gray code

  • Hilbert's space-filling curve

  • And even formulas for prime numbers!

  • This book is for anyone who wants to create efficient code. Hacker's Delight will help you learn to program at a higher level--well beyond what is generally taught in schools and training courses--and will advance you substantially further than is possible through ordinary self-study alone.


    Table of Contents

    1. Copyright
    2. Foreword
    3. Preface
    4. Introduction
      1. Notation
      2. Instruction Set and Execution Time Model
    5. Basics
      1. Manipulating Rightmost Bits
      2. Addition Combined with Logical Operations
      3. Inequalities among Logical and Arithmetic Expressions
      4. Absolute Value Function
      5. Sign Extension
      6. Shift Right Signed from Unsigned
      7. Sign Function
      8. Three-Valued Compare Function
      9. Transfer of Sign
      10. Decoding a “Zero Means 2**n” Field
      11. Comparison Predicates
      12. Overflow Detection
      13. Condition Code Result of Add, Subtract, and Multiply
      14. Rotate Shifts
      15. Double-Length Add/Subtract
      16. Double-Length Shifts
      17. Multibyte Add, Subtract, Absolute Value
      18. Doz, Max, Min
      19. Exchanging Registers
      20. Alternating among Two or More Values
    6. Power-of-2 Boundaries
      1. Rounding Up/Down to a Multiple of a Known Power of 2
      2. Rounding Up/Down to the Next Power of 2
      3. Detecting a Power-of-2 Boundary Crossing
    7. Arithmetic Bounds
      1. Checking Bounds of Integers
      2. Propagating Bounds through Add’s and Subtract’s
      3. Propagating Bounds through Logical Operations
    8. Counting Bits
      1. Counting 1-Bits
      2. Parity
      3. Counting Leading 0’s
      4. Counting Trailing 0’s
    9. Searching Words
      1. Find First 0-Byte
      2. Find First String of 1-Bits of a Given Length
    10. Rearranging Bits and Bytes
      1. Reversing Bits and Bytes
      2. Shuffling Bits
      3. Transposing a Bit Matrix
      4. Compress, or Generalized Extract
      5. General Permutations, Sheep and Goats Operation
      6. Rearrangements and Index Transformations
    11. Multiplication
      1. Multiword Multiplication
      2. High-Order Half of 64-Bit Product
      3. High-Order Product Signed from/to Unsigned
      4. Multiplication by Constants
    12. Integer Division
      1. Preliminaries
      2. Multiword Division
      3. Unsigned Short Division from Signed Division
      4. Unsigned Long Division
    13. Integer Division by Constants
      1. Signed Division by a Known Power of 2
      2. Signed Remainder from Division by a Known Power of 2
      3. Signed Division and Remainder by Non-Powers of 2
      4. Signed Division by Divisors ≥ 2
      5. Signed Division by Divisors ≤ -2
      6. Incorporation into a Compiler
      7. Miscellaneous Topics
      8. Unsigned Division
      9. Unsigned Division by Divisors ≥ 1
      10. Incorporation into a Compiler (Unsigned)
      11. Miscellaneous Topics (Unsigned)
      12. Applicability to Modulus and Floor Division
      13. Similar Methods
      14. Sample Magic Numbers
      15. Exact Division by Constants
      16. Test for Zero Remainder after Division by a Constant
    14. Some Elementary Functions
      1. Integer Square Root
      2. Integer Cube Root
      3. Integer Exponentiation
      4. Integer Logarithm
    15. Unusual Bases for Number Systems
      1. Base -2
      2. Base −1 + i
      3. Other Bases
      4. What Is the Most Efficient Base?
    16. Gray Code
      1. Gray Code
      2. Incrementing a Gray-Coded Integer
      3. Negabinary Gray Code
      4. Brief History and Applications
    17. Hilbert’s Curve
      1. A Recursive Algorithm for Generating the Hilbert Curve
      2. Coordinates from Distance along the Hilbert Curve
      3. Distance from Coordinates on the Hilbert Curve
      4. Incrementing the Coordinates on the Hilbert Curve
      5. Non-recursive Generating Algorithms
      6. Other Space-Filling Curves
      7. Applications
    18. Floating-Point
      1. IEEE Format
      2. Comparing Floating-Point Numbers Using Integer Operations
      3. The Distribution of Leading Digits
      4. Table of Miscellaneous Values
    19. Formulas for Primes
      1. Introduction
      2. Willans’s Formulas
      3. Wormell’s Formula
      4. Formulas for Other Difficult Functions
    20. Arithmetic Tables for a 4-Bit Machine
    21. Newton’s Method
    22. Bibliography