二分查找32位整数的前导0个数

参考自Matrix67!

//二分查找32位整数的前导0个数
//二分查找思想
#include <stdio.h>

int main() {
unsigned int n;
while (scanf("%u", &n) != EOF) {
if (n == 0) printf ("32\n");
else {
int s = 1;
if ((n >> 16) == 0) {s += 16; n <<= 16;}
if ((n >> 24) == 0) {s += 8; n <<= 8;}
if ((n >> 28) == 0) {s += 4; n <<= 4;}
if ((n >> 30) == 0) {s += 2; n <<= 2;}
s -= (n >> 31);
printf ("%d\n", s);
}
}
return 0;
}

 

posted on 2012-04-05 12:10  Try86  阅读(631)  评论(0编辑  收藏  举报