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
2
votes
1 answer

Population count in AVX512

I have been trying to use _mm256_popcnt_epi64 on a machine that supports AVX512 and on code that has previously been optimiized for AVX2. Unfortunately, I ran into the issue that the function isn't found. The corresponding __m512i equivalent is…
Alex
  • 58
  • 4
2
votes
1 answer

Why shouldn't I catch Undefined Instruction exception instead of using CPUID?

Assume I want to use an instruction that may be not available. And this instruction is not of those transparent fallback, it is undefined instruction when it is not available. Say it is popcnt for example. Can I instead of using cpuid just try and…
Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
2
votes
5 answers

Hamming weight/population count in T-SQL

I'm looking for a fast way to calculate the hamming weight/population count/"the number of 1 bits" of a BINARY(1024) field. MySQL has a BIT_COUNT function that does something like that. I couldn't find a similar function in T-SQL? Or would you…
Simon
  • 1,814
  • 20
  • 37
2
votes
1 answer

Find next number with specific hamming weight

Given a certain integer x, I wish to compute the next higher integer y which has a certain hamming weight w. Mind that the hamming weight of x does not have to be w as well. So, for example x = 10 (1010) and w = 4 the result should be y = 15…
l3moony
  • 65
  • 4
2
votes
4 answers

Do these two bit-counting algorithms have the same time complexity?

Below is an algorithm I picked up somewhere (forgot where exactly, possibly from this answer) to calculate the amount of bits set in an integer, i.e. its Hamming weight. function hamming_weight($i) { $i = $i - (($i >> 1) & 0x55555555); $i =…
vvye
  • 1,208
  • 1
  • 10
  • 25
2
votes
4 answers

Hamming weight ( number of 1 in a number) mixing C with assembly

I'm trying to count how many number 1, are in the numbers of an array. First I have a code in C lenguaje(work ok): int popcount2(int* array, int len){ int i; unsigned x; int result=0; for (i=0; i
ant1
  • 191
  • 15
2
votes
4 answers

counting the difference in bits when comparing two numbers in Lua

I would like to compare two numbers to determine the number of bits which must be flipped in order make them equal. For instance, 5 and 6 would require 2 bit flips. I can do this manually, but would like to write a Lua function to do it for me…
ridthyself
  • 821
  • 1
  • 8
  • 19
2
votes
2 answers

Number of set elements in column of type SET (population count)

I have a table declaration like CREATE TABLE my_foos ( id INT NOT NULL AUTO_INCREMENT, bars SET('one', 'two', 'three', 'four', 'five') NOT NULL ) And the values 1, ('one', 'two') 2, ('two') 3, ('three', 'four', 'five') Now I want to select…
Kijewski
  • 25,517
  • 12
  • 101
  • 143
1
vote
2 answers

Hamming weight for a list of integers in Matlab

Quite a simple problem: I have a list of integers, e.g., a = [7 8] Now I want to have a seperate list, that contains the Hamming Weight (that is the number of 1 bits in the binary represenation) for each of the integers in the list. That means the…
Patrick
  • 989
  • 3
  • 16
  • 23
1
vote
2 answers

popcount in arm assembly without neon

I have read this as well as wiki I understand the following code should result in 12 instructions in asm. i = i - ((i >> 1) & 0x55555555); // add pairs of bits i = (i & 0x33333333) + ((i >> 2) & 0x33333333); // quads i = (i + (i >> 4)) &…
user2346536
  • 1,464
  • 2
  • 21
  • 43
1
vote
0 answers

Can I get a POPCNT on a YMM register?

I'm vectorizing some image processing code using 32 bit hand-written assembly to access AVX2 instructions. However I've run into a roadblock. The results of the vector operations end up in a YMM register and I need to get a population count(POPCNT)…
Niya
  • 243
  • 2
  • 8
1
vote
1 answer

YASM is there a way to check for specific numbers in a word?

If I were to try to calculate the number of 1's in the dw data, how would I go about that? I want to store the number of 1's in memory sum. I am using EBE to code in 64 bit Assembly Language. segment .data data dw 1011011011001010b …
puyopop
  • 27
  • 5
1
vote
2 answers

How to calculate how many bits in a decimal number is 1?

This program I created in RISC-V RARS 1.3 application is designed to take a decimal number and count how many bits are in that number. The one I am testing is the decimal number 5, and this program should work for any positive number I put on t1. …
stevenu
  • 11
  • 2
1
vote
1 answer

MIPS assembly code - trying to find out what this code's about

I'm learning assembly code, and given this code, I need to find what this code is about. However I am trying to debug using qtspim. I know what the value inside each register, but I still don't get what is this code about. If you find the pattern…
devss
  • 145
  • 8
1
vote
1 answer

Generate integers with even hamming weight (popcount) c++

I want to effectively (by using bit hacks) generate all integers up a given number, k, such that they have an even hamming weight without explicitly calculating their hamming weights. It is not important to me whether that is done in ascending or…