Little Endian and Big Endian are ways or storing data in machines. Some machines might use Little Endian byte ordering while others might use big Endian. This creates an inconsistency when you are transferring data from a Big Endian machine to a Little Endian machine. Usually, the compiler takes care of the conversion. But, in networking, Big Endian is used as the standard for the exchange of data between networks. Therefore, Little Endian machines need to convert their data to Big Endian while sending data through a network. Similarly, Little Endian machines need to swap the byte ordering when they receive data from a network.
So Endianness comes into picture when you are sending and receiving data across the network from one host to another host. If the sender and receiver computer have different Endianness, then there is a need to swap the Endianness so that it is compatible.
Therefore, it is important to convert the data to little Endian or big Endian so that there is consistency and data integrity. In this article, we will look at how the Endianness of a number can be swapped. This is also a common interview question.
- Get the rightmost 8 bits of the number by anding it with 0x000000FF since the last 8 bits are all ones and the rest are all zeros, the result will be rightmost 8 bits of the number. The result is stored in a variable called leftmost_byte
- Similarly, get the next 8 bits (from the right, right middle) of the number by anding it with 0x0000FF00. The result is stored in left_middle_byte
- Obtain the next 8 bits of the number by anding it with 0x00FF0000. The result is stored in right_middle_byte
- Finally, get the leftmost 8 bits of the number by anding it with 0xFF000000. The result is stored in rightmost_byte
- Now that we have all the 4 bytes of the number, we need to concatenate it in reverse order. i.e, swap the Endianness of the number. To do this, we shift the rightmost 8 bits by 24 to the left so that it becomes the leftmost 8 bits. We left shift the right middle byte by 16 (to store it as the left middle byte) We left shift the left middle byte by 8 (to store it as the right muddle byte) We finally left shift the leftmost byte by 24 to the left
- Now, we logically “or” (concatenate) all the variables to obtain the result.
Consider the number 0x12345678. The number is 4 bytes wide. In Big Endian, this number is represented as:
In Little Endian, the same number is represented as:
Input : 0x12345678
Output : 0x78563412
Input : 0x87654321
Output : 0x21436587
big Endian to little: 0x78563412 litle Endian to big: 0x12345678
- Swap bits in a given number
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Bits manipulation (Important tactics)
- String manipulation instructions in 8086 microprocessor
- Swap all odd and even bits
- Swap every two bits in bytes
- How to swap two bits in a given integer?
- Swap two nibbles in a byte
- Swap three variables without using temporary variable
- 8085 program to swap two 8-bit numbers
- How to swap two numbers without using a temporary variable?
- 8085 program to swap two 8 bit numbers using Direct addressing mode
- 8085 program to swap two 16 bit numbers using Direct addressing mode
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Count number of trailing zeros in Binary representation of a number using Bitset
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.