Byte Reversal

This is a discussion on Byte Reversal within the C Programming forums, part of the General Programming Boards category; I'm having trouble figuring out what exactly I'm missing in this Byte reversal method. For example is should take 0x01020304 ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    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!
    Attached Images Attached Images  

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by tabstop View Post
    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. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    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. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by PureWin View Post
    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. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote 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. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by PureWin View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 04:19 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21