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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximize the Expression | Bit Manipulation
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Fast Exponention using Bit Manipulation
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- 8086 program to divide a 16 bit number by an 8 bit number
- Bits manipulation (Important tactics)
- String manipulation instructions in 8086 microprocessor
- 8085 program to swap two 8-bit numbers
- 8085 program to swap two 16 bit numbers using Direct addressing mode
- 8085 program to swap two 8 bit numbers using Direct addressing mode
- 8086 program to reverse 8 bit number using 8 bit operation
- 8086 program to reverse 16 bit number using 8 bit operation
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Swap bits in a given number
- 8085 program to convert an 8 bit number into Grey number
- 8085 program to count number of ones in the given 8-bit number
- 8085 program to convert an 8 bit BCD number into hexadecimal number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.