Cours Programmation (b-a ba)
< e-learning
< Loisir
< Perso
< alain
Get flash to fully experience Pearltrees
Puzzle : Devise a fast algorithm for computing the number of 1-bits in an unsigned integer. 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? int bitcount (unsigned int n) { Iterated Count runs in time proportional to the total number of bits. It simply loops through all the bits, terminating slightly earlier because of the while condition.
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 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 high-performance processors usually perform addition and multiplication as fast as bitwise operations, [ 1 ] the latter may still be optimal for overall power/performance due to lower resource use.