# Thread: Byte Reversal

1. ## Byte Reversal

I'm having trouble figuring out what exactly I'm missing in this Byte reversal method. For example is should take 0x01020304 and flip the bits to 0x04030201.
I've attached my output giving the errors and here is my code:

Code:
```/*
* reverseBytes - reverse the bytes of x
*   Example: reverseBytes(0x01020304) = 0x04030201
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 25
*   Rating: 3
*/
int reverseBytes(int x) {
//unsigned char x = y;
x = (x & 0x7FFFFFFF );

x = (x&0x0F) << 4 | (x&0xF0) >> 4;
x = (x&0x33) << 2 | (x&0xCC) >> 2;
x = (x&0x55) << 1 | (x&0xAA) >> 1;

return x;
}```
I can't figure out what I'm missing/what needs fixing this is as far as I can get. I can't modify the method name or parameter types and can't use loops/conditionals. Any input/help would be appreciated!

2. Code:
`x = (x & 0x7FFFFFFF )`
Why oh why could you be missing the highest bit....

And then, why do you even think those bit manipulation things make sense? I'd be really surprised if you could reverse an int in-place, but you don't have to: declare a local variable to be your answer.

3. Not sure what you mean by missing the highest bit and declare a local variable to be my answer. Could you clarify? Thanks.

4. The highest bit -- you know, the one that turns 7FFFFFFF into FFFFFFFF? And you are (apparently based on what you posted) allowed to declare variables -- just because x is the only variable that comes into the function doesn't mean you can't have as many other variables as you want.

5. A search of this forum resulted in Brewbuck's answer:

The bits don't get reversed. Just the bytes.

Code:
```unsigned int SwapEndian( unsigned int x )
{
return     ( ( x >> 24 ) & 0x000000FF )
|   ( ( x >>  8 ) & 0x0000FF00 )
|   ( ( x <<  8 ) & 0x00FF0000 )
|   ( ( x << 24 ) & 0xFF000000 );
}```

6. Originally Posted by tabstop
Why oh why could you be missing the highest bit....

And then, why do you even think those bit manipulation things make sense? I'd be really surprised if you could reverse an int in-place, but you don't have to: declare a local variable to be your answer.
I think he's reversing bits in a byte there, not bytes in a bit.

Also, bit-shifting on negative values is undefined. Use an unsigned int to shift. Also, your shifting is wrong as tabstop said, although something very similar would work...

7. Okay I got the highest bit as 0x80000000. And yes I can declare variables in the function though I'm not sure what your getting at. Do I need to add some operation to
x = (x & 0x7FFFFFFF );
that involves the highest bit? Is that where declaring a variable to be my answer would come in?

8. I'm saying "x = (x & 07FFFFFFF)" cannot possibly appear in a working implementation of this function. It is wrong, inaccurate, wrong, misguided, and wrong. You need to declare some other variable (other than x) to be your answer, because x is not your answer. x is your input, and if you try to fiddle with it you will destroy your ability to continue computing.

9. Originally Posted by PureWin
Okay I got the highest bit as 0x80000000. And yes I can declare variables in the function though I'm not sure what your getting at. Do I need to add some operation to
x = (x & 0x7FFFFFFF );
that involves the highest bit? Is that where declaring a variable to be my answer would come in?
Think of it this way. The mere fact that you have X = SOMETHING means that you are most likely clobbering the value of X. Does that make sense?

10. Originally Posted by OP
* Example: reverseBytes(0x01020304) = 0x04030201
THIS IS NOT A BIT REVERSAL!!!! This is ONLY an endian swap!!!!!!!!!!! i.e. Post #5.

11. Originally Posted by PureWin
I'm having trouble figuring out what exactly I'm missing in this Byte reversal method. For example is should take 0x01020304 and flip the bits to 0x04030201.
I've attached my output giving the errors and here is my code:

Code:
```/*
* reverseBytes - reverse the bytes of x
*   Example: reverseBytes(0x01020304) = 0x04030201
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 25
*   Rating: 3
*/
int reverseBytes(int x) {
//unsigned char x = y;
x = (x & 0x7FFFFFFF );

x = (x&0x0F) << 4 | (x&0xF0) >> 4;
x = (x&0x33) << 2 | (x&0xCC) >> 2;
x = (x&0x55) << 1 | (x&0xAA) >> 1;

return x;
}```
I can't figure out what I'm missing/what needs fixing this is as far as I can get. I can't modify the method name or parameter types and can't use loops/conditionals. Any input/help would be appreciated!
You are 100% confused. Going by the code comments, you want endian swapping which Kennedy has already posted.
However you also talk about byte reversal which makes no sense. You appear to want byte-order reversal, but one could also have interpreted it as bit-order reversal. You also talk about flipping bits. This again suggests bit-order reversal but could also mean bit-toggling! Finally, the code you've no doubt copied from somewhere and added to is actually for reversing the bits in an 8-bit quantity and you have a 32-bit quantity.

How could you possibly think any of the code you posted gets you anywhere towards your goal?! You're clearly not trying to solve the problem, you're just looking for where to copy some code that someone else has already written from. Even if you come across the answer I doubt you could even explain how any of it works.

Popular pages Recent additions