Is there a way to find the bit that has been set the least amount of times from using only bit operations?
For example, if I have three bit arrays:
11011001
11100000
11101101
the bits in position 3 and 5 are set to 1 in only 1 of the three vectors.
I currently have an o(n) solution where n is the number of bits in the bitarray, where I go through each bit in the bitarray and increment each time there is a 1, but for some reason I think there is a o(1) solution that I can use with few bitwise operations. Can anyone advise? Thanks.
You can use a duplicate/shift/mask approach to separate the bits and maybe be a little faster than an iterative bit shift scheme, if the total number of values is limited.
Eg for each “bits” 8-bit value, assuming no more than 15 values:
Then, at the end, break each bitsSumN value into two 4-bit counts.