The '&' is bitwise, which will return the address of a variable, |
No no no. Bitwise does not mean "return the address of a variable". You've managed to mix up two separate operators.
&
on the front of a variable, such as
int* p = &a;
returns the address of a variable. In this example, the address of the variable
a
.
&
used between two variables, such as
(a & b)
, is the bitwise AND operator. The word "bitwise" indicates that it operates on each individual bit of the two values. The word "bitwise" in no way at all means "return the address of a variable". That's just completely wrong.
Two different operations; one common symbol.
For reference, here is an example of a bitwise operation. Let's say that int a=7, and int b = 13.
c = (a & b);
a : 00000111
b : 00001101
------------------
c : 00000101
Each pair of bits is individually ANDed, giving a binary answer of 00000101, which in decimal is 5.
This code demonstrates this calculation, and for completion includes the logical AND of (a && b) as well.
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
int main()
{
int a = 7;
int b = 13;
int c = (a & b);
int d = ( a && b);
std::cout << c << std::endl;
std::cout << d;
return 0;
}
|
The '&&' operator is to use more than one thing in a condition. |
It is to carry out the logical AND operation. It is not some clever dodge to have a multi-part condition. That's like saying we have two feet because shoes come in pairs.