What does 1 & mean

Nov 20, 2011 at 11:27pm
I know that the & before a variable is a reference to another variable. What does & mean in an if statement.
Specifically this one:
if (token[0] == '(' & token[1] == '\0')
Nov 20, 2011 at 11:37pm
closed account (zb0S216C)
You mean bit-wise AND? Here's a link: http://msdn.microsoft.com/en-us/library/z0zec0b2(v=vs.71).aspx

Wazzak
Nov 20, 2011 at 11:56pm
I understand this expression

0 1 0 1 // 5
0 1 1 0 // 6
--------
0 1 0 0 // 4

but how would this work in the context of evaluating an if statement. Since if statements evaluate to true or false it seems like the logical 'and' operator should be there instead.
Nov 20, 2011 at 11:59pm
I think that you are trying to say 'AND' operator. Well if you thought that then you wrote it wrong. Try replacing your ampersand with this '&&' two ampersands. And i really think you thought of and, because your ampersand is totally wrong.
Nov 21, 2011 at 12:09am
I didn't write it wrong because I got it from someone else's source code.
http://www.speqmath.com/tutorials/expression_parser_cpp/index.html
parser.cpp

To put it into a bigger context:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// check if it is a parenthesized expression
    if (token_type == DELIMETER)
    {
        if (token[0] == '(' & token[1] == '\0')
        {
            getToken();
            double ans = parse_level2();
            if (token_type != DELIMETER || token[0] != ')' || token[1] || '\0')
            {
                throw Error(row(), col(), 3);
            }
            getToken();
            return ans;
        }
    }
Nov 21, 2011 at 12:11am
Okay i don't care who you wrote that code from. Just add double ampersands. Because the person you got the code from made a tiny mistake or was very high.
Nov 21, 2011 at 12:18am
closed account (zb0S216C)
Happykiller, we don't know the authors reason for using the bit-wise AND. And for all we know, the bit-wise AND could be intentional, or it could be a simple mistake.

Wazzak
Nov 21, 2011 at 12:20am
& with bools as the operands behaves like &&, except that there is no short-circuit behavior.
Nov 21, 2011 at 1:41am
Thx Athar for clearing things up.

Any reason for using the non short-circuit version of AND?
Nov 21, 2011 at 2:05am
You can use it when your operands have side effects and you need to make sure that all of them are carried out.
You'll get no implicit conversion to bool with &, so using it for that purpose is best avoided.
Nov 21, 2011 at 2:17pm
Thx. That helps a lot
Topic archived. No new replies allowed.