hammingweight

Elegantly determine if more than one boolean is “true”

本秂侑毒 提交于 2019-11-27 18:04:35
I have a set of five boolean values. If more than one of these are true I want to excecute a particular function. What is the most elegant way you can think of that would allow me to check this condition in a single if() statement? Target language is C# but I'm interested in solutions in other languages as well (as long as we're not talking about specific built-in functions). One interesting option is to store the booleans in a byte, do a right shift and compare with the original byte. Something like if(myByte && (myByte >> 1)) But this would require converting the separate booleans to a byte

Bit popcount for large buffer, with Core 2 CPU (SSSE3)

ε祈祈猫儿з 提交于 2019-11-27 14:06:29
I'm looking for the fastest way to popcount on large buffer of 512 or more bytes. I can guarantee any required alignment, and the buffer size is always a power of 2. The buffer corresponds to block allocations, so typically the bits are either all set, none set, or mostly set favoring the "left" of the buffer, with occasional holes. Some solutions I've considered are: GCC's __builtin_popcount Bitslice popcount_24words Counting bits set, Brian Kernighan's way I'm interested in the fastest solution, it must work on 32bit x86 chipset belonging to core2 or more recent. SSE and SIMD are of great

Elegantly determine if more than one boolean is “true”

久未见 提交于 2019-11-26 19:13:48
问题 I have a set of five boolean values. If more than one of these are true I want to excecute a particular function. What is the most elegant way you can think of that would allow me to check this condition in a single if() statement? Target language is C# but I'm interested in solutions in other languages as well (as long as we're not talking about specific built-in functions). One interesting option is to store the booleans in a byte, do a right shift and compare with the original byte.

How to count the number of set bits in a 32-bit integer?

半腔热情 提交于 2019-11-25 22:52:58
问题 8 bits representing the number 7 look like this: 00000111 Three bits are set. What are algorithms to determine the number of set bits in a 32-bit integer? 回答1: This is known as the 'Hamming Weight', 'popcount' or 'sideways addition'. The 'best' algorithm really depends on which CPU you are on and what your usage pattern is. Some CPUs have a single built-in instruction to do it and others have parallel instructions which act on bit vectors. The parallel instructions (like x86's popcnt , on