Copyright
Many of the designations used bymanufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and Addison-Wesleywas aware of a trademark claim, the designations have been printed with initialcapital letters or in all capitals.
The author and publisher have taken care inthe preparation of this book, but make no expressed or implied warranty of anykind and assume no responsibility for errors or omissions. No liability isassumed for incidental or consequential damages in connection with or arisingout of the use of the information or programs contained herein.
The publisher offers discounts on this bookwhen ordered in quantity for special sales. For more information, pleasecontact:
U.S. Corporate and Government Sales
(800) 382-3149
For sales outside of the U.S., pleasecontact:
International Sales
(317) 581-3793
Visit Addison-Wesley on the Web: www.awprofessional.com
Library of Congress Cataloging-in-PublicationData
Warren, Henry S.
Hacker's delight / Henry S. Warren, Jr.
p. cm.
Includes bibliographical references andindex.
1. Computer programming. I. Title.
QA76.6 .W375 2002
005.1dc21
2002066501
Copyright 2003 by Pearson Education, Inc.
All rights reserved. No part of thispublication may be reproduced, stored in a retrieval system, or transmitted, inany form, or by any means, electronic, mechanical, photocopying, recording, orotherwise, without the prior consent of the publisher. Printed in the United States of America . Publishedsimultaneously in Canada .
For information on obtaining permission foruse of material from this work, please submit a written request to:
Pearson Education, Inc.
Rights and Contracts Department
75 Arlington Street, Suite 300
Boston , MA 02116
Fax: (617) 848-7047
Text printed on recycled paper
1 2 3 4 5 6 7 8 9 10MA0605040302
First printing, July 2002
Dedication
To Joseph W. Gauld,my high school algebra teacher, for sparking in me a delight in the simplethings in mathematics.
Foreword
When I first got a summer job at MIT'sProject MAC almost 30 years ago, I was delighted to be able to work with theDEC PDP-10 computer, which was more fun to program in assembly language thanany other computer, bar none, because of its rich yet tractable set ofinstructions for performing bit tests, bit masking, field manipulation, andoperations on integers. Though the PDP-10 has not been manufactured for quitesome years, there remains a thriving cult of enthusiasts who keep old PDP-10hardware running and who run old PDP-10 softwareentire operating systems andtheir applicationsby using personal computers to simulate the PDP-10instruction set. They even write new software; there is now at least one Website whose pages are served up by a simulated PDP-10. (Come on, stoplaughingit's no sillier than keeping antique cars running.)
I also enjoyed, in that summer of 1972,reading a brand-new MIT research memo called HAKMEM, a bizarre and eclecticpotpourri of technical trivia. [1] Thesubject matter ranged from electrical circuits to number theory, but whatintrigued me most was its small catalog of ingenious little programming tricks.Each such gem would typically describe some plausible yet unusual operation onintegers or bit strings (such as counting the 1-bits in a word) that couldeasily be programmed using either a longish fixed sequence of machineinstructions or a loop, and then show how the same thing might be done muchmore cleverly, using just four or three or two carefully chosen instructionswhose interactions are not at all obvious until explained or fathomed. For me,devouring these little programming nuggets was like eating peanuts, or ratherbonbonsI just couldn't stopand there was a certain richness to them, a certainintellectual depth, elegance, even poetry.
[1] Why "HAKMEM"? Short for "hacks memo"; one36-bit PDP-10 word could hold six 6-bit characters, so a lot of the namesPDP-10 hackers worked with were limited to six characters. We were used toglancing at a six-character abbreviated name and instantly decoding thecontractions. So naming the memo "HAKMEM" made sense at the timeatleast to the hackers.
"Surely," I thought, "theremust be more of these," and indeed over the years I collected, and in somecases discovered, a few more. "There ought to be a book of them."
I was genuinely thrilled when I saw HankWarren's manuscript. He has systematically collected these little programmingtricks, organized them thematically, and explained them clearly. While some ofthem may be described in terms of machine instructions, this is not a book onlyfor assembly language programmers. The subject matter is basic structuralrelationships among integers and bit strings in a computer and efficienttechniques for performing useful operations on them.
These techniques are just as useful in the Cor Java programming languages as they are in assembly language.
Many books on algorithms and data structuresteach complicated techniques for sorting and searching, for maintaining hashtables and binary trees, for dealing with records and pointers. They overlookwhat can be done with very tiny pieces of databits and arrays of bits. It isamazing what can be done with just binary addition and subtraction and maybesome bitwise operations; the fact that the carry chain allows a single bit toaffect all the bits to its left makes addition a peculiarly powerful datamanipulation operation in ways that are not widely appreciated.
Yes, there ought to be a book about thesetechniques. Now it is in your hands, and it's terrific. If you write optimizingcompilers or high-performance code, you must read this book. You otherwisemight not use this bag of tricks every single daybut if you find yourself stuckin some situation where you apparently need to loop over the bits in a word, orto perform some operation on integers and it just seems harder to code than itought, or you really need the inner loop of some integer or bit-fiddlycomputation to run twice as fast, then this is the place to look. Or maybeyou'll just find yourself reading it straight through out of sheer pleasure.
Guy L. Steele, Jr.
Burlington, Massachusetts
April 2002
Preface
Caveat Emptor: The cost of softwaremaintenance 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 comeacross over many years. Most of them will work only on computers that representintegers in two's-complement form. Although a 32-bit machine is assumed whenthe register length is relevant, most of the tricks are easily adapted tomachines with other register sizes.
This book does not deal with large trickssuch as sophisticated sorting and compiler optimization techniques. Rather, itdeals with small tricks that usually involve individual computer words orinstructions, such as counting the number of 1-bits in a word. Such tricksoften use a mixture of arithmetic and logical instructions.
It is assumed throughout that integeroverflow interrupts have been masked off, so they cannot occur. C, Fortran, andeven Java programs run in this environment, but Pascal and ADA users beware!
The presentation is informal. Proofs aregiven only when the algorithm is not obvious, and sometimes not even then. Themethods use computer arithmetic, "floor" functions, mixtures ofarithmetic and logical operations, and so on. Proofs in this domain are oftendifficult and awkward to express.
To reduce typographical errors andoversights, many of the algorithms have been executed. This is why they aregiven 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 iswidely known, it allows the straightforward mixture of integer and bit-stringoperations, and C compilers that produce high-quality object code areavailable.