gpt4 book ai didi

c++ - 为什么我的函数返回错误值?

转载 作者:行者123 更新时间:2023-11-28 03:56:49 25 4
gpt4 key购买 nike

<分区>

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 时返回相同的结果,为什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com