C reverse bits in unsigned integer

前端 未结 12 896
一整个雨季
一整个雨季 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 10:57

    You can reverse an unsigned 32-bit integer and return using the following reverse function :

    unsigned int reverse(unsigned int A) {
        unsigned int B = 0;
        for(int i=0;i<32;i++){
            unsigned int j = pow(2,31-i);
            if((A & (1<

    Remember to include the math library. Happy coding :)

提交回复
热议问题