Low Level Bit Hacks You Absolutely Must Know - good coders code, great reuse
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'. The result is usually 'y'. The individual bits of 'x' are named b7, b6, b5, b4, b3, b3, b2, b1 and b0. The bit b7 is the sign bit (the most significant bit), and b0 is the least significant.
Read full article from Low Level Bit Hacks You Absolutely Must Know - good coders code, great reuse
No comments:
Post a Comment