Lode Vlaeminck - 17 days ago 5x

C# Question

How does the unary operator '&' work ?

In a test project i ran this code:

`int num = 50, num2 = 100;`

int x = num & num2;

result: x = 32

`int num = 100, num2 = 90;`

int x = num & num2;

result :x = 64

How is this calculated ?

Answer

From MSDN:

Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical 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.

In your case it is integeral type version.

So:

```
50 in binary is 00110010
100 in binary is 01100100
AND result is 00100000 (32 dec)
```

Source (Stackoverflow)

Comments