& and &&

Started by
19 comments, last by jacksaccountongamedev 20 years, 9 months ago
C++. What is the difference between a single & and &&? I personaly always use &, but I often see people using &&. Why?
Advertisement
& = 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]
&& 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
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Often they give the same results but they can absolutely not be used interchangeably.


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


[edited by - eighty on July 8, 2003 4:25:09 AM]
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)
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?
quote:Original post by eighty
Often they give the same results but they can absolutely not be used interchangeably.


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


[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?
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.

Draw your conclusions.

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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.

This topic is closed to new replies.

Advertisement