C reverse bits in unsigned integer

前端 未结 12 858
一整个雨季
一整个雨季 2020-11-30 10:18

I\'m converting an unsigned integer to binary using bitwise operators, and currently do integer & 1 to check if bit is 1 or 0 and output, then right shift by 1 to divide

12条回答
  •  渐次进展
    2020-11-30 11:13

    you could move from left to right instead, that is shift a one from the MSB to the LSB, for example:

    unsigned n = 20543;
    unsigned x = 1<<31;
    while (x) {
        printf("%u ", (x&n)!=0);
        x = x>>1;
    }
    

提交回复
热议问题