bitcount

Matrix transpose and population count

点点圈 提交于 2020-03-16 07:27:31
问题 I have a square boolean matrix M of size N, stored by rows and I want to count the number of bits set to 1 for each column. For instance for n=4: 1101 0101 0001 1001 M stored as { { 1,1,0,1}, {0,1,0,1}, {0,0,0,1}, {1,0,0,1} }; result = { 2, 2, 0, 4}; I can obviously transpose the matrix M into a matrix M' popcount each row of M'. Good algorithms exist for matrix transposition and popcounting through bit manipulation. My question is: would it be possible to "merge" such algorithms into a

LC3 Bit Counter

故事扮演 提交于 2020-01-14 05:11:05
问题 I'm trying to figure out how to implement a bit counter in LC3 assembly language. ex: input "00001100001000001" output "000000000000100" I would be counting the number of ones in the string of bits and outputting that number in binary. I know how to do this given one bit at a time, but I don't know how I can analyze only one bit of a 16 bit string at a time. Thanks. 回答1: There are several different ways you can count the number of bits in a value stored in the LC3. You can use bit shifting

Comparing binary values in MySQL

喜你入骨 提交于 2020-01-11 09:25:11
问题 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. 回答1: SELECT BIT_COUNT( CONV( '001011', 2, 10 ) ^ CONV( '001111', 2, 10 ) ) 回答2: SELECT BIT_COUNT(b'001011' ^ b'001111'); 回答3: 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. 来源:

C++ Fast and Efficient way to perform bit_count and AND operation on 40 byte array

不打扰是莪最后的温柔 提交于 2020-01-05 05:44:08
问题 In my project I need to AND two binary array of size 40 bytes(320 bits) and then compute set bit count in C++. I found some algorithms to do this but I want to know what is the fastest way of implementing it in c++. I mean what c++ data type would be proper?(unsinged char*,unsigned int 32,u_int64,...). I know many algorithms are compatible with 32bit integer although my array size is 40 bytes . what about the algorithms described in this link: Fast Bit Counting Techniques which one is faster?

PHP equivalent of C code from Bit Twiddling Hacks?

为君一笑 提交于 2019-12-24 16:58:10
问题 http://www-graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel v = v - ((v >> 1) & (T)~(T)0/3); // temp v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp c = (T)(v * ((T)~(T)0/255)) >> (sizeof(v) - 1) * CHAR_BIT; // count This is the same problem in Python: Python equivalent of C code from Bit Twiddling Hacks? I need to use this code in PHP, independently from integer size (the above code works up to 128-bit integers,

How to get lg2 of a number that is 2^k

故事扮演 提交于 2019-12-21 09:13:56
问题 What is the best solution for getting the base 2 logarithm of a number that I know is a power of two ( 2^k ). (Of course I know only the value 2^k not k itself.) One way I thought of doing is by subtracting 1 and then doing a bitcount: lg2(n) = bitcount( n - 1 ) = k, iff k is an integer 0b10000 - 1 = 0b01111, bitcount(0b01111) = 4 But is there a faster way of doing it (without caching)? Also something that doesn't involve bitcount about as fast would be nice to know? One of the applications

How to simulate the MySQL bit_count function in Sybase SQL Anywhere?

我只是一个虾纸丫 提交于 2019-12-13 01:25:50
问题 MySQL's bit_count function is quite useful for some cases: http://dev.mysql.com/doc/refman/5.5/en/bit-functions.html#function_bit-count Now I would like to use that function in other databases, that do not support it. What's the simplest way to do it (without creating a stored function, as I do not have access to the client database on a DDL level). One pretty verbose option is this (for TINYINT data types): SELECT (my_field & 1) + (my_field & 2) >> 1 + (my_field & 4) >> 2 + (my_field & 8) >>

How to get lg2 of a number that is 2^k

╄→尐↘猪︶ㄣ 提交于 2019-12-04 03:31:10
What is the best solution for getting the base 2 logarithm of a number that I know is a power of two ( 2^k ). (Of course I know only the value 2^k not k itself.) One way I thought of doing is by subtracting 1 and then doing a bitcount: lg2(n) = bitcount( n - 1 ) = k, iff k is an integer 0b10000 - 1 = 0b01111, bitcount(0b01111) = 4 But is there a faster way of doing it (without caching)? Also something that doesn't involve bitcount about as fast would be nice to know? One of the applications this is: suppose you have bitmask 0b0110111000 and value 0b0101010101 and you are interested of (value &

Finding how many bits it takes to represent a 2's complement using only bitwise functions

冷暖自知 提交于 2019-12-02 22:45:12
问题 We can assume an int is 32 bits in 2's compliment The only Legal operators are: ! ~ & ^ | + << >> At this point i am using brute force int a=0x01; x=(x+1)>>1; //(have tried with just x instead of x+1 as well) a = a+(!(!x)); ... with the last 2 statements repeated 32 times. This adds 1 to a everytime x is shifted one place and != 0 for all 32 bits Using the test compiler it says my method fails on test case 0x7FFFFFFF (a 0 followed by 31 1's) and says this number requires 32 bits to represent.

Finding how many bits it takes to represent a 2's complement using only bitwise functions

情到浓时终转凉″ 提交于 2019-12-02 13:03:41
We can assume an int is 32 bits in 2's compliment The only Legal operators are: ! ~ & ^ | + << >> At this point i am using brute force int a=0x01; x=(x+1)>>1; //(have tried with just x instead of x+1 as well) a = a+(!(!x)); ... with the last 2 statements repeated 32 times. This adds 1 to a everytime x is shifted one place and != 0 for all 32 bits Using the test compiler it says my method fails on test case 0x7FFFFFFF (a 0 followed by 31 1's) and says this number requires 32 bits to represent. I dont see why this isnt 31 (which my method computes) Can anyone explain why? And what i need to