Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Little endian and big endian

Status
Not open for further replies.

Jinzpaul4u

Full Member level 4
Joined
Feb 1, 2012
Messages
231
Helped
59
Reputation
118
Reaction score
60
Trophy points
1,328
Location
India
Activity points
2,822
Hi Everyone,

Recently I faced an interview question regarding Little-Big endians..
Write a macro to convert little endian to big endian??


Does anyone of you know the answer for the same?

I am awaiting for your reply.

Thanks and regards,
Paul
 

Hi,

it is just byt swapping.

The problem is when a multi byte variable is stored or transmitted.

Some trasmit the lower significant byte first, other transmit the most significant byte first...So the transmitter and receiver must match on this.

The same is with storing data in a memory.
Some store the least significant byte in the lower address, some store the most significant byte in the lower address:
Example: a 32 bit variable with hex value "0x12345678" is stored in address 0x1000 of an 8 bit system:

either this way:
Address / data
0x1000 / 0x12
0x1001 / 0x34
0x1002 / 0x56
0x1003 / 0x78

or this way:
Address / data
0x1000 / 0x78
0x1001 / 0x56
0x1002 / 0x34
0x1003 / 0x12

The problem is not within one closed sytem where the compiler has the control of all data, The problem arises when two systems access the same data.
For example a profibus controller has dual por RAM inside and and expects a variable stored in RAM with big endian, but the microcontroller stores it little endian in the same RAM area.

You can imagine funny results...

Klaus
 
Hello!

He's not asking what is big / little endian, he's asking for a macro to convert endianness.
Here is one. Easy to understand. But imagine that for 64 bits.... it's better to define a swap16,
then a swap32 which uses swap16, then swap64 which uses swap32.

Code:
#define SWAP_ENDIAN(x)                  \
        (((x & 0x000000FF) << 24)        \
       | ((x & 0x0000FF00) << 8)          \
       | ((x & 0x00FF0000) >> 8)          \
       | ((x & 0xFF000000) >> 24))

Dora.
 
Hello!

He's not asking what is big / little endian, he's asking for a macro to convert endianness.
Here is one. Easy to understand. But imagine that for 64 bits.... it's better to define a swap16,
then a swap32 which uses swap16, then swap64 which uses swap32.

Code:
#define SWAP_ENDIAN(x)                  \
        (((x & 0x000000FF) << 24)        \
       | ((x & 0x0000FF00) << 8)          \
       | ((x & 0x00FF0000) >> 8)          \
       | ((x & 0xFF000000) >> 24))

Dora.



Hi Dora and klaus..

Thanks for replying me.

Yeah your logic is working well..
Code:
#include <stdio.h>
#include <string.h>
#define SWAP_ENDIAN(x)                  \
        (((x & 0x000000FF) << 24)        \
       | ((x & 0x0000FF00) << 8)          \
       | ((x & 0x00FF0000) >> 8)          \
       | ((x & 0xFF000000) >> 24))

main()
{
   unsigned long int abc=0x12345678;
    printf(" first output %d",abc  );
   abc=SWAP_ENDIAN(abc);
         printf(" endian output%d",abc  );
   
}

and output is ..

first output 305419896(0x12345678) endian output 2018915346 (0x78563412)

but what about 16 bit data.. which logic is suitable for 16 bit little-big endian conversion?



Best regards.
Paul
 
Last edited:

Hello!

Basically, there is no almighty endianness converter. If your number has to be byte reversed, you
have to know where it starts and how long it is. Therefore, you may implement a SWAP_ENDIAN16,
SWAP_ENDIAN32 and SWAP_ENDIAN64.
As for the 16 bit version, you can use the exact same method with (x&0x00FF)>>8 + (x&0x00FF) << 8;

Dora.
 

Traditionally, Lilliputians broke boiled eggs on the larger end; a few generations ago, an Emperor of Lilliput, the Present Emperor's great-grandfather, had decreed that all eggs be broken on the smaller end after he cut himself breaking the egg on the larger end. The differences between Big-Endians (those who broke their eggs at the larger end) and Little-Endians had given rise to "six rebellions... wherein one Emperor lost his life, and another his crown". The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. The Big-Endians gained favour in Blefuscu.
 

Traditionally, Lilliputians broke boiled eggs on the larger end; a few generations ago, an Emperor of Lilliput, the Present Emperor's great-grandfather, had decreed that all eggs be broken on the smaller end after he cut himself breaking the egg on the larger end. The differences between Big-Endians (those who broke their eggs at the larger end) and Little-Endians had given rise to "six rebellions... wherein one Emperor lost his life, and another his crown". The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. The Big-Endians gained favour in Blefuscu.

Thanks for your reply... LOL :grin:
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top