Jump to content
  • Advertisement

Archived

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

jack_1313

& and &&

This topic is 5492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
&& 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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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; // 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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!