c - Bitwise AND behaves differently than expected? -
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int = 50; // 110010 int b = 30; // 011110 if (a & b) { printf("hi"); } return 0; }
the code above prints hi.
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int = 50; // 110010 int b = 13; // 001101 if (a & b) { printf("hi"); } return 0; }
the code above doesn't print anything.
logically, think bitwise , mean digits in binary have match in order return true. instead, in reality, each digit in binary have different condition return false.
i don't understand point of bitwise and.
i understand false equivalent 0 in c.
this purpose of bitwise and. it's used bit testing masks. net effect keep common 1s , 0 out else. say, instance want test if 3rd bit 1, can write
if ( & 4 /*0100*/ ) //
as karthik said there other methods compare operands way expected.
Comments
Post a Comment