- Bit manipulation
Bit manipulation is the act of algorithmically manipulating
bit s or other pieces of data shorter than abyte . Programming tasks that require bit manipulation include low-level device control,error detection and correction algorithms,encryption algorithms, and optimization. For most other tasks, modernprogramming languages allow theprogrammer to work directly with abstractions instead of bits that represent those abstractions.Source code that does bit manipulation makes use of thebitwise operation s: AND, OR, XOR, NOT, and bit shifts.Bit twiddling
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 .Example of bit manipulation
The following code samples, written in the C programming language, both determine the minimum of two
integer values (x
andy
) and assign the result tor
.// The obvious method if (x < y) r = x; else 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.dubious
Note that character
&
represents thebitwise operation "AND " in theC programming language .Common bit manipulation techniques
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 withindex / 8
. A faster version, avoiding the use of the expensive modulus operation, is recommended though: usen = (index & 7)
to calculate the right bit, and calculate the correct byte with:index >> 3
.See also
*
Bit twiddler
*Bit specification
* Flag — a bit representing a boolean value
*Nibble — unit of data consisting of 4 bits, or half a byte
*Mask (computing)
* Bit Twiddling Hacks http://graphics.stanford.edu/~seander/bithacks.html
* [http://bits.stephan-brumme.com Bit Manipulation Tricks] with full explanations and source codeReferences
* cite book
author = Henry S. Warren Jr.
title = Hacker's Delight
publisher =Addison-Wesley
isbn = 0-201-91465-4
* cite web
author = Sean Eron Anderson
title = Bit Twiddling Hacks
url = http://graphics.stanford.edu/~seander/bithacks.html
Wikimedia Foundation. 2010.