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

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -