1

I don't understand what's going on with this code:

if ((_value & item.Value) == item.Value) { item.IsSet = true; } 

In this particular example that I'm looking at, I'm seeing the following:

if _value is 4103 but item.Value is 0, it's true.

if _value is 4103 but item.Value is 1, it's true.

if _value is 4103 but item.Value is 2, it's true.

if _value is 4103 but item.Value is 4, it's true.

if _value is 4103 but item.Value is 8, it's not true.

if _value is 4103 but item.Value is 16, it's not true.

if _value is 4103 but item.Value is 256, it's not true.

if _value is 4103 but item.Value is 512, it's not true.

if _value is 4103 but item.Value is 1024, it's not true.

if _value is 4103 but item.Value is 2048 it's not true.

if _value is 4103 but item.Value is 4096, it's true.

In another example, I'm seeing the following:

if _value is 51 but item.Value is 0, it's true.

if _value is 51 but item.Value is 1, it's true.

if _value is 51 but item.Value is 2, it's true.

if _value is 51 but item.Value is 4, it's not true.

if _value is 51 but item.Value is 8, it's not true.

if _value is 51 but item.Value is 16, it's true.

if _value is 51 but item.Value is 32, it's true.

Can someone please explain it to me?

2
  • 8
    HINT: Rewrite your question with all the numbers written in binary instead of decimal. The pattern will become very clear. (Start with the 51 example; it'll go faster.) Commented Feb 27, 2013 at 17:27
  • Why is it, @EricLippert, that I always seem to post an answer, then see some comment you added, and think "Shoot, I should have just said that"? :) Commented Feb 27, 2013 at 17:30

4 Answers 4

4

Rewrite the numbers in binary, and let's skip the zero case:

if _value is 00110011 and item.Value is 00000001, it's true. if _value is 00110011 and item.Value is 00000010, it's true. if _value is 00110011 and item.Value is 00000100, it's false. if _value is 00110011 and item.Value is 00001000, it's false. if _value is 00110011 and item.Value is 00010000, it's true. if _value is 00110011 and item.Value is 00100000, it's true. if _value is 00110011 and item.Value is 01000000, it's false. if _value is 00110011 and item.Value is 10000000, it's false. 

You see the pattern? The trues are where the corresponding bit is 1 and the falses are where it is 0.

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

Comments

4

Bitwise AND, so from your examples:

(_value & item.Value) == item.Value _value = 4103 = 1000000000111 binary _item.Value = 0 = 0 binary 1000000000111 & 0000000000000 --------------- 0000000000000 _value = 4103 = 1000000000111 binary _item.Value = 4096 = 1000000000000 binary 1000000000111 & 1000000000000 --------------- 1000000000000 

Comments

0

It is a bitwise AND operator. Essentially when you do bitwise AND of integers X and Y and then compare the result with Y you make sure that X has all bit that are set to 1 in Y also set to 1. X may also have some other bits set to 1, but those we don't care about. Here is MSDN article on the operator:

http://msdn.microsoft.com/en-us/library/sbf85k1c(v=vs.71).aspx

Comments

0

From MSDN;

The & operator can function as either a unary or a binary operator.

For integral types, & computes the bitwise AND of its operands.

For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.

So, when you write 4103, its actually equal as a binary 1000000000111. You can find binary representation of an integer value like this;

int i = 4103; Console.WriteLine (Convert.ToString(i, 2)); // 1000000000111 

Process of the rest of that situation is simple calculation with truth table.

 INPUT OUTPUT A B A ∧ B T T T T F F F T F F F F 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.