C经典笔试题之统计一个无符号数二进制形式的0和1的个数

目录

 

统计一个无符号数中的1的个数

方法一:

方法二:

统计一个无符号数中的0的个数

实现方法:

测试

测试代码

运行结果


  • 统计一个无符号数中的1的个数

  • 实现方法一:

/*方法一: 
 *
 * eg:
 *
 *   9--> 1001
 *
 *   &
 *-----------------------
 * 9-1--> 1000
 *
 *-----------------------
 *        1000 
 *一次类推,直到:num = 0,为止.
 * 
 * */

/**************************************************************************************
 *  Description: To info the num of 1 in an unsigned number.
 *   Input Args: An unsigned number.
 *  Output Args: No.
 * Return Value: The num of 1 in the unsigned  number.
 *************************************************************************************/

int Count_One1 (unsigned int num)
{
    if (num < 0)
        return -1;

    static int count = 0;
    for (count = 0; num != 0; count++)
    {
        num &= num - 1;
    }

    return count;
} /* ----- End of Count_One()  ----- */
  • 实现方法二:

/*方法二: 
 * 因为>>n,相当于/2^n
 * 即也可以使用num / 2来代替num >> 1;
 * eg:
 *
 *   D ---> B 
 *   9 ---> 1001 
 *          &
 *          0001
 *---------------------
 *          0001 
 *---------------------
 *依次右移一位后&1:
 *---------------------
 *   9>>1 --> 0100
 *            &       
 *            0001
 *   ------------------
 *            0000
 *
 **********************
 *
 *   9>>1 --> 0010
 *            &
 *            0001
 *   ---------------
 *            0000 
 **********************
 *
 *   9>>1 --> 0000
 *            &
 *            0001 
 *   -----------------
 *            0001
 *----------------------
 *依次与1做与运算,直到:num = 0,为止.
 * 
 * */


/**************************************************************************************
 *  Description: To info the num of 1 in an unsigned number.
 *   Input Args: An unsigned number.
 *  Output Args: No.
 * Return Value: The num of 1 in the unsigned  number.
 *************************************************************************************/
int count_One2(unsigned int num)
{
    static int count = 0;
    while ( num )
    {
        // 当最后一位为1时,则加1 
        if( (num & 1) ){
            ++count;
        }
        num >>= 1;
    }
    return count;
} /* ----- End of count_one()  ----- */


  • 统计一个无符号数中的0的个数

  • 实现方法:

/*方法: 
 * 因为>>n,相当于/2^n
 * 即也可以使用num / 2来代替num >> 1;
 *
 * eg:
 *
 *   D ---> B 
 *   9 ---> 1001 
 *          &
 *          0001
 *---------------------
 *          0001
 *          !(0001) = 0000
 *
 *---------------------
 *依次右移一位后&1:
 *---------------------
 *   9>>1 --> 0100
 *            &       
 *            0001
 *   ------------------
 *            0000 
 *            !(0000) = 1111
 *
 **********************
 *
 *   9>>1 --> 0010
 *            &
 *            0000
 *   ---------------
 *            0000 
 *            !(0000) = 1111
 **********************
 *
 *   9>>1 --> 0000
 *            &
 *            0001 
 *   -----------------
 *            0000
 *            !(0000) = 1111
 *----------------------
 *依次与1做与运算之后在取反,直到:num = 0,为止.
 * 
 * */


/**************************************************************************************
 *  Description: To info the num of 0 in an unsigned number.
 *   Input Args: An unsigned number.
 *  Output Args: No.
 * Return Value: The num of 0 in the unsigned  number.
 *************************************************************************************/


int count_Zero(unsigned int num)
{
    static int count = 0;
    while ( num )
    {
        // 当最后一位为1时,则加1 
        if( !(num & 1) ){
            ++count;
        }
        num >>= 1;
    }
    return count;
} /* ----- End of count_Zero()  ----- */
                 

  • 测试

  • 测试代码

    /*********************************************************************************
     *      Copyright:  (C) 2020 ysn
     *                  All rights reserved.
     *
     *       Filename:  Info_zero_one_num.c
     *    Description:  This file is to info a unsigned num 0/1 's count.
     *                 
     *        Version:  1.0.0(24/07/20)
     *         Author:  tianjincheng <473892093@qq.com>
     *      ChangeLog:  1, Release initial version on "24/07/20 14:38:42"
     *                 
     ********************************************************************************/
    
    #include <stdio.h>
    
    /*方法一: 
     *
     * eg:
     *
     *   9--> 1001
     *
     *   &
     *-----------------------
     * 9-1--> 1000
     *
     *-----------------------
     *        1000 
     *一次类推,直到:num = 0,为止.
     * 
     * */
    
    /**************************************************************************************
     *  Description: To info the num of 1 in an unsigned number.
     *   Input Args: An unsigned number.
     *  Output Args: No.
     * Return Value: The num of 1 in the unsigned  number.
     *************************************************************************************/
    
    int Count_One1 (unsigned int num)
    {
        if (num < 0)
            return -1;
    
        static int count = 0;
        for (count = 0; num != 0; count++)
        {
            num &= num - 1;
        }
    
        return count;
    } /* ----- End of Count_One()  ----- */
    
    
    /*方法二: 
     * 因为>>n,相当于/2^n
     * 即也可以使用num / 2来代替num >> 1;
     * eg:
     *
     *   D ---> B 
     *   9 ---> 1001 
     *          &
     *          0001
     *---------------------
     *          0001 
     *---------------------
     *依次左移一位后&1:
     *---------------------
     *   9>>1 --> 0100
     *            &       
     *            0001
     *   ------------------
     *            0000
     *
     **********************
     *
     *   9>>1 --> 0010
     *            &
     *            0001
     *   ---------------
     *            0000 
     **********************
     *
     *   9>>1 --> 0000
     *            &
     *            0001 
     *   -----------------
     *            0001
     *----------------------
     *依次与1做与运算,直到:num = 0,为止.
     * 
     * */
    
    
    /**************************************************************************************
     *  Description: To info the num of 1 in an unsigned number.
     *   Input Args: An unsigned number.
     *  Output Args: No.
     * Return Value: The num of 1 in the unsigned  number.
     *************************************************************************************/
    int count_One2(unsigned int num)
    {
        static int count = 0;
        while ( num )
        {
            // 当最后一位为1时,则加1 
            if( (num & 1) ){
                ++count;
            }
            num >>= 1;
        }
        return count;
    } /* ----- End of count_one()  ----- */
    
    
    
    /*方法: 
     * 因为>>n,相当于/2^n
     * 即也可以使用num / 2来代替num >> 1;
     *
     * eg:
     *
     *   D ---> B 
     *   9 ---> 1001 
     *          &
     *          0001
     *---------------------
     *          0001
     *          !(0001) = 0000
     *
     *---------------------
     *依次左移一位后&1:
     *---------------------
     *   9>>1 --> 0100
     *            &       
     *            0001
     *   ------------------
     *            0000 
     *            !(0000) = 1111
     *
     **********************
     *
     *   9>>1 --> 0010
     *            &
     *            0000
     *   ---------------
     *            0000 
     *            !(0000) = 1111
     **********************
     *
     *   9>>1 --> 0000
     *            &
     *            0001 
     *   -----------------
     *            0000
     *            !(0000) = 1111
     *----------------------
     *依次与1做与运算之后在取反,直到:num = 0,为止.
     * 
     * */
    
    
    /**************************************************************************************
     *  Description: To info the num of 0 in an unsigned number.
     *   Input Args: An unsigned number.
     *  Output Args: No.
     * Return Value: The num of 0 in the unsigned  number.
     *************************************************************************************/
    int count_Zero(unsigned int num)
    {
        static int count = 0;
        while ( num )
        {
            // 当最后一位为1时,则加1 
            if( !(num & 1) ){
                ++count;
            }
            num >>= 1;
        }
        return count;
    } /* ----- End of count_Zero()  ----- */
    
    
    int main (int argc, char **argv)
    {
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
    
        count1 = Count_One1(2020);
        count3 = count_One2(2020);
        count2 = count_Zero(2020);
        printf("way1:count of 1: %d\n", count1);
        printf("way2:count of 1: %d\n", count3);
        printf("count of 0: %d\n", count2);
        return 0;
    } /* ----- End of main() ----- */
    
    
    

     

  • 运行结果

 

代码仓库:源码仓库链接

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiaoCheng'Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值