0
Can someone explain what this --> & <-- do and for what I need this?
2 Respuestas
+ 2
Its used mostly, like a logic operator, and means "and". It's used to check expresions like this.
"if( a<b && a<c)"
in this code, you are checking that A its lower than B, AND A is lower than c.
It can be used also to pass reference values, but that its not the most frecuent use.
+ 2
If you are working with pointers the '&' operator is used to assign the address of a variable to a pointer.
Ex:
int a = 3;
int* p = &a // assign the address of the variable 'a' to the pointer 'p'.
In this context the '&' operator is the 'address of' operator.
If the '&' operator is used in a logical context it is the 'bitwise AND' operator. Bitwise operators perform bit by bit operations, in this case AND.
If the two bits are 1 the result is 1, otherwise is 0.
AND table:
a b a & b
0 0 0
0 1 0
1 0 0
1 1 1
Ex.
int a = 20; // 00010100 in binary
int b = 5; // 00000101 in binary
int c = a & b // 00000100 in binary, 4 in decimal.
AND table:
a b a & b
0 0 0
0 0 0
0 0 0
1 0 0
0 0 0
1 1 1
0 0 0
0 1 0