In C, is there a shorthand for boolean and (&&)?

Sometimes I want to do something like this (with i and j - ints).

(if i==4 && j==9) { ... } 

Where will it go through the brackets if I equals 4 and j equals 9. I used one ampersand (&) instead of double, and my code compiled and worked.

Does this do the same thing as double ampersand & &, and if not what he was doing?

Edit: Oh, and I did the same with or using '|' instead of '||'

+4
source share
4 answers

Presumably, you mean if (i==4 && j==9) .

Under the circumstances, the change in this parameter from && to & should not change much. The big thing that will change is that with && value j==9 will only be evaluated if part i==4 was true, but with & they will both be evaluated independently.

If you have something like if (x != NULL && x->whatever ...) , you want to make sure that the second part (these markups x ) is evaluated only if x not a null pointer. In your case, however, comparing what seems to int is unlikely to cause any problems.

You may also run into a problem when you are dealing with something that could lead to a value other than 1 to a true signal. Again, this is not a problem, because == will always produce either 0 or 1 . If (for example) you used isalpha , islower , etc., from <ctype.h> , they should only create 0 or non-zero values. If you combine those that have & , you get a bit-wise or , which could produce 0 for two non-zero inputs (for example, 1 & 2 == 0 , but 1 && 2 == 1 ).

When you use the bitwise and result from == , you will get 0 & 0 or 0 & 1 or 1 & 0 or 1 & 1 . 1 & 1 will give 1 (true). Everyone else will give 0 (false) --- exactly the same as && .

+9
source

Performs bitwise and .

What is it, the expression i == 4 equivalent to 1 if i is 4 and the same for RHS (with j and 9 , obviously). The & operator returns a number in which both operands have this bit.

It works the same as && , because 00000001 & 00000001 (e.g. decrementing to byte) is the same. If the value is 0 (the condition was false), then & will not display two bits for both operands, and 00000000 - 0 , which is false.

However , do not just use & , because it is one character shorter or similar. Use it because it expresses what you want to achieve. If this is logical and you want, use && .

The same applies to | , except instead of the resulting bit, if each operand is turned on, it turns it on if one of the operands turned on this bit.

+5
source

One ampersand performs bitwise I. Each bit of the result is set only if both operands have 1 in this position.

Since comparisons in C return 1 for true and 0 for false, & will give the same results as && , while both operands are compared. But for arbitrary values, it will return seemingly random results. 1 && 2 true, but 1 & 2 is false because the binary representations 1 and 2 do not have common bits.

+2
source

One ampersand is called bitwise. This is a binary operator that is "ands" two numbers beaten.

For example, if you have two binary numbers: 01100010 and 00011111, your bitwise and will lead to 00000010

i == 4 returns 1 (true), as does j == 9. So, i == 4 and j == 9 are really 1 and 1, which is 1 (true).

Try these examples to see the difference.

1) int k = 4 and 6; vs int k = 4 && 6;

2) if (i == 4 & 2) vs, if (i = 4 and 2)

0
source

All Articles