![]() Int bitsInByteflyLUT::bitsInByte(byte _byte)įor (unsigned int i = 0 i < (1 << CHAR_BIT) i += 1)Ĭout << i << " " << flut.Get_bitsInByte(i) << endl Ĭonstexpr T m1 = stBitPattern::res(0x55) //binary: 0101.Ĭonstexpr T m2 = stBitPattern::res(0x33) //binary: 00110011.Ĭonstexpr T m4 = stBitPattern::res(0x0f) //binary: 4 zeros, 4 ones. The answer sought is probably one where kilo is taken to mean kibi, but there are times when the difference really matters. If (flyLUT = DUMMY) // if its first time we try to get answer for this char.įlyLUT = bitsInByte(_byte) // O(8) 2 Mind the difference between SI prefix kilobytes (1,000 bytes) and binary prefix kibibytes (1,024 bytes). Int bitsInByteflyLUT::Get_bitsInByte(byte _byte) Memset(flyLUT, DUMMY, sizeof(int)*nIndexes) O(8) for finding how many bits are ON in a byte.īitsInByteflyLUT(const bitsInByteflyLUT & _class) // COPY CTOR - forbiddenĬonst bitsInByteflyLUT & operator= (const bitsInByteflyLUT& _class) after that its O(1) īitsInByteflyLUT() //CTOR - throws std::bad_alloc first approch : activate the O(8) function in first get try. #include // for CHAR_BIT (most likely to be 8) Here is the scheme published there for counting bits in 32 bit words in C: unsigned int v // count bits set in this (32-bit value) Instead you combine values by shifting and masking in clever ways.Ī great source of clever tricks for doing this is Bit Hacks. These tend to be pure arithmetic, since you can't realistically lookup a 32 bit value in an array to get its bitcount. If you do it a lot, you are better off batching up bytes into words or doublewords, and doing fast bitcount operations on these. If you only do this once in awhile, it is likely the fastest, but then you don't need the fastest if you only do it once in awhile. That kind of works for bytes, but you pay an actual memory access for it. In this case, no C program will even come close (think 20x slower or more).The usual answer for "fastest way to do bitcount" is "look up the byte in an array". Note that if the byte-to-count mapping is inlined right next to this instruction then the whole data table will be in the CPU cache so it will be really fast. If it is compiled C it will probably generate a lot of memory accesses that will slow it down even beyond the larger number of clock cycles it requires. The problem with Kernighan's solution is that even when hand-coded in assembly it is slower than my algorithm. Note that the shift instructions in Art's solution are very CPU expensive. ![]() This is the absolute fastest way to do this and will be faster than any mathematical computation. ![]() If the number has many bytes in it (say an array pointed to by ebp), then you loop through the bytes (where ecx is the number of bytes in the array containing the number): sub ecx, 1 If the number is a single byte then the answer is one instruction: mov eax, The fastest way to do this calculation is with a table array edx where the bl register contains a byte value. More than one system exists to define larger units based on the byte. So, can this be done in better than O( n) bit tests? No.Ĭan it be done in fewer than O( n) add/shift/mask operations? Yes. The byte is a unit of digital information that most commonly consists of eight bits. If n is the number of bits then even a simple bit mask ( &) is already an O( n) operation. What's tricky about this is when you aren't careful about the definition of n and let n be "the number of bit shifting/masking instructions" or some such. Any algorithm must examine all of the bits in a number. V &= v - 1 // clear the least significant bit setīrian Kernighan's method goes through as many iterations as there are I then case my 32 bit value to a byte, and see if it equals 8. Unsigned int c // c accumulates the total bits set in v Bit Twiddling Hacks presents a number of methods, including this one: Counting bits set, Brian Kernighan's way unsigned int v // count the number of bits set in v
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |