开发者

Why is my function returning wrong values? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

q开发者_开发技巧uestion about leading zeros

As in stackoverflow.com/questions/3232534/question-about-leading-zeros.

Number of trailing zeros, binary search from 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;
}

When i enter 8 it return 8 and when I enter 9 the same result why?


Firstly, your code doesn't compile. The parentheses in lines 9 and 11 are not balanced correctly.

That said, after fixing the errors and compiling, I get the following results:

$ ./a.out 
8
3

$ ./a.out 
9
0
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜