bitcount

Comparing binary values in MySQL

核能气质少年 提交于 2019-12-01 19:46:46
Say you have two binary values 001011 001111 How can you get the number of different bits in MySQL? I tried SELECT BIT_COUNT(BINARY 001011 ^ BINARY 001111) This returns 6, while I need a solution that returns 1 in this example. SELECT BIT_COUNT( CONV( '001011', 2, 10 ) ^ CONV( '001111', 2, 10 ) ) SELECT BIT_COUNT(b'001011' ^ b'001111'); It's converting the numbers 1011 and 1111 (base 10) to binary and doing the comparison. If you did: SELECT BIT_COUNT(11 ^ 15) It'd work. 来源: https://stackoverflow.com/questions/7111728/comparing-binary-values-in-mysql

Bit Twiddling in C - Counting Bits

房东的猫 提交于 2019-12-01 13:22:04
I want to count the bits that are set in an extremely large bit-vector (i.e. 100,000 bits). What I am currently doing is using a pointer to char (i.e. char *cPtr) to point to the beginning of the array of bits. I then: 1. look at each element of the array (i.e. cPtr[x]), 2. convert it to an integer (i.e. (int) cPtr[x]) 3. use a 256 element look-up table to see how many bits are set in the given byte (i.e. cPtr[x]). It occurs to me that if I use a short int pointer instead (i.e. short int * sPtr), then I will only need half as many look-ups, but with a 65534 element look-up table, which will

Bit Twiddling in C - Counting Bits

偶尔善良 提交于 2019-12-01 11:39:00
问题 I want to count the bits that are set in an extremely large bit-vector (i.e. 100,000 bits). What I am currently doing is using a pointer to char (i.e. char *cPtr) to point to the beginning of the array of bits. I then: 1. look at each element of the array (i.e. cPtr[x]), 2. convert it to an integer (i.e. (int) cPtr[x]) 3. use a 256 element look-up table to see how many bits are set in the given byte (i.e. cPtr[x]). It occurs to me that if I use a short int pointer instead (i.e. short int *

Counting number of bits: How does this line work ? n=n&(n-1); [duplicate]

瘦欲@ 提交于 2019-11-30 08:24:06
问题 This question already has answers here : n & (n-1) what does this expression do? [duplicate] (4 answers) Closed 3 years ago . I need some explanation how this specific line works. I know that this function counts the number of 1's bits, but how exactly this line clears the rightmost 1 bit? int f(int n) { int c; for (c = 0; n != 0; ++c) n = n & (n - 1); return c; } Can some explain it to me briefly or give some "proof"? 回答1: Any unsigned integer 'n' will have the following last k digits: One

Trailing/leading zero count for a byte

社会主义新天地 提交于 2019-11-29 10:28:22
问题 I'm using Java and I'm coding a chess engine. I'm trying to find the index of the first 1 bit and the index of the last 1 bit in a byte. I'm currently using Long.numberOfTrailingZeros() (or something like that) in Java, and would like to emulate that functionality, except with bytes. Would it be something like: byte b = 0b011000101; int firstOneBit = bitCount ((b & -b) - 1); If so, how would I implement bitCount relatively efficiently. I don't mind good explainations, please don't just give

Bit length of a positive integer in Python

删除回忆录丶 提交于 2019-11-28 18:11:49
1 = 0b1 -> 1 5 = 0b101 -> 3 10 = 0b1010 -> 4 100 = 0b1100100 -> 7 1000 = 0b1111101000 -> 10 … How can I get the bit length of an integer, i.e. the number of bits that are necessary to represent a positive integer in Python? SilentGhost In python 2.7+ there is a int.bit_length() method: >>> a = 100 >>> a.bit_length() 7 >>> len(bin(1000))-2 10 >>> len(bin(100))-2 7 >>> len(bin(10))-2 4 Note : will not work for negative numbers, may be need to substract 3 instead of 2 Gilles If your Python version has it (≥2.7 for Python 2, ≥3.1 for Python 3), use the bit_length method from the standard library.

Counting 1 bits (population count) on large data using AVX-512 or AVX-2

好久不见. 提交于 2019-11-28 01:09:10
I have a long chunk of memory, say, 256 KiB or longer. I want to count the number of 1 bits in this entire chunk, or in other words: Add up the "population count" values for all bytes. I know that AVX-512 has a VPOPCNTDQ instruction which counts the number of 1 bits in each consecutive 64 bits within a 512-bit vector, and IIANM it should be possible to issue one of these every cycle (if an appropriate SIMD vector register is available) - but I don't have any experience writing SIMD code (I'm more of a GPU guy). Also, I'm not 100% sure about compiler support for AVX-512 targets. On most CPUs,

Bit length of a positive integer in Python

前提是你 提交于 2019-11-27 10:43:58
问题 1 = 0b1 -> 1 5 = 0b101 -> 3 10 = 0b1010 -> 4 100 = 0b1100100 -> 7 1000 = 0b1111101000 -> 10 … How can I get the bit length of an integer, i.e. the number of bits that are necessary to represent a positive integer in Python? 回答1: In python 2.7+ there is a int.bit_length() method: >>> a = 100 >>> a.bit_length() 7 回答2: >>> len(bin(1000))-2 10 >>> len(bin(100))-2 7 >>> len(bin(10))-2 4 Note : will not work for negative numbers, may be need to substract 3 instead of 2 回答3: If your Python version

calculate number of bits set in byte

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 02:41:46
问题 I am interested, which is the optimal way of calculating the number of bits set in byte by this way template< unsigned char byte > class BITS_SET { public: enum { B0 = (byte & 0x01) ? 1:0, B1 = (byte & 0x02) ? 1:0, B2 = (byte & 0x04) ? 1:0, B3 = (byte & 0x08) ? 1:0, B4 = (byte & 0x10) ? 1:0, B5 = (byte & 0x20) ? 1:0, B6 = (byte & 0x40) ? 1:0, B7 = (byte & 0x80) ? 1:0 }; public: enum{RESULT = B0+B1+B2+B3+B4+B5+B6+B7}; }; Maybe it is optimal when value of byte is known at run-time? Is it

Counting 1 bits (population count) on large data using AVX-512 or AVX-2

て烟熏妆下的殇ゞ 提交于 2019-11-26 21:49:52
问题 I have a long chunk of memory, say, 256 KiB or longer. I want to count the number of 1 bits in this entire chunk, or in other words: Add up the "population count" values for all bytes. I know that AVX-512 has a VPOPCNTDQ instruction which counts the number of 1 bits in each consecutive 64 bits within a 512-bit vector, and IIANM it should be possible to issue one of these every cycle (if an appropriate SIMD vector register is available) - but I don't have any experience writing SIMD code (I'm