How to print bits in c

前端 未结 5 1138
礼貌的吻别
礼貌的吻别 2021-01-21 10:30

I\'m writing a function to print bits in c, I\'m only allowed to use write function. my function doesn\'t work for other numbers.

void    print_bit         


        
5条回答
  •  后悔当初
    2021-01-21 11:01

    I have re-written the code from Is there a printf converter to print in binary format?

    void    print_bits(unsigned char octet)
    {
        int z = 128, oct = octet;
    
        while (z > 0)
        {
            if (oct & z)
                write(1, "1", 1);
            else
                write(1, "0", 1);
            z >>= 1;
        }
    }
    

提交回复
热议问题