These are two simple samples in C++ written on Dev-cpp C++ 5.4.2:
float a, b, c;
if (a | b & a | c)
printf("x = %.2f\tF = %.0f\n", x, F);
else
printf("x = %.2f\tF = %.2f\n", x, F);
and this code :
float a, b, c;
if (a || b && a || c)
printf("x = %.2f\tF = %.0f\n", x, F);
else
printf("x = %.2f\tF = %.2f\n", x, F);
Can somebody tell my difference between ||
> |
and &
> &&
. The second code works , but first does not.
And compiler gives an error message :
[Error] invalid operands of types 'float' and 'float' to binary 'operator&'.
The operators |
, &
, and ~
act on individual bits in parallel. They can be used only on integer types. a | b
does an independent OR operation of each bit of a
with the corresponding bit of b
to generate that bit of the result.
The operators ||
, &&
, and !
act on each entire operand as a single true
/false
value. Any data type can be used that implicitly converts to bool
. Many data types, including float
implicitly convert to bool with an implied !=0
operation.
||
and &&
also "short circuit". That means whenever the value of the result can be determined by just the first operand, the second is not evaluated. Example:
ptr && (*ptr==7)
If ptr
is zero, the result is false without any risk of seg faulting by dereferencing zero.
You could contrast that with (int)ptr & (*ptr)
. Ignoring the fact that this would be a bizarre operation to even want, if (int)ptr
were zero, the entire result would be zero, so a human might think you don't need the second operand in that case. But the program will likely compute both anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With