What is the difference between bitwise and logical operators inside conditional statements in C? -
there many questions on net refer differences between bitwise , logical operators. hoping have done search, none of them specialize whether same or not when used inside conditional statements nor refer exclusively c language. majority referred c++ , c# , not know if same answers applicable c language too.
this example code wrote test going on:
// difference between logical && , bitwise & // #include <stdio.h> #define true 123>45 #define false 4>2342 void print_tt(int table[][4]); int main(void) { int and_tt[2][4]; // , truth table int or_tt[2][4]; // or truth table // create truth table logical , bitwise , operator in 1 2d array and_tt[0][0] = true && true ? 1 : 0; and_tt[0][1] = true && false ? 1 : 0; and_tt[0][2] = false && true ? 1 : 0; and_tt[0][3] = false && false ? 1 : 0; and_tt[1][0] = true & true ? 1 : 0; and_tt[1][1] = true & false ? 1 : 0; and_tt[1][2] = false & true ? 1 : 0; and_tt[1][3] = false & false ? 1 : 0; // create truth table logical , bitwise or operator in 1 2d array or_tt[0][0] = true || true ? 1 : 0; or_tt[0][1] = true || false ? 1 : 0; or_tt[0][2] = false || true ? 1 : 0; or_tt[0][3] = false || false ? 1 : 0; or_tt[1][0] = true | true ? 1 : 0; or_tt[1][1] = true | false ? 1 : 0; or_tt[1][2] = false | true ? 1 : 0; or_tt[1][3] = false | false ? 1 : 0; puts("_______and_______"); puts("logical bitwise"); print_tt(and_tt); puts("_______or________"); puts("logical bitwise"); print_tt(or_tt); } // prints truth table of bitwise , logical operator given side side void print_tt(int table[][4]) { int i; for(i=0; i<4 ; ++i) { printf("%-10s%s\n", table[0][i] ? "true" : "false", table[1][i] ? "true" : "false"); } }
the program’s output is:
_______and_______ logical bitwise true true false false false false false false _______or________ logical bitwise true true true true true true false false
which proves there no differences between bitwise , logical operators. changing definition of true
, false
macros include remaining comparison operators, 1 can see there no difference again.
therefore, if there differences, these might associated way compiler interprets statement or efficiency of code.
in conclusion, in specific case when have bitwise or logical operator between 2 or more results of comparison operation inside conditional statement, of 2 should use, greater efficiency ?
you're checking values 0
, 1
. try other values , you'll see differences.
int = 4, b = 2; puts(a && b ? "true" : "false"); puts(a & b ? "true" : "false");
this prints:
true false
bitwise operators only work integers. logical operators can used pointers, floating point numbers, , other non-integral types.
there's short-circuiting. logical operators won't evaluate second operand if first enough.
int a() { puts("a"); return 0; } int b() { puts("b"); return 1; } int main() { puts(a() && b() ? "true" : "false"); puts("---"); puts(a() & b() ? "true" : "false"); }
this prints:
a false --- b false
notice how b
printed when using &
. there no short-circuiting &
calls both functions, whereas &&
calls a()
.
and on subtler note, unlike &&
, &
not impose order of evaluation on operands. output equally have a
, b
printouts reversed.
a false --- b false
if put all of these differences aside, yes, operators equivalent. in case, do not worry efficiency. use whichever operators semantically correct: logical ones.
(if helps ease mind, there no difference in efficiency. compilers smart , emit optimal bytecode evaluate these expressions, whichever operator use.)