mattkrick / fast-bitset

A fast bitset with some nice methods

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.not operation

skutac opened this issue · comments

It would be really nice to have the .not function for the negation of two bit sets:
111 NOT 010 = 101

Best,
Ctibor

What you're describing is xor: ('111').toString(2) ^ ('010').toString(2). not is a unary bitwise operator. Typically, you don't want to use not because it'll always be faster to just look for the inverse of what you want. (eg instead of foo.nextSetBit(5) just call foo.nextUnsetBit(5).

Well, probably I shouldn't use the name NOT, but rather the difference (subtraction) between two bit sets, which is not XOR. In XOR you get: 1100 XOR 1010 = 0110, but with DIFF you get: 1100 DIFF 1010 = 0100.

i've never heard of that. How would you compute that with 2 INT32s?

I think the best thing to do would be use the forEach, since that iterates over just set bits. So, if it's subtraction, and your diff logic assumes 0 - 1 === 0, then your logic looks like this:

  • 1 AND 1 -> 0
  • 1 AND 0 -> 1
  • 0 AND 1 -> 0
  • 0 AND 0 -> 0

to do this:

const result = new BitSet(1000);
topBitSet.forEach(idx => if (!bottomBitSet.get(idx)) result.set(idx))

Thanks Matt.

Actually, I've realized that we both look at the bit set a little bit differently. For me the bit set is more of a bit string where bits are independent and each bit represents e.g., that an item belongs or not belongs to a set. The right word for the kind of comparison I was looking for is set complement, but in many programming languages is called difference, it can be done by subtracting two sets and it can be done as A AND NOT(B).