Auto boutiques-francophones Simple and secure cloud storage giftguide Kitchen Kindle Black Friday Deals Week in Music SGG Tools
Have one to sell?
Flip to back Flip to front
Listen Playing... Paused   You're listening to a sample of the Audible audio edition.
Learn more
See all 2 images

Hacker's Delight Hardcover – Jul 17 2002

5 customer reviews

See all formats and editions Hide other formats and editions
Amazon Price
New from Used from
"Please retry"
CDN$ 89.85 CDN$ 35.00

There is a newer edition of this item:

Black Friday Deals Week in Books

No Kindle device required. Download one of the Free Kindle apps to start reading Kindle books on your smartphone, tablet, and computer.

  • Apple
  • Android
  • Windows Phone
  • Android

To get the free app, enter your e-mail address or mobile phone number.

Product Details

  • Hardcover: 320 pages
  • Publisher: Addison-Wesley Professional; 1 edition (July 17 2002)
  • Language: English
  • ISBN-10: 0201914654
  • ISBN-13: 978-0201914658
  • Product Dimensions: 16.5 x 2.1 x 23.8 cm
  • Shipping Weight: 599 g
  • Average Customer Review: 5.0 out of 5 stars  See all reviews (5 customer reviews)
  • Amazon Bestsellers Rank: #371,436 in Books (See Top 100 in Books)
  •  Would you like to update product info, give feedback on images, or tell us about a lower price?

  • See Complete Table of Contents

Product Description

From the Inside Flap

Caveat Emptor: The cost of software maintenance
increases with the square of the programmer's creativity.

First Law of Programmer Creativity, Robert D. Bliss, 1992

This is a collection of small programming tricks that I have come across over many years. Most of them will work only on computers that represent integers in two's-complement form. Although a 32-bit machine is assumed when the register length is relevant, most of the tricks are easily adapted to machines with other register sizes.

This book does not deal with large tricks such as sophisticated sorting and compiler optimization techniques. Rather, it deals with small tricks that usually involve individual computer words or instructions, such as counting the number of 1-bits in a word. Such tricks often use a mixture of arithmetic and logical instructions.

It is assumed throughout that integer overflow interrupts have been masked off, so they cannot occur. C, Fortran, and even Java programs run in this environment, but Pascal and ADA users beware!

The presentation is informal. Proofs are given only when the algorithm is not obvious, and sometimes not even then. The methods use computer arithmetic, "floor" functions, mixtures of arithmetic and logical operations, and so on. Proofs in this domain are often difficult and awkward to express.

To reduce typographical errors and oversights, many of the algorithms have been executed. This is why they are given in a real programming language, even though, like every computer language, it has some ugly features. C is used for the high-level language because it is widely known, it allows the straightforward mixture of integer and bit-string operations, and C compilers that produce high-quality object code are available.

Occasionally, machine language is used. It employs a three-address format, mainly for ease of readability. The assembly language used is that of a fictitious machine that is representative of today's RISC computers.

Branch-free code is favored. This is because on many computers, branches slow down instruction fetching and inhibit executing instructions in parallel. Another problem with branches is that they may inhibit compiler optimizations such as instruction scheduling, commoning, and register allocation. That is, the compiler may be more effective at these optimizations with a program that consists of a few large basic blocks rather than many small ones.

The code sequences also tend to favor small immediate values, comparisons to zero (rather than to some other number), and instruction-level parallelism. Although much of the code would become more concise by using table lookups (from memory), this is not often mentioned. This is because loads are becoming more expensive relative to arithmetic instructions, and the table lookup methods are often not very interesting (although they are often practical). But there are exceptional cases.

Finally, I should mention that the term "hacker" in the title is meant in the original sense of an aficionado of computers--someone who enjoys making computers do new things, or do old things in a new and clever way. The hacker is usually quite good at his craft, but may very well not be a professional computer programmer or designer. The hacker's work may be useful or may be just a game. As an example of the latter, more than one determined hacker has written a program which, when executed, writes out an exact copy of itself1. This is the sense in which we use the term "hacker." If you're looking for tips on how to break into someone else's computer, you won't find them here.

H. S. Warren, Jr.
Yorktown, New York
February 2002

1. The shortest such program written in C, known to the present author, is by Vlad Taeerov and Rashit Fakhreyev and is 64 characters in length: main(a){printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);}


From the Back Cover

"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.


See all Product Description

Customer Reviews

5.0 out of 5 stars
5 star
4 star
3 star
2 star
1 star
See all 5 customer reviews
Share your thoughts with other customers

Most helpful customer reviews

5 of 5 people found the following review helpful By A Customer on Aug. 18 2002
Format: Hardcover
Early drafts of Dr Warren's book have circulated for several years samizdat style among a group of hardware, compiler and OS people at a large computer research lab. One copy in particular always sits about three feet from me. If the building were to catch on fire, you might very well hear shouts of "who's taking Hacker's Delight?"
How do you determine, using the smallest number of instructions, if a word contains at least one zero byte? How do you transpose a bit matrix? Divide by 5? Count the number of ones in a word? Permute bits? Maybe you're smart enough to already know. Or perhaps you know someone else who does. For the rest of us there's Hacker's Delight.
Some years back, in the course of building a large machine, we made a mistake that resulted in some very expensive rework. Just one particular paragraph in this book would have saved us an amount of money best not admitted in print. If you have Knuth on your shelf then there's a good chance that you'll want Hacker's Delight right next to it.
And just in case life is getting too serious, there are some entertaining chapters on prime numbers and Hilbert curves, written so compellingly that you can't stop reading until the end.
Highly recommended. If this book relates to the kind of work you do, then don't leave home without it.
Was this review helpful to you? Yes No Sending feedback...
Thank you for your feedback.
Sorry, we failed to record your vote. Please try again.
Report abuse
7 of 8 people found the following review helpful By Mike Blaszczak on Oct. 22 2002
Format: Hardcover
I feel compelled to point out that this book is _not_ a few things: It's not a book that teaches you how to break into computers, or crack codes. It's also not the kind of book that teaches you how to do something which you don't know how to do.
This book is a collection of tricks that show the reader better ways to do things they already know how to do. And it's also a book that can give the reader insight into different approaches and mechanisms for solving problems.
Computer programmers translate their ideas and requirements into any of several computer languages. Those expressions are limited by the language the programmer is using, and maybe even the machine the programmer is targeting. But there is a wide continum of expressions that result in the same -- hopefully correct -- results. Choosing the most efficient, and most elegant, expression to some is "real" hacking.
This book is for real hackers. It's a great collection of tricks for performing usually simple operations in an elegant way. What's elegant? Well, elegant is efficeint. If there's a side-effect of an elegant operation, it turns out that side-effect is probably useful and not simply discarded.
This book catalogs insights into concrete binary math, shortcuts derived from different boolean operators, and even approaches some interesting numerical analysis problems.
If you already know how to write software, and you already know you want to find faster or more efficient ways to check for overflows on integers, divide nubmers, count bits, search for binary patterns, or do other twiddling, then this book is for you.
If the application of such techniques doesn't seem important to you, then this book probably isn't going to be of interest to you.
Was this review helpful to you? Yes No Sending feedback...
Thank you for your feedback.
Sorry, we failed to record your vote. Please try again.
Report abuse
1 of 1 people found the following review helpful By Dmitry Vostokov on June 21 2004
Format: Hardcover
I've just read only section 2-1 (Manipulating Rightmst Bits) and I got much better understanding of binary arithmetic than I had before. As a collection of different computer arithmetic techniques the book is brilliant, but I think the book would have been even better if the auther had included derivations for formulas.
Was this review helpful to you? Yes No Sending feedback...
Thank you for your feedback.
Sorry, we failed to record your vote. Please try again.
Report abuse
By A Customer on Feb. 11 2003
Format: Hardcover
If you love the nuts and bolts of logical operations in computer programming, do I have a book for you! This book does a great job of describing in reasonable detail logical operators and what you can do with them ranging from very basic "what is it" to reasonably advanced applications such as wierd base -2 math, division, pattern matching, etc. I found this book to be a great reference and refresher clearly layed out and easy to read. I wish my software engineer co-workers would read this book. I'm tired of seeing ugly code (a while loop with a mod operator to align data pointers on, say, 8 byte boundaries).
Was this review helpful to you? Yes No Sending feedback...
Thank you for your feedback.
Sorry, we failed to record your vote. Please try again.
Report abuse
Format: Hardcover
No where will you find such a detailed study on the inner working of computer arithmetic.
Was this review helpful to you? Yes No Sending feedback...
Thank you for your feedback.
Sorry, we failed to record your vote. Please try again.
Report abuse