Skip to main content
make int -> long
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418

Putting all your code in the main method is seldom a good idea.

Additionally, int is only guaranteed to 16 bits, you need to use long.

This problem lends itself to creating a function to do the work for you:

intunsigned long bitReverse(intunsigned long value)
{
    intunsigned long result = 0;
    while (value != 0)
    {
        /* make space by shifting left on the result */
        result <<= 1;
        /* add the low bit in to the space */
        result |= value & 1;
        /* get a new low bit. */
        value >>= 1;
    }
    return result;
}

The above function takes the low-bit off of the input value, and feeds them to the high bits of the output value.

I have put this together in a simple ideone

Putting all your code is seldom a good idea. This problem lends itself to creating a function to do the work for you:

int bitReverse(int value)
{
    int result = 0;
    while (value != 0)
    {
        /* make space by shifting left on the result */
        result <<= 1;
        /* add the low bit in to the space */
        result |= value & 1;
        /* get a new low bit. */
        value >>= 1;
    }
    return result;
}

The above function takes the low-bit off of the input value, and feeds them to the high bits of the output value.

I have put this together in a simple ideone

Putting all your code in the main method is seldom a good idea.

Additionally, int is only guaranteed to 16 bits, you need to use long.

This problem lends itself to creating a function to do the work for you:

unsigned long bitReverse(unsigned long value)
{
    unsigned long result = 0;
    while (value)
    {
        /* make space by shifting left on the result */
        result <<= 1;
        /* add the low bit in to the space */
        result |= value & 1;
        /* get a new low bit. */
        value >>= 1;
    }
    return result;
}

The above function takes the low-bit off of the input value, and feeds them to the high bits of the output value.

I have put this together in a simple ideone

Add ideone link
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418

Putting all your code is seldom a good idea. This problem lends itself to creating a function to do the work for you:

int bitReverse(int value)
{
    int result = 0;
    while (value != 0)
    {
        /* make space by shifting left on the result */
        result <<= 1;
        /* add the low bit in to the space */
        result |= value & 1;
        /* get a new low bit. */
        value >>= 1;
    }
    return result;
}

The above function takes the low-bit off of the input value, and feeds them to the high bits of the output value.

I have put this together in a simple ideone

Putting all your code is seldom a good idea. This problem lends itself to creating a function to do the work for you:

int bitReverse(int value)
{
    int result = 0;
    while (value != 0)
    {
        /* make space by shifting left on the result */
        result <<= 1;
        /* add the low bit in to the space */
        result |= value & 1;
        /* get a new low bit. */
        value >>= 1;
    }
    return result;
}

The above function takes the low-bit off of the input value, and feeds them to the high bits of the output value.

Putting all your code is seldom a good idea. This problem lends itself to creating a function to do the work for you:

int bitReverse(int value)
{
    int result = 0;
    while (value != 0)
    {
        /* make space by shifting left on the result */
        result <<= 1;
        /* add the low bit in to the space */
        result |= value & 1;
        /* get a new low bit. */
        value >>= 1;
    }
    return result;
}

The above function takes the low-bit off of the input value, and feeds them to the high bits of the output value.

I have put this together in a simple ideone

Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418

Putting all your code is seldom a good idea. This problem lends itself to creating a function to do the work for you:

int bitReverse(int value)
{
    int result = 0;
    while (value != 0)
    {
        /* make space by shifting left on the result */
        result <<= 1;
        /* add the low bit in to the space */
        result |= value & 1;
        /* get a new low bit. */
        value >>= 1;
    }
    return result;
}

The above function takes the low-bit off of the input value, and feeds them to the high bits of the output value.