C printing bits

前端 未结 5 1839
死守一世寂寞
死守一世寂寞 2020-12-03 15:14

I am trying to write a program in C that prints bits of int. for some reason i get wrong values,

void printBits(unsigned int num){
    unsigned int size = si         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 15:33

    To address point two, I'd consider the following, which is simplified a bit for ease of understanding.

    void printBits(unsigned int num)
    {
       for(int bit=0;bit<(sizeof(unsigned int) * 8); bit++)
       {
          printf("%i ", num & 0x01);
          num = num >> 1;
       }
    }
    

提交回复
热议问题