background preloader

Back Up

Facebook Twitter

Bitwise operation. In digital computer programming, a bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits.

Bitwise operation

It is a fast, primitive action directly supported by the processor, 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. Bitwise operators[edit] In the explanations below, any indication of a bit's position is counted from the right (least significant) side, advancing left. Fast Bit Counting. Devise a fast algorithm for computing the number of 1-bits in an unsigned integer.

Fast Bit Counting

If the machine supports n-bit integers, can we compute the number of 1-bits in O(log n) machine instructions? Can we compute the number of 1-bits in O(b) machine instructions where b is the number of 1-bits in the integer? Source: Commonly asked in job interviews for Software Engineers.