c语言中输出int型在内存中的占位

c语言中输出不同数据类型在内存中的占位

1、

#include <stdio.h>

int main(void)
{
    int n = 0;
    unsigned x = ~0U;
    while(x)
    {
        if(x & 1U)
            n++;
        x >>= 1;
    }
    printf("the number of bits: %u\n", n);
    return 0;
}

 

2、

#include <stdio.h>

int main(void)
{
    int bits = 0;
    unsigned x = ~(0 & 1);
    while(x)
    {
        if(x & 1U)
            bits++;
        x >>= 1;
    }
    
    printf("the result:   %u\n", bits);
    return 0;
}

 

 

3、

#include <stdio.h>

int main(void)
{
    int bits = 0;
    unsigned long x = ~0U;
    while(x)
    {
        if(x & 1U)
            bits++;
        x >>= 1;
    }
    
    printf("the result:   %d\n", bits);
    return 0;
}

 

 

4、

#include <stdio.h>

int main(void)
{
    int bits = 0;
    unsigned long x = ~(0 & 5);
    while(x)
    {
        if(x & 1U)
            bits++;
        x >>= 1;
    }
    
    printf("the result:   %d\n", bits);
    return 0;
}

 

posted @ 2021-05-20 23:05  小鲨鱼2018  阅读(274)  评论(0编辑  收藏  举报