zoukankan      html  css  js  c++  java
  • *** 自写代码:计算一个字节中有多少位是1

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    #define BIT(n) (0x1<<n)
    int calculateBits (unsigned char c)
    {
        int count = 0;
        int bits = sizeof(c)*8;
        for (int i=0; i<bits; i++)
        {
            if (c & BIT(i)) count++;
        }
        return count;
    }
    void showBits (unsigned char c)
    {
        printf ("Input:0x%x  ", c);
        int bits = sizeof(c)*8;
        for (int i=bits-1; i>=0; i--)
        {
            if (c & BIT(i))
                cout << "1";
            else
                cout << "0";
        }
        cout <<"b" << endl;
    }
    int main(int argc, char * agrv[])
    {
        unsigned char c = 0;
        cout << "Please input a unsigned char in hex: " << endl;
        scanf ("%x", &c);
        showBits(c);
        cout << "bits of 1: " << calculateBits(c) << endl;
        return 0;
    }
  • 相关阅读:
    P、NP、NPC、NPH问题介绍
    过河卒 bfs搜索
    对迪杰斯特拉算法的理解
    第七周
    周作业
    月考一
    第四周
    第三周
    第二周作业
    46期第一次作业
  • 原文地址:https://www.cnblogs.com/superrunner/p/10165170.html
Copyright © 2011-2022 走看看