写了个位运算

#include<stdio.h>
#define SQR(x) (x*x)
inline int function_SQR(int x) { return x*x;}
#define MASK1 0x01
#define MASK2 0x01<<1
#define MASK3 0x01<<2
#define MASK4 0x01<<3
#define MASK5 0x01<<4
#define MASK6 0x01<<5
#define MASK7 0x01<<6
#define MASK8 0x01<<7
#define MASK2 0x01<<8

#define FUNCTION_BIT_TEST(x,mask)  (x)&(mask)


int Function_CountNumber(unsigned short int a );
int Function_CountNumber(unsigned short int a)
{
    int count = 0;
    bool result;
    result = FUNCTION_BIT_TEST(a,MASK1);
    if(result)
        count++;
    result = FUNCTION_BIT_TEST(a,MASK2);
    if(result)
        count++;
    result = FUNCTION_BIT_TEST(a,MASK3);
    if(result)
        count++;
    result = FUNCTION_BIT_TEST(a,MASK4);
    if(result)
        count++;
    result = FUNCTION_BIT_TEST(a,MASK5);
    if(result)
        count++;
    result = FUNCTION_BIT_TEST(a,MASK6);
    if(result)
        count++;
    result = FUNCTION_BIT_TEST(a,MASK7);
    if(result)
        count++;
    result = FUNCTION_BIT_TEST(a,MASK8);
    if(result)
        count++;
    printf("number of 1: %d\n",count);
    return count;
}
int main()
{
    unsigned short int   c =  0;
    printf("size of short int %d",sizeof(short));
    while(1)
    {    

        printf("input c\n");
        fflush(stdin); 
        scanf("%d",&c);
        Function_CountNumber(c);

    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值