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: 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, but the code can become more difficult to write and maintain.
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. 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 simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition. While modern processors usually perform addition and multiplication 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.
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. The second method uses the fact that powers of two have one and only one bit set in their binary representation:. 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 predefined function for that, e. Such instructions may have greater latency, however, than the bit-twiddling solution. The programming language C has direct support for bitwise operations that can be used for bit manipulation. Bit indexing begins at 0, not 1.
Bit 0 is the least significant bit. When using an array of bytes to represent a set of bits, i. The index of the bit within the byte indexed by the above can be calculated via a modulo operation:. From Wikipedia, the free encyclopedia. This article does not cite any sources.
Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. April Learn how and when to remove this template message. Retrieved from " https: Binary arithmetic Computer arithmetic. Articles lacking sources from April All articles lacking sources. Views Read Edit View history.