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);
    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
", 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
", 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
", bits);
    return 0;
}

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14791985.html