Why is my function returning wrong values?

0

Possible Duplicate:
question 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?

c++
asked on Stack Overflow Jul 12, 2010 by dato datuashvili • edited May 23, 2017 by Community

1 Answer

2

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
answered on Stack Overflow Jul 12, 2010 by danben

User contributions licensed under CC BY-SA 3.0