Is there a printf converter to print in binary format?

前端 未结 30 2752
盖世英雄少女心
盖世英雄少女心 2020-11-21 06:20

I can print with printf as a hex or octal number. Is there a format tag to print as binary, or arbitrary base?

I am running gcc.

printf(\"%d %x %o         


        
30条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 06:35

    const char* byte_to_binary(int x)
    {
        static char b[sizeof(int)*8+1] = {0};
        int y;
        long long z;
    
        for (z = 1LL< 0; z >>= 1, y++) {
            b[y] = (((x & z) == z) ? '1' : '0');
        }
        b[y] = 0;
    
        return b;
    }
    

提交回复
热议问题