1) Why there is no assignment by logical operator like there is assignment by sum and difference?
bool a = true;
bool b = false;
a = a || b;
a ||= b; // syntax error!
a |= b; // OK.
2) What is the meaning of applying bitwise operator on boolean variable?
Is it the same as using logical operator?
It's true that &&= and ||= are "missing" from C. I think one reason is that logical AND and OR in C perform short-circuiting, which would be a little strange in the abbreviated form. But don't use the bitwise assignment operators in their place. Instead, just write:
a = a && b;
c = c || d;
The bitwise operators will work if you have canonical true/false values (1 and 0). But if applied to non-canonical values, such as 5 and 2, you will get different results (5 && 2 is 1, but 5 & 2 is 0).
a |= b means the same as a = (a | b), except that the address of a is evaluated only once. Then you look up the rules for promotion, and for assignment to bool.
Related
Why C has both || and | operators? As far I know, | operator can replace || in conditions because it will return true (nonzero) value when at least one of operands is nonzero.
I ask just out of my curiosity. I know I should use || for logical expressions.
Example
#include <stdio.h>
int main(void) {
int to_compare = 5;
/* Try with bitwise or */
if ((5 > to_compare) | (to_compare == 6)) {
printf("‘to_compare’ is less than or equal to 5 or equal to 6.\n");
}
/* Try with logical or */
if ((5 > to_compare) || (to_compare == 6)) {
printf("‘to_compare’ is less than or equal to 5 or equal to 6.\n");
}
return 0;
}
|| and | are very different beasts.
Aside from || having the short-circuting property (the right operand is only evaluted if the left one evaluates to 0), it's also a sequencing point.
The value of the expression can also be different: 1 || 2 for example is 1 whereas 1 | 2 is 3.
(Note that && and & have a more pernicious difference, for example 1 && 2 is 1 whereas 1 & 2 is 0.)
In addition to the fact that the || operator is short-circuiting, the result of the || operator is always either 0 or 1 based on its truth, whereas the result of the bitwise or | operator will be a combination of bits that were set in the operands, which is not necessarily 1 (i.e. 0x0A | 0xB0 = 0xBA, whereas 0x0A || 0xB0 = 1.
§6.5.14 Logical OR operator
The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.
§6.5.12 Bitwise inclusive OR operator
The result of the | operator is the bitwise inclusive OR of the operands (that is, each bit in
the result is set if and only if at least one of the corresponding bits in the converted
operands is set).
In BCPL and B -- C ancestors --, there is only | and &. But their interpretation is dependent on the context: in control structures like if, they behaved like the C logical operators, in other contexts, they behaved like the C binary operators. That was deemed too difficult to use and explain and thus additional operators where introduced so that the operator used indicated clearly if the operation was logical and short-circuiting, or binary and not short-circuiting. And that also explains the inconvenient relative priorities with comparison operators.
What is the difference between & and && in C?
My teacher gave me this example:
int a = 8;
int b = 4;
printf("a & b = %d\n", a & b);
printf("a && b = %d\n", a && b);
Output:
a & b = 0;
a && b = 1;
I'm not sure why this would return true in one scenario and false in another.
& is bitwise and and && is logical and.
The expression x && y will return 1 if both x and y is non-zero, and 0 otherwise. Note that if x is zero, then y will not be evaluated at all. This will matter if y is an expression with side effects. This behviour is called short circuiting.
The expression x & y will perform a bitwise operation on each individual bit in x and y. So if x is 1010 in binary and y is 1100 then x & y will evaluate to 1000. Note that the return value of x & y should NOT be interpreted as a Boolean value, even if it's possible. In early C, the operator && did not exist, and because of that & was used for this purpose.
One way to explain it is that you could imagine that & is the same thing as applying && on each individual bit in the operands.
Also note that & has lower precedence than &&, even though intuition says that it should be the other way around. This also goes for comparison operators, like <, <=, ==, !=, >=, >. This goes back to the time when C did not have the operators && and || and the bitwise versions was used instead. At this time, it made sense, but when the logical operators were added, it did not anymore. Kernighan and Ritchie admitted that it would have made more sense, but they did not fix it because this would break existing code.
I'm not sure why this would return true in one scenario and false in another.
The return value from x & y should not be treated as a Boolean value at all. However, it can (depending on how the code is written) be treated as a Boolean array. If you have two integers, flags1 and flags2 then the result of flags1 & flags2 will denote which flags that are toggled in both flags1 and flags2.
The & operator performs a bit-wise and operation on its integer operands, producing an integer result. Thus (8 & 4) is (0b00001000 bitand 0b00000100) (using a binary notation that does not exist in standard C, for clarity), which results in 0b00000000 or 0.
The && operator performs a logical and operation on its boolean operands, producing a boolean result. Thus (8 && 4) is equivalent to ((8 != 0) and (4 != 0)), or (true and true), which results in true.
&& (logical and operator) - The left and right operands are boolean expressions. If both the operands are non-zero, then the condition becomes true.
>
& (bitwise and operator) - The left and right operands are integral types. Binary AND Operator copies a bit to the result if it exists in both operands.
In your teacher's example a && b, the left operand 4 and the right operand 8 are both non-zero. So the condition will become true.
In your teacher's other example a & b, the left operand 4 or 0100 and the right operand 8 or 01000 copies no bits to the result. This is because there are no common set bits in either operand.
& is bitwise operator and, && is logical for example if you use two number and you want to use bitwise operator you can write & .
if you want to use to phrase and you want to treat them logically you can use && .
I tried running the following code in C:
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
I got the output as d = 1. Can someone please explain to me what happens when we use == like this?
§6.5.9 (== and !=)-http://c0x.coding-guidelines.com/6.5.9.html
The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.)Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int.
For any pair of operands, exactly one of the relations is true.
So here as b+c is equal to a as both has value 10 therefore it yields 1.
Because b + c is executed first, and after is evaluate comparison with == operator.
In c, addition has higher precedence than ==, so it adds b and c before comparing the result to a, since it is true it results in 1, if it was false it would result in 0.
== is the equal-to operator. It returns 1 if the two sides are equal and 0 otherwise.
It is known that both assignment = and conditional ?: operators have right associativity. In following code sample:
#include <stdio.h>
int main(void)
{
int a, b, c, d;
a = b = c = d = 1;
1 ? a++ : b ? c++ : d;
printf("%d %d %d %d\n", a, b, c, d);
return 0;
}
the assignment:
a = b = c = d = 1;
is equivalent to:
a = (b = (c = (d = 1)));
and correspondingly:
1 ? a++ : b ? c++ : d;
is the same as:
1 ? a++ : (b ? c++ : d);
What the Standard say about this last case? Does it guarantee that such combined expression is evaluated from left to right (so the c++ part is not evaluated), just as opposite to assignment?
The evaluation order of ?: is guaranteed: the first operand is evaluated first, then evaluate the second or the third operand depending on whether the first operand is true.
You are mainly confused about the relationship between operator precedence/associativity and order of evaluation. They play different roles. The former decides how operators are grouped, while the latter decides which sub-expression is evaluated first.
Consider the expression a * b + c * d, the precedence rule means it's equivalent to (a * b) + (c * d). But is it guaranteed that the compiler will evaluate a * b before c * d? The answer is no, in this example, the operator + doesn't guarantee the order of evaluation.
The conditional operator ?: is one of the few operators that do have a specified order of evaluation. (The rest are &&, ||, and ,).
In your example
1 ? a++ : b ? c++ : d;
1 ? a++ : (b ? c++ : d);
are always equivalent, in both expressions, 1 is evaluated first, and since it's true, a++ is evaluated next, the end.
Associativity and precedence do not define order of evaluation. These concepts are completely unrelated. Order of evaluation in C is defined by sequencing rules, not by precedence or associativity.
It is true that a = b = c = d = 1; is associated as a = (b = (c = (d = 1)));, but that does not mean that d = 1 should be evaluated first, especially in C language, where assignment operator evaluates to an rvalue.
Associtivity simply says that c should receive value 1 converted to the type of d ("as if" it was read from d). But that does not mean that d = 1 should be done first. In your example all variables have the same type, which means that the whole thing is equivalent to a = 1; b = 1; c = 1; d = 1; in absolutely any order. There's no sequencing inside a = b = c = d = 1 expression.
The same logic applies to ?: operator. Its associativity simply tells you which operand belongs to which operator. And the grouping is indeed 1 ? a++ : (b ? c++ : d);. But associativity does not tell you anything about the order of evaluation. Order of evaluation in ?: operator is defined separately and independently: the condition is always evaluated (sequenced) first, then one (and only one) of the branches is evaluated. In your example 1 is evaluated first, then a++ is evaluated and its result becomes the result of the entire expression. The (b ? c++ : d) part is not even touched.
1 ? a++ : b ? c++ : d;
is equivalent to
if (1) {
a++;
}
else {
if (b) {
c++;
}
else {
d;
}
}
So, the output will be
2 1 1 1
using only bitwise operators (|, &, ~, ^, >>, <<), is it possible to replace the != below?
// ...
if(a != b){
// Some code
}
/// ...
this is mainly out of self interest, since I saw how to do it with == but not !=.
if(a ^ b) {
//some code
}
should work.
You can also use your preferred method for == and add ^ 0xFFFFFFFF behind it (with the right amount of Fs to match the length of the datatype). This negates the value (same as ! in front of it).
a != b means that there is at least one different bit in the bit representations of a and b. The XOR bit operator returns 1 if both input bit operands are different, 0 otherwise.
So, you can apply a XOR operation to a and b and check if the result is not equal to zero.
A bitwise version of the '!=' test could look something like:
if((a - b) | (b - a)) {
/* code... */
}
which ORs the two subtractions. If the two numbers are the same, the result will be 0. However, if they differ (aka, the '!=' operator) then the result will be 1.
Note: The above snippet will only work with integers (and those integers should probably be unsigned).
If you want to simulate the '==' operator, however, check out Fabian Giesen's answer in Replacing "==" with bitwise operators
x ^ y isn't always sufficient. Use !!(x ^ y). Values expecting a one bit return value will not work with x ^ y since it leaves a remainder that could be greater than just 1.
Yes, using this:
if (a ^ b) { }
"~" is equaled to NOT so that should work. example would be "a & ~b".