如何找到前导数字零的若干使用C [英] How To Find The Leading Number Of Zero's In a Number using C

查看:105
本文介绍了如何找到前导数字零的若干使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有64号,那么它的二进制重新presentation将是0000 0000 0000 0000 0000 0000 0100 0000零的是25,领先的数字。 记得我算算这在O(1)时间。

for example,if i have number 64,then its binary representation would be 0000 0000 0000 0000 0000 0000 0100 0000 so leading number of zero's is 25. remember i have to calculate this in O(1) time.

请告诉我正确的方式做that.even,如果你的复杂度> O(1)请发表您的答案。感谢名单

please tell me the right way to do that.even if your complexity is >O(1) please do post your answer. thanx

推荐答案

右移是你的朋友。

    int input = 64;
    int sample = ( input < 0 ) ? 0 : input;
    int leadingZeros = ( input < 0 ) ? 0 : 32;

    while(sample) {
        sample >>= 1;
        --leadingZeros;
    }
    printf("Input = %d, leading zeroes = %d\n",input, leadingZeros);

这篇关于如何找到前导数字零的若干使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆