hammingweight

#内核里的神函数# 之 hweight32

我们两清 提交于 2020-01-07 05:20:32
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在阅读iptables内核模块的代码时,我遇到了这样一个函数hweight32: unsigned int hweight32(unsigned int w) { unsigned int res = w - ((w >> 1) & 0x55555555); res = (res & 0x33333333) + ((res >> 2) & 0x33333333); res = (res + (res >> 4)) & 0x0F0F0F0F; res = res + (res >> 8); return (res + (res >> 16)) & 0x000000FF; } 乍看起来,似乎很难理解这段代码的功能,其实它就完成了“统计给定数字中值为1的bit位个数”的功能,怎么样,是不是有点风马牛不相及的感觉。 下面我们先看网络上的一篇分析 文章 : ============转载:华丽的分割线============ 在 linux 内核里,在计算 CPU 个数时用到了 hweight32 这个函数,这个函数就是汉明重量的计算方法。对于二进制串来说,它就是计算这个二进制串里含有多少个 1 。hweight32() 函数实现如下: /*代码如上*/ 下面先对一个任意的二进制串中 1 的数量的计算推出上面的公式: 1.

Calculating Hamming weight efficiently in matlab

China☆狼群 提交于 2019-12-28 13:32:53
问题 Given a MATLAB uint32 to be interpreted as a bit string, what is an efficient and concise way of counting how many nonzero bits are in the string? I have a working, naive approach which loops over the bits, but that's too slow for my needs. (A C++ implementation using std::bitset count() runs almost instantly). I've found a pretty nice page listing various bit counting techniques, but I'm hoping there is an easy MATLAB-esque way. http://graphics.stanford.edu/~seander/bithacks.html

How to generate a sse4.2 popcnt machine instruction

∥☆過路亽.° 提交于 2019-12-20 10:21:38
问题 Using the c program: int main(int argc , char** argv) { return __builtin_popcountll(0xf0f0f0f0f0f0f0f0); } and the compiler line (gcc 4.4 - Intel Xeon L3426): gcc -msse4.2 poptest.c -o poptest I do NOT get the builtin popcnt insruction rather the compiler generates a lookup table and computes the popcount that way. The resulting binary is over 8000 bytes. (Yuk!) Thanks so much for any assistance. 回答1: You have to tell GCC to generate code for an architecture that supports the popcnt

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

对着背影说爱祢 提交于 2019-12-18 16:45:18
问题 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 such combinations is computed as C(n,r) in this example C(4,2) which is 6. Note that you can solve this just by increasing a number from 0 to 2^n and see if the count is OK. However, it is not a fast solution. I am considering solving the problem

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

微笑、不失礼 提交于 2019-12-18 09:08:49
问题 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<len; i++){ x = array[i]; do{ result+= x & 0x1; x>>= 1; } while(x); } return result; } Now I need to translate the do-while loop into Assembly using 3-6 lines of code. I have write some code but the result is not correct.( I'm new in the assembly world ) int popcount3(int* array, int len){ int i;

C code to count the number of '1' bits in an unsigned char

吃可爱长大的小学妹 提交于 2019-12-18 05:55:41
问题 I need C code to return the number of 1's in an unsigned char in C. I need an explanation as to why it works if it's not obvious. I've found a lot of code for a 32-bit number but not much for an unsigned char. 回答1: The same code will work for an unsigned char. Loop over all bits testing them. See this. 回答2: const unsigned char oneBits[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}; unsigned char CountOnes(unsigned char x) { unsigned char results; results = oneBits[x&0x0f]; results += oneBits[x>>4];

C code to count the number of '1' bits in an unsigned char

好久不见. 提交于 2019-12-18 05:55:11
问题 I need C code to return the number of 1's in an unsigned char in C. I need an explanation as to why it works if it's not obvious. I've found a lot of code for a 32-bit number but not much for an unsigned char. 回答1: The same code will work for an unsigned char. Loop over all bits testing them. See this. 回答2: const unsigned char oneBits[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}; unsigned char CountOnes(unsigned char x) { unsigned char results; results = oneBits[x&0x0f]; results += oneBits[x>>4];

Hamming weight/population count in T-SQL

我的梦境 提交于 2019-12-17 20:56:35
问题 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 suggest storing the binary data in a field of another type? If you don't know what I'm talking about, here's a Wikipedia article about the hamming weight. 回答1: You could use a helper table with precalculated Hamming weights for small numbers, like bytes

Hamming weight based indexing

给你一囗甜甜゛ 提交于 2019-12-17 18:29:05
问题 Assume we have a integer of bitsize n=4; The problem I am describing is how you would go about indexing a number to an array position based on the Hamming weight and its value knowing the bitsize . E.g. An array with 16 elements for bitsize 4 would/could look like this: |0|1|2|4|8|3|5|6|9|10|12|7|11|13|14|15| Where elements are grouped by their Hamming weight(necessary) and sorted based on size(not necessary). Sorting is not necessary as long as you can take e.g. 3(0011) do some operations

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

牧云@^-^@ 提交于 2019-12-17 10:59:28
问题 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