-4

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 ?

Lode Vlaeminck
  • 894
  • 1
  • 9
  • 24

8 Answers8

2

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)
BWA
  • 5,672
  • 7
  • 34
  • 45
1

Bitwise operator works on bits and perform bit-by-bit operation.

Binary AND Operator copies a bit to the result if it exists in both operands.

(A & B) = 12, i.e., 0000 1100
Dsenese1
  • 1,106
  • 10
  • 18
1

First thing: & in this context is a binary operator, as it has two arguments. It is not a unary operator.

& with two arguments is the bitwise AND operator.

50 & 100 is 0b110010 & 0b1100100. Apply & to each bit gives you 0b100000 which is 32.

You can analyse 100 & 90 similarly.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Binary values

50 (10 ) = 0110010  (2)
100 (10) = 1100100  (2)

And a logical AND is used so only the bits where both values are 1 are now one resulting in:

0100000 (2) = 32 (10)

and

100 (10) = 1100100  (2)
AND
90 (10) =  1011010 (2)
------------------------
64 (10) =  1000000(2)
DaMachk
  • 643
  • 4
  • 10
1

Represent both numbers as binaries (in order to see bits clearly when doing bitwise operations) and have a look at what's going on:

private static String AndExplanation(int left, int right) {
  String x = Convert.ToString(left, 2);
  String y = Convert.ToString(right, 2);
  String z = Convert.ToString(left & right, 2);

  int length = Math.Max(x.Length, y.Length);

  return String.Join(Environment.NewLine,
    $"{x.PadLeft(length, '0')} ({left}) &",
    $"{y.PadLeft(length, '0')} ({right})",
    new String('-', length),
    $"{z.PadLeft(length, '0')} ({left & right})"
  );
}

Tests

Console.Write(AndExplanation(50, 100));

Console.Write(AndExplanation(90, 100));

You'll see:

0110010 (50) &
1100100 (100)
-------
0100000 (32)

and

1011010 (90) &
1100100 (100)
-------
1000000 (64)
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Short answer is - it depends.

For integers, as in your case it does bit operations. So 01100100 (100) bitwise AND 00110010 (50) = 00100000 (32).

For booleans that's an eager AND (evaluates both expressions always) - unlike the lazy AND && which doesn't evaluate the second expression if first is false.

decPL
  • 5,384
  • 1
  • 26
  • 36
0

This is a bitwise AND:

In "0b0110010 AND 0b1100100", you should look for the the 1's and 0's shared at common places, which gives you 0b0100000=32.

The same holds for "100 AND 90" which gives you 0b1000000=64.

I hope it helped.

A.Yazdiha
  • 1,336
  • 1
  • 14
  • 29
0

It's a bits operator and. The bits(binary) for 50 is 110010 and 100 is 1100100, when you align them (flush to the right), you get 110010 1100100

The & operator works in such a way that, at each position, you get 1 if both lines have 1 and you get 0 otherwise, as shown below. 110010 1100100 ||||||| 0100000 So you get the binary result 100000, which is 32.

Shiping
  • 1,203
  • 2
  • 11
  • 21