Questions tagged [hammingweight]

The Hamming weight of a positive integer is the count of one bits in its binary representation.

The Hamming weight of a positive integer is the count of one bits in its binary representation.

A common synonym is "population count", hence the common function name popcount.

69 questions
9
votes
2 answers

Fast popcount on Intel Xeon Phi

I'm implementing an ultra fast popcount on Intel Xeon® Phi®, as it's a performance hotspot of various bioinformatics software. I've implemented five pieces of code, #if defined(__MIC__) #include __attribute__((align(64))) static const…
7
votes
2 answers

What is the fastest algorithm to computer all permutations of a binary number with same hamming weight?

I want an algorithm to compute all permutations of a fixed size binary number with a given hamming weight. For example, if the hamming weight is 2 and the binary size is 4 then there are these outputs: 0011 0110 0101 1100 1010 1001 The number of…
Aznaveh
  • 558
  • 8
  • 27
6
votes
5 answers

Counting the number of bits that are set

I want to count the number of bits in a binary number that are set. For example, user enter the number 97 which is 01100001 in binary. The program should give me that 3 bits are set using MIPS ISA. I am able to achieve this in C, but I don't know…
csguy11
  • 5,007
  • 6
  • 22
  • 20
6
votes
2 answers

Bit-Count or Hamming-weight of a BitString in Elixir?

Please how can we efficiently calculate the hamming-weight of a bit-string in elixir? Example: 0b0101101001 has a Hamming-weight of 5 (i.e. 5 bits set) My Attempt: iex> Enum.count(Integer.to_char_list(n,2),&(&1===49))
Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
5
votes
3 answers

Popcount assembly / sum indexes of set bits

I want to sum all indexes of set bits. http://bitmath.blogspot.com/2023/01/weighted-popcnt.html?m=1 has an interesting implementation: // sum of indexes of set bits int A073642(uint64_t n) { return __popcnt64(n & 0xAAAAAAAAAAAAAAAA) + …
user21777985
5
votes
3 answers

Checking CPU Popcount from C#

Does anyone know how to check from C# whether the CPU supports popcount (population count)? I'm trying to port some chess code from C++ to C#.
David
  • 131
  • 3
  • 6
5
votes
9 answers

NASM: Count how many bits in a 32 Bit number are set to 1

I have a 32 Bit number and want to count know how many bits are 1. I'm thinking of this pseudocode: mov eax, [number] while(eax != 0) { div eax, 2 if(edx == 1) { ecx++; } shr eax, 1 } Is there a more efficient way? I'm using NASM on…
citronas
  • 19,035
  • 27
  • 96
  • 164
4
votes
5 answers

How many 1s in an n-bit integer?

An interesting problem I ran into today: what is the fastest way to count the number of 1s in an n-bit integer? Is it possible to beat O(n)? For example: 42 = 0b101010 => 3 ones 512 = 0b1000000000 => 1 one Clearly, the naive algorithm is to simply…
carl
  • 49,756
  • 17
  • 74
  • 82
4
votes
2 answers

How do I enable the SSE4.2 instruction set in Visual C++?

I am using the BRIEF descriptor in OpenCV in Visual C++ 2010 to match points in two images. In the paper about the BRIEF-descriptor is written that it is possible to speed up things: "The BRIEF descriptor uses hamming distance, which can be done…
Fredrik
  • 141
  • 2
  • 5
3
votes
1 answer

Efficiently calculate hamming weight

I'm on an apple m1 processor. What i'm trying to do is efficiently count the 1 bits in a large char array in rust. I looked up the arm neon instructions, and I think I can do it via a cnt instruction (which counts 1's per 8 bit block) and then a…
Anko
  • 1,292
  • 10
  • 19
3
votes
1 answer

Bijection between (n choose k) and bitstrings of length n with k bits set

While I know how to generate all (n choose k) bitstrings of size n with exactly k bits set to one, I'm struggling finding a bijection, that gets as input a number i between 1 and (n choose k) and outputs the i-th vector of that kind in an arbitrary…
Memphisd
  • 307
  • 1
  • 8
3
votes
1 answer

Why does my Hamming weight function work in C but not in Rust?

I've got the following Hamming weight code in Rust, and it returns garbage for 0xffff and 0xffffffff, but the identical code in C works, so I must be misunderstanding something about how Rust does bit-level operations. It's completely…
Mark Wright
  • 705
  • 7
  • 20
3
votes
1 answer

Calculate Hamming weight and/or distance in VBA Excel

I’m trying to compare clients, two by two, whose qualities can be defined by binary choices (for example a client uses a product or not). After much search online, it looks like I’d need to use the Hamming Distance for that, or its equivalent: find…
P. O.
  • 185
  • 1
  • 14
3
votes
1 answer

How does this magic bit counting method work?

While working on the XKCD April Fool's skein hash collision problem I ran across this strange, fast, multiplicative method of counting the set bits in a word: c = (v * 0x200040008001ULL & 0x111111111111111ULL) % 0xf; Why does this work / what's…
2
votes
2 answers

Count integers in [1..N] with K zero bits below the leading 1? (popcount for a contiguous range without HW POPCNT)

I have following task: Count how many numbers between 1 and N will have exactly K zero non-leading bits. (e.g. 710=1112 will have 0 of them, 4 will have 2) N and K satisfy condition 0 ≤ K, N ≤ 1000000000 This version uses POPCNT and is fast enough…