# Bit Twiddling Hacks

By Sean Eron Anderson seander@cs.stanford.edu Individually, the code snippets here are in the public domain (unless otherwise noted) — feel free to use them however you please. The aggregate collection and descriptions are © 1997-2005 Sean Eron Anderson. The code and descriptions are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY and without even the implied warranty of merchantability or fitness for a particular purpose. As of May 5, 2005, all the code has been tested thoroughly. Contents About the operation counting methodology When totaling the number of operations for algorithms here, any C operator is counted as one operation. Compute the sign of an integer The last expression above evaluates to sign = v >> 31 for 32-bit integers. Alternatively, if you prefer the result be either -1 or +1, then use: sign = +1 | (v >> (sizeof(int) * CHAR_BIT - 1)); // if v < 0 then -1, else +1 On the other hand, if you prefer the result be either -1, 0, or +1, then use:

Another coding story (This one is kind of technical, sorry.) We're working on this computer game called Terra Nova, where you run around outside in powered battle armor with your squadmates beating people up. One guy on the team is a junior programmer, who is a fine guy and an okay coder, but whose coding style isn't all that 'mature', as we say in the business -- the code often isn't very elegant or robust. Anyway, he's working on this system for displaying data about your powered battle armor. There's a little 3-digit LED that shows the suit's temperature, and he's putting the temperature into string form for displaying there. So his code looks something like this: temp_to_str (int temp) { char str[4]; sprintf (str, "%d", temp); and so forth. I tell him, "Hey, this is no good, because if the temperature ever goes over 1000 (or under -100), you'll overflow your string and the game will just totally crash without warning." He says, "Oh yeah, okay, whatever, I'll fix it."

HAKMEM -- CONTENTS -- DRAFT, NOT YET PROOFED contents index [Retyped and formatted in 'html' ('Web browser format) by Henry Baker, April, 1995. The goal of this 'html' document is to make HAKMEM available to the widest possible audience -- including those without bitmapped graphics browsers. Therefore, equations have been formatted to be readable even on ASCII browsers such as 'lynx'. Click here to get original AI Memo 239 in 400 dots/inch, 1 bit/pixel, Group 4 facsimile TIFF format (a single 5 megabyte gzip compressed tar file, AIM-239.tiff.tar.gz).] Work reported herein was conducted at the Artificial Intelligence Laboratory, a Massachusetts Institute of Technology research program supported in part by the Advanced Research Projects Agency of the Department of Defense and monitored by the Office of Naval Research under Contract Number N00014-70-A-0362-0002. Reproduction of this document, in whole or in part, is permitted for any purpose of the United States Government. People referred to are from the A. Once at the A. in France:

Faster than C ― Andreas Zwinkau Judging the performance of programming languages, usually C is called the leader, though Fortran is often faster. New programming languages commonly use C as their reference and they are really proud to be only so much slower than C. Few language designer try to beat C. What does it take for a language to be faster than C? Better Aliasing Information Aliasing describes the fact that two references might point to the same memory location. void* memcopy(void* dst, const void* src, size_t count) { while (count--) *dst++ = *src++; return dst; } Depending on the target architecture, a compiler might perform a lot of optimizations with this code. In C99 the restrict keyword was added, which we could use here to encode that src and dst are different from all other references. Fortran semantics say that function arguments never alias and there is an array type, where in C arrays are pointers. Push Computation to Compile-Time Doing things at compile time reduces the run time. Runtime Optimization

Essential Math for Games Programmers As the quality of games has improved, more attention has been given to all aspects of a game to increase the feeling of reality during gameplay and distinguish it from its competitors. Mathematics provides much of the groundwork for this improvement in realism. And a large part of this improvement is due to the addition of physical simulation. Creating such a simulation may appear to be a daunting task, but given the right background it is not too difficult, and can add a great deal of realism to animation systems, and interactions between avatars and the world. This tutorial deepens the approach of the previous years' Essential Math for Games Programmers, by spending one day on general math topics, and one day focusing in on the topic of physical simulation. Topics for the various incarnations of this tutorial can be found below. Current Materials Slides The latest available versions of the slides for the math tutorials at GDC 2015 are as follows: Past Materials Core Mathematics

Research - Can Polling Location Influence STANFORD GRADUATE SCHOOL OF BUSINESS—What would you say influenced your voting decisions in the most recent local or national election? Political preferences? A candidate's stance on a particular issue? The repercussions of a proposition on your economic well-being? But Stanford Graduate School of Business researchers, doctoral graduates Jonah Berger and Marc Meredith, and S. It's hard to imagine that something as innocuous as polling location (e.g., school, church, or fire station) might actually influence voting behavior, but the Stanford researchers have discovered just that. Why might something like polling location influence voting behavior? Using data from Arizona's 2000 general election, Berger, Meredith, a visiting lecturer at MIT, and Wheeler discovered that people who voted in schools were more likely to support raising the state sales tax to fund education. This effect persisted even when the researchers controlled for—or removed the possibility of—other factors such as:

The Brainfuck Programming Language Brainfuck is the ungodly creation of Urban Müller, whose goal was apparently to create a Turing-complete language for which he could write the smallest compiler ever, for the Amiga OS 2.0. His compiler was 240 bytes in size. (Though he improved upon this later -- he informed me at one point that he had managed to bring it under 200 bytes.) I originally started playing around with Brainfuck because of my own interest in writing very small programs for x86 Linux. I also used it as a vehicle for writing a program that created ELF files. The Language A Brainfuck program has an implicit byte pointer, called "the pointer", which is free to move around within an array of 30000 bytes, initially all set to zero. The Brainfuck programming language consists of eight commands, each of which is represented as a single character. The semantics of the Brainfuck commands can also be succinctly expressed in terms of C, as follows (assuming that p has been previously defined as a char*): Resources