superrunner
#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;
}

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2021-12-02
  • 2021-06-08
  • 2022-02-25
猜你喜欢
  • 2021-09-27
  • 2021-11-04
  • 2021-12-06
  • 2021-08-06
  • 2021-12-03
  • 2022-01-17
相关资源
相似解决方案