#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;
}
上面的代碼打印出嗨。按位與行爲與預期不同?
#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;
}
上面的代碼不打印任何東西。
從邏輯上講,你會認爲按位AND意味着二進制中的所有數字都必須匹配才能返回true。實際上,二進制中的每個數字必須不同才能返回false。
我不明白點位AND。
我也明白,假相當於0℃。
谷歌搜索 「位掩碼」 可能的幫助。 –
邏輯上,如果二進制數字中的所有數字都匹配,那麼與'a == b'沒有區別。 – antak