Jump to content

Bit manipulation

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Sbalfour (talk | contribs) at 20:25, 13 November 2019 (See also: dash). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer programming tasks that require bit manipulation include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization. For most other tasks, modern programming languages allow the programmer to work directly with abstractions instead of bits that represent those abstractions. Source code that does bit manipulation makes use of the bitwise operations: AND, OR, XOR, NOT, and possibly other operations analogous to the boolean operators; there are also bit shifts and operations to count ones and zeros, find high and low one or zero, set, reset and test bits, extract and insert fields, mask and zero fields, gather and scatter bits to and from specified bit positions or fields. Integer arithmetic operators can also effect bit-operations in conjunction with the other operators.

Bit manipulation, in some cases, can obviate or reduce the need to loop over a data structure and can give many-fold speed ups, as bit manipulations are processed in parallel.

Terminology

Bit twiddling and bit bashing are often used interchangeably with bit manipulation, but sometimes exclusively refer to clever or non-obvious ways or uses of bit manipulation, or tedious or challenging low-level device control data manipulation tasks.

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.

Bitwise operation

A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the central processing unit (CPU), and is used to manipulate values for comparisons and calculations.

On most processors, the majority of bitwise operations are single cycle - substantially faster than division and multiplication and branches. While modern processors usually perform some arithmetic and logical operations just as fast as bitwise operations due to their longer instruction pipelines and other architectural design choices, bitwise operations do commonly use less power because of the reduced use of resources.

Example of bit manipulation

To determine if a number is a power of two, conceptually we may repeatedly do integer divide by two until the number won't divide by 2 evenly; if the only factor left is 1, the original number was a power of 2. Using bit and logical operators, there is a simple expression which will return true (1) or false (0):

 bool isPowerOfTwo = x && !(x & (x - 1));

The second method uses the fact that powers of two have one and only one bit set in their binary representation:

x         == 0...010...0
x-1       == 0...001...1
x & (x-1) == 0...000...0

If the number is neither zero nor a power of two, it will have '1' in more than one place:

x         == 0...1...010...0
x-1       == 0...1...001...1
x & (x-1) == 0...1...000...0

If inline assembly language code is used, then an instruction that counts the number of 1's or 0's might be available; for example, the POPCNT instruction from the x86 instruction set. Some compilers provide a predefined function for that. Such instructions may have greater latency, however, than the bit-twiddling solution.

Masks

A mask is data that is used for bitwise operations, particularly in a bit field.

Using a mask, multiple bits in a Byte, nibble, word (etc.) can be set either on, off or inverted from on to off (or vice versa) in a single bitwise operation.

Bit manipulation in the C programming language

The programming language C has direct support for bitwise operations that can be used for bit manipulation. In the following examples, n is the index of the bit to be manipulated within the variable bit_fld, which is an unsigned char being used as a bit field. Bit indexing begins at 0, not 1. Bit 0 is the least significant bit.

Set a bit
bit_fld |= (1 << n)
Clear a bit
bit_fld &= ~(1 << n)
Toggle a bit
bit_fld ^= (1 << n)
Test a bit
bit_fld & (1 << n)

When using an array of bytes to represent a set of bits, i.e., a bit array or bitset, the index of the byte in the array associated with a bit n can be calculated using division:

n / CHAR_BIT

where n is the index of the given bit and CHAR_BIT gives the number of bits in a C char.

The index of the bit within the byte indexed by the above can be calculated via a modulo operation:

n % CHAR_BIT

Note: unsigned char is typically used in C to represent a byte and CHAR_BIT is most often 8 on modern processors. % is the C modulo operator.

See also

References

Further reading

  • Warren, Henry S. (2012). Hacker's Delight (2nd ed.). Addison–Wesley Professional. p. 512. ISBN 978-0321842688.
  • Knuth, Donald E. (2009). The Art of Computer Programming Volume 4, Fascicle 1: Bitwise tricks & techniques; Binary Decision Diagrams (1st ed.). Addison–Wesley Professional. p. 272. ISBN 978-0321580504. (Draft of Fascicle 1a available for download)