Bit twiddling and bit bashing are often used interchangeably with bit manipulation, but sometimes to exclusively refer to clever or non-obvious ways or uses of bit manipulation, or tedious or challenging low-level device control or data manipulation tasks. As a derogatory term, bit twiddling is the process of tweaking a computer program where vast amounts of time and effort produce negligible improvement, leaving the program source code unreadable to all but the original author.
The term bit twiddling dates from early computing hardware, where computer operators would make adjustments by tweaking or twiddling computer controls. As computer programming languages evolved, programmers adopted the term to mean any handling of data that involved bit-level computation.
// The obvious method
if (x < y)
r = x;
r = y;
// A method using bit manipulation
r = y + ((x - y) & -(x < y));
In most cases, the programmer would choose the former method. If it was necessary to find the minimum of two integers millions of times per second, the programmer might exploit his knowledge of binary representation of integers and come up with the latter method. Since that method does not use branching, it runs faster on some processors.
Bit manipulation often causes problems with beginning computer programmers, particularly with regard to "good programming habits". The usage of low level assembly instructions to manipulate bits is generally frowned upon. Instead the use of high level bit masks is recommended, since it increases portability and is readable for everyone who knows the language.
The following examples are written in C, but can be applied to any language supporting bitwise operators.
Set a bit (where
n is the bit number, and 0 is the least significant bit):
unsigned char a |= (1 << n);
Clear a bit:
unsigned char b &= ~(1 << n);
Toggle a bit:
unsigned char c ^= (1 << n);
Test a bit:
unsigned char e = d & (1 << n); //d has the byte value.When manipulating a bitmap consisting of multiple bytes
n = (index % 8)can be used to calculate the right bit, and the correct byte with
index / 8. A faster version, avoiding the use of the expensive modulus operation, is recommended though: use
n = (index & 7)to calculate the right bit, and calculate the correct byte with:
index >> 3.