Bitwise AND behaves differently than expected?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int a = 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 a = 50; // 110010
int b = 13; // 001101
if (a & b) {
printf("Hi");
}
return 0;
}
The code above doesn't print anything.
Logically, you would think that a bitwise AND would mean that all the
digits in binary would have to match in order to return true. Instead, in
reality, each digit in binary would have to be different for the condition
to return false.
I don't understand the point of bitwise AND.
I also understand that false is equivalent to 0 in C.
No comments:
Post a Comment