Here is code which returns number of leading zeros from Hacker's Delight book:
#include <iostream>
using namespace std;
int nlz(unsigned x) {
int n;
if (x == 0) return(32);
n = 1;
if ((x >> 16) == 0) {n = n +16; x = x <<16;}
if ((x >> 24) == 0) {n = n + 8; x = x << 8;}
if ((x >> 28) == 0) {n = n + 4; x = x << 4;}
if ((x >> 30) == 0) {n = n + 2; x = x << 2;}
n = n - (x >> 31);
return n;
}
int main(){
int x;
cin>>x;
cout<<nlz(x)<<endl;
return 0;
}
and when I enter number 8 it return 8 and is it correct maybe it should return 3 yes?
8//1000