• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# & and &&

## Recommended Posts

jack_1313    536
C++. What is the difference between a single & and &&? I personaly always use &, but I often see people using &&. Why?

##### Share on other sites
Zipster    2359
& = bitwise-AND
&& = conditional-AND

You use the latter for doing conditional checks, and the former for operations on bits. This also determines how the compiler treats it, i.e if it finds a bitwise-AND it will literally AND the bits of the operands (assuming it lets you do the operation), while the conditional-AND will work with true/false, non-zero/zero.

[edited by - Zipster on July 7, 2003 12:16:42 AM]

##### Share on other sites
Boblin    122
&& is used to check if two or more expressions are true or not
example
if(x = 0 && x < 5){ // do some code stuff}

this checks to see if x is 0 AND if x is less than 5

the symbol & is a bitwise operation which compares the bits between two numbers. if 5 (binary 101) was & with 7 (binary 111), the result would be 5 (binary 101) because the second binary number of both 5 and 7 are different, which makes the result of the second bit false. The first and the third bits of 5 and 7 are the same, so the final results of the first and third bits are true.

I hope this helps some
-Boblin

##### Share on other sites
ApochPiQ    23003
Since it hasn''t been mentioned, if you are using & or && in an if statement, and you aren''t explicitly needing bitwise testing, then always use &&. Using & when you are looking for a "this and this" condition is bad form.

##### Share on other sites
eighty    220
Often they give the same results but they can absolutely not be used interchangeably.

1 & 2; // 01 && 2; // 1int x = 0;0 & (x = 1); // here x will be 10 && (x = 1); // here x will remain 0

[edited by - eighty on July 8, 2003 4:25:09 AM]

##### Share on other sites
Guest Anonymous Poster
As eighty implicitely said, && is a short-circuit operation. If its left side is false, it won''t even evaluate the right side. So using && on conditionals is often faster. (well, sometimes branching can make it slower than &. But if right side needs calculation, using && will be faster. && is considered better style anyway)

##### Share on other sites
Guest Anonymous Poster
quote:
Original post by eighty
Often they give the same results but they can absolutely not be used interchangeably.

<code>
1 & 2; // 0
1 && 2; // 1

int x = 0;
0 & (x = 1); // here x will be 1
0 && (x = 1); // here x will remain 0
</code>

<SPAN CLASS=editedby>[edited by - eighty on July 8, 2003 4:25:09 AM]</SPAN>

0 & (x = 1);
0 && (x = 1);
Both will be null
0 & 1 = 0
0 and 1 will be 0
0 && 1 = 0
First operand is false second is true, the same? no = false = 0

But true isn’t always 1 it can be all but 0, or is it?

##### Share on other sites
quote:
Original post by eighty
Often they give the same results but they can absolutely not be used interchangeably.

1 & 2; // 01 && 2; // 1int x = 0;0 & (x = 1); // here x will be 10 && (x = 1); // here x will remain 0

[edited by - eighty on July 8, 2003 4:25:09 AM]

0 & (x = 1);
0 && (x = 1);
Both will be null
0 & 1 = 0
0 and 1 will be 0
0 && 1 = 0
First operand is false second is true, the same? no = false = 0

But true isn’t always 1 it can be all but 0, or is it?

##### Share on other sites
Fruny    1658
The bool true evaluates to 1 when converted to int.
The bool false evaluates to 0 when converted to int.

but

Any non-zero int evaluates to true when converted to bool.
The int zero evaluates to false when converted to bool.

As it happens, && takes two bools and returns a bool.

Note: in operations involving bool and int, the bool is promoted to int, not the other way round. Therefore, true == 2 is false, but true == bool(2) is true.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on July 8, 2003 7:45:48 AM]

##### Share on other sites
Puzzler183    540
BTW, youse use == for comparisons, not =. = always returns true which would mess up code in posts by Boblin, eighty, AP, and Leadorn. Only Fruny managed to do it right.

##### Share on other sites
ToohrVyk    1595
if(x = 0 && x < 5){ // do some code stuff}

If I''m not mistaken, concerning operator precedence, this is equivalent to

if(x=(0&&(x < 5)))

which is, in turn, equivalent to

if(x=0)

which is equivalent to

if(0)

ToohrVyk

##### Share on other sites
Puzzler183,

They wanted to show that the x= assigment would never get called due to the short circuiting and therefore were correct in their examples!

Paul Cunningham

[edited by - PaulCunningham on July 8, 2003 8:33:12 AM]

##### Share on other sites
Wildfire    154
quote:

this checks to see if x is 0 AND if x is less than 5

Judging by the comment Boblin added to his code he meant to write ''==''.

quote:

They wanted to show that the x= assigment would never get called due to the short circuiting and therefore were correct in their examples!

Short circuiting: Evaluate first expression (i.a: x=0). If the result is already determined after that (i.a: For && this is the case when the first expression is false) do not evaluate the second expression.

x=0 is the first operand and thus would be evaluated in any case.

##### Share on other sites
I was referring to this example:

int x = 0;0 & (x = 1); // here x will be 10 && (x = 1); // here x will remain 0

Which shows that they are not interchangeable (due to the short circuiting on &&) and that infact the assignment operator was used correctly to illustrate the point.

Cheers,
Paul Cunningham

Wildfire    154

##### Share on other sites
Fruny    1658
quote:
Original post by Puzzler183
BTW, youse use == for comparisons, not =. = always returns true which would mess up code in posts by Boblin, eighty, AP, and Leadorn. Only Fruny managed to do it right.

nope, = returns a reference to the lvalue you assigned to.
some_int = 0 returns a reference to some_int.
Whether it evaluates to true or false depends on the value assigned.

while( EOF != c = getc() ) { ... } is common enough in C.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

##### Share on other sites

if(x = 2) cout << "Kuk" << endl;
else cout << "fitta" << endl;

output
kuk

if(x = 0) cout << "Kuk" << endl;
else cout << "fitta" << endl;
output
fitta

all but 0 will be true for the if statement.

##### Share on other sites
Nypyren    12063
I prefer using && knowing that I can do:

if (ptr != NULL && ptr->enabled == true)

if (ptr != NULL)
if (ptr->enabled == true)

##### Share on other sites
Guest Anonymous Poster
You could, in fact do

if (ptr && ptr->enabled)

I don''t think I''ve ever compared ==true or ==false. At least not in a year..

##### Share on other sites
jack_1313    536
I need to go through all my code and chang my "if(blah==blah & blah2==blah2)" to the &&

##### Share on other sites
Trienco    2555
quote:
Original post by Anonymous Poster
You could, in fact do

if (ptr && ptr->enabled)

I don''t think I''ve ever compared ==true or ==false. At least not in a year..

or one step further.. when was the last time you were using NULL? not just because sometimes its not defined but also because of jokers hiding a #define NULL 1 in their header files *lol*