10
  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

1
  • It's unclear what the colors are refering to in your example. I guess your question is "How can I check if all the bits set to 1 in number A are also set to 1 in number B ?" Commented Jan 21, 2014 at 11:56

3 Answers 3

17

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.

Sign up to request clarification or add additional context in comments.

1 Comment

a ⊆ b ↔ (a ∩ b) = a Took me back a few years, thanks!
1

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.

1 Comment

Of course your test won't print anything: your value of B doesn't contain all the bits of your value of A, so you get false as you say you want.
0

Use ex-nor

 In C ^ is ex-or operator and ~ is complement, to get ex-nor use ~(a^b)

if a and b are same then all bits will be 1 in ~(a^b)

2 Comments

Or, equivalently, a == b. I don't think the obfuscation adds anything.
Definitely. To perform bit wise operator you can use above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.