4

Little Endian vs Big Endian

Big Endian = 0x31014950
Little Endian = 0x50490131

However Using this Method

inline unsigned int endian_swap(unsigned int& x)  
{
return ( ( (x & 0x000000FF) << 24 ) | 
         ( (x & 0x0000FF00) << 8  ) |
         ( (x & 0x00FF0000) >> 8  ) |
         ( (x & 0xFF000000) >> 24 ) );
}

result = 0x54110131
i spent lot of time trying lots of similar methods and even a library one like

unsigned long _byteswap_ulong(unsigned long value);  

But Still no luck .. all returns same result

EDIT
I'm Working on Little-Endian System with Microsoft Visual Studio 2008
the example as Follows

int main()
{
    unsigned int y = 0x31014950;
    unsigned int r = endian_swap( y );
    std::cout << r;
}  

the example posted on Ideone.com is correct .. but it doesn't work with me

EDIT

std::cout << std::hex << r;  

Either Ways Pals .. Hex or Not it's not getting the Right Number .. Either Visual Studio got a serious Error in it's Debugger or My Whole Machine ..
Because i Replaced the Whole Code with a More Slow Redunant code but still getting same results
BTW if it makes any difference .. i'm using Debugger to Break after the function to check the result

VirusEcks
  • 596
  • 1
  • 10
  • 14
  • You are using the return value, right? While you take `x` by reference, you never actually modify it. – James McNellis Nov 12 '10 at 22:00
  • `endian_swap` looks fine. And if replacing it with a library function "returns same result", maybe your problem is somewhere outside that function. – aschepler Nov 12 '10 at 22:02
  • @James made me think you're not even acting upon the value of `x`, you're doing all the swapping on the reference. Is that what you actually want? – BeemerGuy Nov 12 '10 at 22:02
  • result is printed correctly .. argument passed by refrnce and is correct .. i use the return ... nothing outside the code .. it's just the number and return – VirusEcks Nov 12 '10 at 22:09
  • 2
    @VirusEcks: show your complete program, there's no point a bunch of people sitting around guessing what it is you've done wrong. – Steve Jessop Nov 12 '10 at 22:17
  • 2
    So when you said, "I use the return", what did you mean? The function doesn't modify `y`. The value you print out is `y`. The output of this program surely is "822167888". If you print out `r`, you'll see the endian-reversed value. – Steve Jessop Nov 12 '10 at 22:29
  • 1
    @Steve: and we need to `cout << hex`, otherwise we get decimal output – Vlad Nov 12 '10 at 22:36
  • 1
    @VirusEcks: HELLO! YOU'RE PRINTING OUT `y`! Why would you think that your function, `endian_swap`, modifies `y` in any way? It doesn't. – Steve Jessop Nov 13 '10 at 00:44
  • Sorry for the Confusion .. well .. i'm not printing it out anyways .. i'm using debugger to break after the function and check the result .. – VirusEcks Nov 13 '10 at 00:47
  • @VirusEcks: (1) does your example work as intended? (2) what is the input value, and what is the expected output value, and what is the actual output value? – Vlad Nov 13 '10 at 12:34

7 Answers7

2
  • Are you printing the result correctly?
  • Does the fact that you're passing in a reference instead of a value make a difference?
John
  • 15,990
  • 10
  • 70
  • 110
  • will removing the refrence make a difference ? because appearantly it doesn't :/ – VirusEcks Nov 12 '10 at 22:08
  • 1
    Usually you pass in by reference if you want to change the passed-in parameter from within the function. You don't appear to be changing `x` at all from within your function, so passing in by value makes your intention clearer (and possibly correct). :) – John Nov 12 '10 at 22:11
  • OK, it's not the reference thing. Interesting problem! – John Nov 12 '10 at 22:19
2

Your code seems to be correct.

The following program (http://ideone.com/a5TBF):

#include <cstdio>

inline unsigned int endian_swap(unsigned const int& x)  
{
return ( ( (x & 0x000000FF) << 24 ) | 
         ( (x & 0x0000FF00) << 8  ) |
         ( (x & 0x00FF0000) >> 8  ) |
         ( (x & 0xFF000000) >> 24 ) );
}

int main()
{
    unsigned int x = 0x12345678;
    unsigned int y = endian_swap(x);
    printf("%x %x\n", x, y);
    return 0;
}

outputs:

12345678 78563412


Edit:
you need std::cout << std::hex << r, otherwise you are printing (1) wrong variable, and (2) in decimal :-)

See this example: http://ideone.com/EPFz8

Vlad
  • 35,022
  • 6
  • 77
  • 199
1

Eliminate the ampersand in front of the x in your argument specifier. You want to pass the value.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • @John: They didn't say, so impossible to know, but this doesn't even address the problem asked about. –  Nov 13 '10 at 03:09
  • At one point it wasn't clear whether it applied or not, but I think we've figured out now that it didn't cause the problem. – John Nov 13 '10 at 03:52
0

Have you unit-tested your code?

On my platform, this passes:

void LittleEndianTest::testLittleEndian()
{
    unsigned int x = 0x31014950;
    unsigned int result = 
         (
             ( (x & 0x000000FF) << 24 ) + 
             ( (x & 0x0000FF00) << 8  ) +
             ( (x & 0x00FF0000) >> 8  ) +
             ( (x & 0xFF000000) >> 24 ) 
         );

    CPPUNIT_ASSERT_EQUAL((unsigned int)0x50490131, result); 
}
J. Polfer
  • 12,251
  • 10
  • 54
  • 83
0

The example in your edit is outputting y not r. The input y is, of course, not modified.

zdan
  • 28,667
  • 7
  • 60
  • 71
0

If I run the code you posted, it gives the correct result: endian_swap ( 0x31014950 ) == 0x50490131.

To get the result: endian_swap ( 0x31014950 ) == 0x54110131, your code must be equivalent to this:

#define __
inline unsigned int endian_swap(unsigned int& x)  
{                  //0x31014950  ->            0x54110131
    return  ( ( (x & 0x000000FF) << 24 ) |   //  50
        __    ( (x & 0x00F0F000) << 12 ) |   //   4
        __    ( (x & 0x00FF0000) << 4  ) |   //    1  
        __    ( (x & 0x00FF0000) << 0  ) |   //     1  
        __    ( (x & 0x00FF0000) >> 8  ) |   //      01
        __    ( (x & 0xFF000000) >> 24 ) );  //        31        
}

Check you haven't got similar differences in your code too.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
0

Bit operators are not useful because they operates as if the bits are arranged in order from least significant bit to most significant bit regardless of the true internal byte order.

void isBigEndian()
{
    void *number;
    number = (int *) new int(0x01000010);
    // 0x01000010
    //   01 00 00 10                                      Hexadecimal
    //   0    1      0    0      0    0       1    0
    //   0000 0001   0000 0000   0000 0000    0001 0000   Bit
    //   1           0           0            16          Decimal
    char *byte;
    byte = (char *)number;
    cout << static_cast<int>(*byte);//prints 16: Little Endian
}

You change the number above and make it return 1 when it is BigEndian.

haberdar
  • 501
  • 5
  • 6