Last active
June 20, 2018 23:44
-
-
Save beiweiqiang/16b367b83a6a4b32af1c1a8d9b8159ef to your computer and use it in GitHub Desktop.
最低有效字节中的位都为 1, 则返回 1, 其他条件下产生 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
* 最低有效字节中的位都为 1, 则返回 1, 其他条件下产生 0 | |
* */ | |
int get_result(int x); | |
int main() { | |
printf("%d \n", get_result(1)); | |
printf("%d \n", get_result(-1)); | |
printf("%d \n", get_result(0)); | |
printf("%d \n", get_result(25645)); | |
printf("%d \n", get_result(0x73)); | |
printf("%d \n", get_result(255)); | |
printf("%d \n", get_result(254)); | |
return 0; | |
} | |
int get_result(int x) { | |
int low_most_value = (1 << 8) - 1; // 注意加 圆括号 | |
// 或者 int low_most_value = 0xff; | |
int right_value = x & low_most_value; | |
return !(right_value ^ low_most_value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment