# Śîœü·ĂÎÊ°ćżé ČéżŽ: 2874 | »ŰžŽ: 3

# ŐâžöËă·šÔőĂŽÀíœâŁżCounting bits set [žŽÖÆÁŽœÓ]

ÂÛÌł»ŐŐÂ:
0 ·ą±íÓÚ 2011-11-22 16:08 |ÏÔÊŸÈ«ČżÂ„Čă
 Counting bits set, in parallel unsigned int v; // count bits set in this (32-bit value) unsigned int c; // store the total here static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers static const int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF}; c = v - ((v >> 1) & B); c = ((c >> S) & B) + (c & B); c = ((c >> S) + c) & B; c = ((c >> S) + c) & B; c = ((c >> S) + c) & B; The B array, expressed as binary, is: B = 0x55555555 = 01010101 01010101 01010101 01010101 B = 0x33333333 = 00110011 00110011 00110011 00110011 B = 0x0F0F0F0F = 00001111 00001111 00001111 00001111 B = 0x00FF00FF = 00000000 11111111 00000000 11111111 B = 0x0000FFFF = 00000000 00000000 11111111 11111111 We can adjust the method for larger integer sizes by continuing with the patterns for the Binary Magic Numbers, B and S. If there are k bits, then we need the arrays S and B to be ceil(lg(k)) elements long, and we must compute the same number of expressions for c as S or B are long. For a 32-bit v, 16 operations are used. The best method for counting bits in a 32-bit integer v is the following: v = v - ((v >> 1) & 0x55555555);                    // reuse input as temporary v = (v & 0x33333333) + ((v >> 2) & 0x33333333);     // temp c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count The best bit counting method takes only 12 operations, which is the same as the lookup-table method, but avoids the memory and potential cache misses of a table. It is a hybrid between the purely parallel method above and the earlier methods using multiplies (in the section on counting bits with 64-bit instructions), though it doesn't use 64-bit instructions. The counts of bits set in the bytes is done in parallel, and the sum total of the bits set in the bytes is computed by multiplying by 0x1010101 and shifting right 24 bits. žŽÖÆŽúÂë

ÂÛÌł»ŐŐÂ:
0 ·ą±íÓÚ 2011-11-23 01:47 |ÏÔÊŸÈ«ČżÂ„Čă
 ±ŸÌûŚîșóÓÉ fallening ÓÚ 2011-11-23 01:59 ±àŒ­ ¶ț·Ö hacker's delight ÉÏÓĐĐŽ

ÂÛÌł»ŐŐÂ:
0 ·ą±íÓÚ 2011-11-23 09:01 |ÏÔÊŸÈ«ČżÂ„Čă
 »ŰžŽ 2# fallening     Œ«Æ·șĂÊé°ąŁŹĐ»Đ»

ÂÛÌł»ŐŐÂ:
0 ·ą±íÓÚ 2011-11-23 09:39 |ÏÔÊŸÈ«ČżÂ„Čă
 Č»¶źŃœ
 ÄúĐèÒȘ”ÇÂŒșóČĆżÉÒÔ»ŰÌû ”ÇÂŒ | ŚąČá ±Ÿ°æ»ę·ÖčæÔò ·ą±í»ŰžŽ »ŰÌûșóÌűŚȘ”œŚîșóÒ»Òł DTCC2020ÖĐčúÊęŸĘżâŒŒÊőŽó»á