Bit strings: checking if one bit string is a subset of another

I represent a set of English alphabets as a 26 bit bitstring. The first bit corresponds to "a", the set bit to "b", etc. Thus,
The string ab is represented as 11000000000000000000000000
Now, given the two bit strings, I want to check if bitstring 1 is a subset of bit string 2. That is, in all places of bitstring 1 has β€œ1”, bit 2 should also have β€œ1”. This means that all characters in line1 are also present in line2. Can someone please let me know the best way to do this?
I know a simple way: iterate over the bit of string1 and check the corresponding bit in bit string2. However, I am wondering if this can be done using some bit operator in a more efficient way.

+6
source share
2 answers

If you really use only 26 bits, you can use an integer (32 bits) to represent the bit set and use the bitwise AND (&) operator to get the intersection of the two sets.

If a & b == a , a is a subset of b

+10
source

If you used BitSet instead of byte , you could use the and or xor operators.

BitSet has various bit operations, except shift , unfortunately.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/BitSet.html#xor%28java.util.BitSet%29

The first set xor second set should be 0.

Since you use only 26 characters, you can do the same with a simple int . Just setting individual bits is a bit more dirty:

 a |= 1 << offset; 
0
source

Source: https://habr.com/ru/post/925096/


All Articles