tags:

views:

80

answers:

1

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?

+2  A: 

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
danben
yes danben i have tested problem was that i have not created new project just delete code in the same project and write another code into it so it repeated result from old code
All that means is that you didn't build the project after changing the code (assuming i'm understanding correctly).
Cogwheel - Matthew Orlando
yes it is so now it works thanks