C reverse bits in unsigned integer

前端 未结 12 854
一整个雨季
一整个雨季 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:17

    I came up with a solution which dosesn't involve any application of bitwise operators. it is inefficient in terms of both space and time.

    int arr[32];
    for(int i=0;i<32;i++)
    {
        arr[i]=A%2;
        A=A/2;
    }
    double res=1;
    double re=0;
    for(int i=0;i<32;i++)
    {
        int j=31-i;
        res=arr[i];
        while(j>0)
        {
            res=res*2;
            j--;
        }
        re=re+res;
    }
    cout<<(unsigned int )re;
    

提交回复
热议问题