Questions tagged [bitcount]
42 questions
3
votes
3 answers
Bit counting arbitrarily large positive integers in C#
There are many implementations of bit counting out there but in my case, I need to test if an arbitrarily large number contains at most two set bits.
I wrote the following function that does the job and seems to be quite fast but I wanted to find…

Raheel Khan
- 14,205
- 13
- 80
- 168
2
votes
3 answers
Matrix transpose and population count
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,…

edrezen
- 127
- 1
- 7
2
votes
1 answer
Counting number of '1' values in each bit position in Redshift column
I have BIGINT column in my Redshift table, and I want a query that will:
Count how many times the value '1' appears in each bit position across the binary value in all the rows of this column
Will show it in a way that I'll be able to take the x…

JustinCase
- 171
- 1
- 11
2
votes
4 answers
Trailing/leading zero count for a byte
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…

solinent
- 1,605
- 1
- 17
- 19
2
votes
0 answers
MongoDB support search Bitwise XOR and Bit Count?
I would like to move from MYSQL to MongoDB, one of the question I can not find answer for, if I can get or simulate XOR and Bit Count, which I need.
In MYSQL I would do:
SELECT BIT_COUNT(SimHash ^ $SimHash) as simhash ... ORDER BY simhash
It is…

2ge
- 269
- 4
- 12
2
votes
2 answers
Why does Integer.bitCount() return 8 for an input of 255?
The Java API for Integer.bitCount() tells us:
"public static int bitCount(int i)
Returns the number of one-bits in the two's complement binary representation of the specified int value. This function is sometimes referred to as the population…

Jonathan Sterling
- 604
- 4
- 13
2
votes
2 answers
How can I know the bit count of a cpu/os in C
I know how to get the bit count of a cpu or an operation system with shell.
cat /proc/cpuinfo | grep lm #-> get bit count of a cpu
uname -a #-> get bit count of an operation system
However, how can we get the bit count of those…

xianyu1337
- 231
- 2
- 7
1
vote
4 answers
Bit Twiddling in C - Counting Bits
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…

user1245262
- 6,968
- 8
- 50
- 77
1
vote
1 answer
How to simulate the MySQL bit_count function in Sybase SQL Anywhere?
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…

Lukas Eder
- 211,314
- 129
- 689
- 1,509
1
vote
2 answers
XOR operation and BitCount in Java on Long variables returns java.lang.NumberFormatException
I am trying to do a XOR operation on two 64 bits Long variables in Java.
The problem is that it fails when I add more than 16 bits in a variable.
For instance this works and returns 7:
Long h1 = Long.parseLong("1100001101001101");
Long h2 =…

c1377554
- 173
- 1
- 10
1
vote
2 answers
PHP equivalent of C code from Bit Twiddling Hacks?
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; …

user323094
- 3,643
- 4
- 21
- 30
1
vote
2 answers
Can someone explain how this bitCount code works?
This is code that my TA help me get, but then I completely forgot how it worked exactly since I can't seem to get the right answer, and the interview grading is tomorrow. If anybody can help please do. Thanks
* bitCount - returns count of number of…

Ryan Fasching
- 449
- 2
- 11
- 21
1
vote
4 answers
Counting number of 1 bits in C++ negative number
The following function:
int numOnesInBinary(int number) {
int numOnes = 0;
while (number != 0) {
if ((number & 1) == 1) {
numOnes++;
}
number >>= 1;
}
return numOnes;
}
will only work for positive…
programmer
1
vote
1 answer
LC3 Bit Counter
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…

CDO
- 302
- 1
- 3
- 17
1
vote
4 answers
Is there a big.BitCount?
Is there an already-written BitCount method for big.Int? There doesn't seem to be one in math/big.
Obviously I will write one myself if not - does anyone have one already written?
I want the number of set bits in the number. Like Java…

OldCurmudgeon
- 64,482
- 16
- 119
- 213