Created
June 20, 2018 23:58
-
-
Save beiweiqiang/5ed8f538d56da2a1775789d154d67e72 to your computer and use it in GitHub Desktop.
最高有效字节中的位都为 0, 则返回 1, 其他条件下产生 0
This file contains 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> | |
/* | |
* 最高有效字节中的位都为 0, 则返回 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)); | |
printf("%d \n", get_result(0x0)); | |
printf("%d \n", get_result(0xfeffffff)); | |
printf("%d \n", get_result(0x00ffffff)); | |
return 0; | |
} | |
int get_result(int x) { | |
int shift = (sizeof(int) - 1) << 3; | |
int right_value = (x >> shift) & 0xff; | |
return !(right_value & 0xff); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment