background preloader

Bit Twiddling Hacks

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

Code Charts Scripts | Symbols | Notes Find chart by hex code: Related links: Name index Help & links Scripts Symbols and Punctuation Notes To get a list of code charts for a character, enter its code in the search box at the top. For terms of use, conventions used in this table, access to additional charts and ways to access the code charts, see Character Code Chart Help and Links. s Nginx Tutorials (version 2012.03.23) I've been doing a lot of work in the Nginx world over the last few years and I've also been thinking about writing series of tutorial-like articles to explain to more people what I've done and what I've learned in this area. Now I have finally decided to post serial tutorials to the Sina Blog in Chinese. Every article will have one rough topic and will be in a rather casual style. They're not parts of a book after all. But I do have plans to re-orginaize these stuffs to form a real book. Now the tutorials being written is devided into "series". The samples in my tutorials are at least compatible with Nginx 0.8.54; do not try the samples with older versions of Nginx. All of the Nginx modules mentioned in these tutorials are production-ready. I'm going to make extensive use of Nginx 3rd-party modules here. ? Do not reproduce these articles without explicit permissions from us. October 30, 2011

Public Domain Aeronautical Software (PDAS) CS 229: Machine Learning (Course handouts) Lecture notes 1 (ps) (pdf) Supervised Learning, Discriminative Algorithms Lecture notes 2 (ps) (pdf) Generative Algorithms Lecture notes 3 (ps) (pdf) Support Vector Machines Lecture notes 4 (ps) (pdf) Learning Theory Lecture notes 5 (ps) (pdf) Regularization and Model Selection Lecture notes 6 (ps) (pdf) Online Learning and the Perceptron Algorithm. (optional reading) Lecture notes 7a (ps) (pdf) Unsupervised Learning, k-means clustering. Lecture notes 7b (ps) (pdf) Mixture of Gaussians Lecture notes 8 (ps) (pdf) The EM Algorithm Lecture notes 9 (ps) (pdf) Factor Analysis Lecture notes 10 (ps) (pdf) Principal Components Analysis Lecture notes 11 (ps) (pdf) Independent Components Analysis Lecture notes 12 (ps) (pdf) Reinforcement Learning and Control Supplemental notes 1 (pdf) Binary classification with +/-1 labels. Supplemental notes 2 (pdf) Boosting algorithms and weak learning.

An Interview by a Seventh Grader June 2010 (A seventh grader sent me these interview questions for a school project.) 1. Programmers tell computers what to do—not in a human language like English but in special unambiguous languages called programming languages. 2. For me the best part is building things. For me the worst thing about programming is dealing with external constraints. What's most challenging about being a programmer depends on how good you are. 3. The range is very wide, because some programmers start their own companies and if these companies succeed they can make a lot. 4. This varies so much that there's no one answer. Interruptions are very bad for programming, so people who spend a lot of time programming often do it at times when they'll be left alone, like late at night or early in the morning. 5. Programming is something you learn by doing. (This is true for most fields, actually, not just programming.) 6. If you're good it's always easy to find programming jobs. 7. 8. 9. 10. 11.

Related:  CSDemoscenegraphics optimizationsasm