Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise - How can I check if a binary number contains another?

  A  = 110000000    -    384 Blue+Red
  B  = 011000010    -    194 Green+Black+Red

  A & B =   C  = 010000000    -    128 Red

How can I check if B contains all the bits in A and perhaps others? In the case above I would like to get "false".

I'm using XCode & objective-c but that shouldn't matter as far as I know

like image 818
Segev Avatar asked Jan 21 '14 11:01

Segev


2 Answers

B contains A if A & B (ie, the intersection) is equal to A:

(a & b) == a

Which is analogous to

a ⊆ b ↔ (a ∩ b) = a

from set theory.

like image 161
harold Avatar answered Oct 24 '22 10:10

harold


If you mean exactly the same bits, the test is A == B.

If you mean B must have all the bits that are set in A, and perhaps others, (A & B) == A.

like image 34
Mike Seymour Avatar answered Oct 24 '22 09:10

Mike Seymour