background preloader

Low Level Bit Hacks You Absolutely Must Know

Low Level Bit Hacks You Absolutely Must Know
I decided to write an article about a thing that is second nature to embedded systems programmers - low level bit hacks. Bit hacks are ingenious little programming tricks that manipulate integers in a smart and efficient manner. Instead of performing some operation (such as counting the 1 bits in an integer) by looping over individual bits, these programming nuggets do the same with one or two carefully chosen bitwise operations. To get things going I'll assume that you know what the two's complement binary representation of an integer is and also that you know all the the bitwise operations. I'll use the following notation for bitwise operations in the article: & - bitwise and | - bitwise or ^ - bitwise xor ~ - bitwise not << - bitwise shift left >> - bitwise shift right The numbers in the article are 8 bit signed integers (though the operations work on arbitrary length signed integers) that are represented as two's complement and they are usually named 'x'. Here we go. Bit Hack #1. 1. 2.

Programming Methodology - Download free content from Stanford Binary Game Skip to Content | Skip to Footer Cisco Binary Game The Cisco Binary Game is the best way to learn and practice the binary number system. It is great for classes, students and teachers in science, math, digital electronics, computers, programming, logic and networking. It is also a LOT of fun to play for anyone who likes to play fast-paced arcade games.

Low Level Details When I started programming many of the elements we take for granted now, did not exist. There was no DirectX and not many compatible libs were available for the free compilers of the day. So I had to write my own code for most basic programs, keyboard handlers, mouse handlers, video memory accessors, rasterizers, texture mappers, blitters… the programs I wrote then were 100% my own code and I had to be able to handle anything and everything. Personally I’ve always been interested in what was going on under the hood so this suited me just fine. Combine the two and what you get today is someone who enjoys spending 5 days making that single function 20x faster, who revels in reducing the memory footprint of the primary data structure by 1 byte per element across the entire program whilst simultaneously writing a pre-caching system to avoid the special case issues…. who… well you get the picture… I’m an OCD level sport optimizing geek.

[C#] useful nippets C# for beginners 50 Places You Can Learn to Code (for Free) Online If you’re curious about learning a programming language then you’re in luck: there’s no shortage of resources for learning how to code online. University-level courses, tutorials, cheat sheets, and coding communities all offer excellent ways to pick up a new language, and maybe even a new job, too. Read on, and you’ll discover 50 great places to learn how to code, for free, online. University Many big names in education including MIT and Stanford offer programming courses, absolutely free. General If you’re just dipping your toes into programming, or you want to find a variety of resources, these sites offer several different ways to learn how to code. Community Learn how to code on these sites with a heavy community influence ready to offer help to newbs. Language Specific Drill down to the language you really want on these sites, offering expansive learning in one or two specific languages.

gotoAndLearn() - Free video tutorials from Lee Brimelow on Adobe Flash 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. Thousands of people have read it. 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 sign = (v ! Patented variation: f = v && ! Sean A.

Higher Order Macros in C++ [C#] useful nippets C# for advanced Signs that you're a bad programmer - Software Engineering Tips Why was this written? Most of these faults were discovered the hard way by the author himself, either because he committed them himself or saw them in the work of others. This paper is not meant for grading programmers, it was intended to be read by programmers who trust their ability to judge when something is a sign of bad practice, and when it's a consequence of special circumstances. This paper was written to force its author to think, and published because he thinks you lot would probably get a kick out of it, too. 1. Reasoning about code means being able to follow the execution path ("running the program in your head") while knowing what the goal of the code is. Symptoms Remedies To get over this deficiency a programmer can practice by using the IDE's own debugger as an aide, if it has the ability to step through the code one line at a time. 2. Object Oriented Programming is an example of a language model, as is Functional or Declarative programming. 3. 4. 5. 6. 1. 2. 3. 4. 5. Symptoms

Optimizing C and C++ Code Embedded software often runs on processors with limited computation power, thus optimizing the code becomes a necessity. In this article we will explore the following optimization techniques for C and C++ code developed for Real-time and Embedded Systems. Many techniques discussed here have roots in the material we covered in the articles dealing with C to Assembly translation. A good understanding of the following articles will help: Premature optimization is the root of all evil Donald Knuth wrote, "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. In general, correctness and readability considerations trump code performance issues for most of your code. Adjust structure sizes to power of two Place case labels in narrow range Place frequent case labels first Splitting a switch statement

turing Cache Coherency explained Cache coherency explained The Intel SA-110 StrongARM processor is a Harvard cache architecture processor – hence it uses separate instruction and data caches, and in addition its data cache is a write-back cache. This raises cache coherency problems in a multiprocessing configuration. Caching explained Most modern microprocessors contain some form of instruction and data caching where a small but fast bit of memory is used to speed up access to main memory. Again at its simplest level, a cache must be write-through cache whenever the processor performs a write to main memory. The problem is, writes to main memory are usually even slower than reads. Write-back Caching explained A write-back cache is for obvious reasons somewhat more complicated than that for a write-through cache. Cache incoherency Now imagine if there were two processors of this type accessing a particular area of memory at the same time. Software based Cache Coherency 1. 2. 3. Conclusions

10 places where anyone can learn to code Teens, tweens and kids are often referred to as “digital natives.” Having grown up with the Internet, smartphones and tablets, they’re often extraordinarily adept at interacting with digital technology. But Mitch Resnick, who spoke at TEDxBeaconStreet, is skeptical of this descriptor. Sure, young people can text and chat and play games, he says, “but that doesn’t really make you fluent.” Mitch Resnick: Let's teach kids to code Fluency, Resnick proposes in this TED Talk, comes not through interacting with new technologies, but through creating them. The point isn’t to create a generation of programmers, Resnick argues. In his talk, Resnick describes Scratch, the programming software that he and a research group at MIT Media Lab developed to allow people to easily create and share their own interactive games and animations. At Codecademy, you can take lessons on writing simple commands in JavaScript, HTML and CSS, Python and Ruby. While we’re at it: bonus!

Related: