Software & Finance





Visual C++ - Reverse Number





The source code for reversing a Number in the bits is given on this page.

 


Source Code


 

#include "stdafx.h"

 

//0xC5 = 1100 0101

//0xA3 = 1010 0011

 

unsigned int ReverseInt(unsigned int number)

{

    int result = 0;

    int msbpos = sizeof(unsigned int) * 8 - 1;

 

    while(1)

    {

        if(number & 0x01)

        {

            result |= (0x01 << msbpos);

        }

        number >>= 1;

        msbpos--;

        if(msbpos < 0)

            break;

    }

    return result;

}

 

int _tmain(int argc, _TCHAR* argv[])

{

    //int number = 0x10101010;

    unsigned int number = 0xA3C50000;

    // unsigned int number = 0xA0000000;

    //unsigned int number = 0xAAAAAAAA;

    printf(" 0x%08x \n 0x%08x \n", number, ReverseInt(number));

    number = 0xA0A0A0A0;

    printf(" 0x%08x \n 0x%08x \n", number, ReverseInt(number));

      return 0;

}

Output


 

0xa3c50000

0x0000a3c5

 

0xa0a0a0a0

0x05050505