# & operator

• 01-11-2002
Suge
& operator
How do you perform the binary operation '&' on
two ints? and what does it do?

I thought '&' was '&&' and used for logical operations...

Any help is appreciated
• 01-11-2002
Govtcheez
> what does it do?

All numbers are represented in binary, right?

So, let's say i == 4 and j == 6.

4 == 100 in binary
6 == 110 in binary

4 & 6 == 100 == 4

Whichever bits the two values both have set will be set in the answer
• 01-11-2002
Shiro
The &-operator is the bitwise logical AND.

Assume A and B are integers, than C, which is (A AND B) can be calculated as follows:

C = A & B

The result depends on the values of A and B. Bitwise:

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

Example of bitwise AND for a:

A = 1111 0101
B = 0100 1001
--------------------
C = 0100 0001

&& is a logical operation which can be used in for example if-constructions. Assume E and F are expressions. Then

Code:

```if (E == TRUE && F == TRUE) { }```
If and only if E evaluates to TRUE and F evaluates to TRUE, then the code between the brackets will be executed.