<分区>
Possible Duplicate:
question about leading zeros
如 stackoverflow.com/questions/3232534/question-about-leading-zeros .
尾随零的数量,来自 Hacker's Delight 的二进制搜索:
#include <iostream>
using namespace std;
int ntz(unsigned x){
int n;
if ( x==0) return 32;
n=1;
if ((x & 0x0000FFFF))==0) {n=n+16; x=x>>16;}
if ((x & 0x000000ff)==0) {n=n+8;x>>=8;}
if ( x &0x0000000F)==0) {n=n+4; x>>=4;}
if ((x & 0x00000003)==0) { n=n+2; x>>=2;}
return n-(x &1);
}
int main(){
unsigned x;
cin>>x;
cout<<ntz(x)<<endl;
return 0;
}
当我输入 8 时返回 8,当我输入 9 时返回相同的结果,为什么?