Bit manipulation | Swap Endianness of a number - GeeksforGeeks

21 May, 2021
Prerequisite: https://www.geeksforgeeks.org/little-and-big-Endian-mystery/ 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. 
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: 
Examples:  
Input : 0x12345678 Output : 0x78563412 Input : 0x87654321 Output : 0x21436587  
Implementation: 
C++
C
Java
Python3
C#
// C++ program to print the difference// of Alternate Nodes#include <bits/stdc++.h>using namespace std; // Function to swap a value from// big Endian to little Endian and// vice versa.int swap_Endians(int value){     // This var holds the leftmost 8    // bits of the output.    int leftmost_byte;     // This holds the left middle    // 8 bits of the output    int left_middle_byle;     // This holds the right middle    // 8 bits of the output    int right_middle_byte;     // This holds the rightmost    // 8 bits of the output    int rightmost_byte;     // To store the result    // after conversion    int result;     // Get the rightmost 8 bits of the number    // by anding it 0x000000FF. since the last    // 8 bits are all ones, the result will be the    // rightmost 8 bits of the number. this will    // be converted into the leftmost 8 bits for the    // output (swapping)    leftmost_byte = (value & 0x000000FF) >> 0;     // Similarly, get the right middle and left    // middle 8 bits which will become    // the left_middle bits in the output    left_middle_byle = (value & 0x0000FF00) >> 8;     right_middle_byte = (value & 0x00FF0000) >> 16;     // Get the leftmost 8 bits which will be the    // rightmost 8 bits of the output    rightmost_byte = (value & 0xFF000000) >> 24;     // Left shift the 8 bits by 24    // so that it is shifted to the    // leftmost end    leftmost_byte <<= 24;     // Similarly, left shift by 16    // so that it is in the left_middle    // position. i.e, it starts at the    // 9th bit from the left and ends at the    // 16th bit from the left    left_middle_byle <<= 16;     right_middle_byte <<= 8;     // The rightmost bit stays as it is    // as it is in the correct position    rightmost_byte <<= 0;     // Result is the concatenation of all these values.    result = (leftmost_byte | left_middle_byle |              right_middle_byte | rightmost_byte);     return result;} // Driver Codeint main(){     // Consider a hexadecimal value    // given below. we are gonna convert    // this from big Endian to little Endian    // and vice versa.     int big_Endian = 0x12345678;    int little_Endian = 0x78563412;     int result1, result2;     result1 = swap_Endians(big_Endian);     result2 = swap_Endians(little_Endian);     printf("big Endian to little:"           "0x%x\nlittle Endian to big: 0x%x\n",            result1, result2);     return 0;} // This code is contributed by SHUBHAMSINGH10
#include <stdio.h> // Function to swap a value from// big Endian to little Endian and// vice versa. int swap_Endians(int value){     // This var holds the leftmost 8    // bits of the output.     int leftmost_byte;     // This holds the left middle    // 8 bits of the output     int left_middle_byle;     // This holds the right middle    // 8 bits of the output     int right_middle_byte;     // This holds the rightmost    // 8 bits of the output     int rightmost_byte;     // To store the result    // after conversion     int result;     // Get the rightmost 8 bits of the number    // by anding it 0x000000FF. since the last    // 8 bits are all ones, the result will be the    // rightmost 8 bits of the number. this will    // be converted into the leftmost 8 bits for the    // output (swapping)     leftmost_byte = (value & 0x000000FF) >> 0;     // Similarly, get the right middle and left    // middle 8 bits which will become    // the left_middle bits in the output     left_middle_byle = (value & 0x0000FF00) >> 8;     right_middle_byte = (value & 0x00FF0000) >> 16;     // Get the leftmost 8 bits which will be the    // rightmost 8 bits of the output     rightmost_byte = (value & 0xFF000000) >> 24;     // Left shift the 8 bits by 24    // so that it is shifted to the    // leftmost end     leftmost_byte <<= 24;     // Similarly, left shift by 16    // so that it is in the left_middle    // position. i.e, it starts at the    // 9th bit from the left and ends at the    // 16th bit from the left     left_middle_byle <<= 16;     right_middle_byte <<= 8;     // The rightmost bit stays as it is    // as it is in the correct position     rightmost_byte <<= 0;     // Result is the concatenation of all these values.     result = (leftmost_byte | left_middle_byle              | right_middle_byte | rightmost_byte);     return result;} // Driver Codeint main(){     // Consider a hexadecimal value    // given below. we are gonna convert    // this from big Endian to little Endian    // and vice versa.     int big_Endian = 0x12345678;    int little_Endian = 0x78563412;     int result1, result2;     result1 = swap_Endians(big_Endian);     result2 = swap_Endians(little_Endian);     printf("big Endian to little: 0x%x\nlittle Endian to big: 0x%x\n",           result1, result2);     return 0;}
// Java program to print the difference// of Alternate Nodesimport java.util.*; class GFG{ // Function to swap a value from// big Endian to little Endian and// vice versa.static int swap_Endians(int value){     // This var holds the leftmost 8    // bits of the output.    int leftmost_byte;     // This holds the left middle    // 8 bits of the output    int left_middle_byle;     // This holds the right middle    // 8 bits of the output    int right_middle_byte;     // This holds the rightmost    // 8 bits of the output    int rightmost_byte;     // To store the result    // after conversion    int result;     // Get the rightmost 8 bits of the number    // by anding it 0x000000FF. since the last    // 8 bits are all ones, the result will be the    // rightmost 8 bits of the number. this will    // be converted into the leftmost 8 bits for the    // output (swapping)    leftmost_byte = (value & 0x000000FF) >> 0;     // Similarly, get the right middle and left    // middle 8 bits which will become    // the left_middle bits in the output    left_middle_byle = (value & 0x0000FF00) >> 8;     right_middle_byte = (value & 0x00FF0000) >> 16;     // Get the leftmost 8 bits which will be the    // rightmost 8 bits of the output    rightmost_byte = (value & 0xFF000000) >> 24;     // Left shift the 8 bits by 24    // so that it is shifted to the    // leftmost end    leftmost_byte <<= 24;     // Similarly, left shift by 16    // so that it is in the left_middle    // position. i.e, it starts at the    // 9th bit from the left and ends at the    // 16th bit from the left    left_middle_byle <<= 16;     right_middle_byte <<= 8;     // The rightmost bit stays as it is    // as it is in the correct position    rightmost_byte <<= 0;     // Result is the concatenation of all these values.    result = (leftmost_byte | left_middle_byle |              right_middle_byte | rightmost_byte);     return result;} // Driver Codepublic static void main(String[] args){    // Consider a hexadecimal value    // given below. we are gonna convert    // this from big Endian to little Endian    // and vice versa.    int big_Endian = 0x12345678;    int little_Endian = 0x78563412;     int result1, result2;     result1 = swap_Endians(big_Endian);     result2 = swap_Endians(little_Endian);     System.out.printf("big Endian to little: 0x%x\n" +                      "little Endian to big: 0x%x\n",                       result1, result2);}} // This code is contributed by PrinciRaj1992
# Function to swap a value from# big Endian to little Endian and# vice versa.def swap_Endians(value):     # Get the rightmost 8 bits of the number    # by anding it 0x000000FF. since the last    # 8 bits are all ones, the result will be the    # rightmost 8 bits of the number. this will    # be converted into the leftmost 8 bits for the    # output (swapping)     leftmost_byte = (value & eval('0x000000FF')) >> 0     # Similarly, get the right middle and left     # middle 8 bits which will become    # the left_middle bits in the output     left_middle_byle = (value & eval('0x0000FF00')) >> 8     right_middle_byte = (value & eval('0x00FF0000'))>> 16     # Get the leftmost 8 bits which will be the    # rightmost 8 bits of the output     rightmost_byte = (value & eval('0xFF000000'))>> 24     # Left shift the 8 bits by 24    # so that it is shifted to the    # leftmost end     leftmost_byte <<= 24     # Similarly, left shift by 16    # so that it is in the left_middle    # position. i.e, it starts at the    # 9th bit from the left and ends at the    # 16th bit from the left     left_middle_byle <<= 16     right_middle_byte <<= 8     # The rightmost bit stays as it is    # as it is in the correct position     rightmost_byte <<= 0     # Result is the concatenation of all these values     result = (leftmost_byte | left_middle_byle                  | right_middle_byte | rightmost_byte)      return result   # main functionif __name__ == '__main__':     # Consider a hexadecimal value    # given below. we are gonna convert    # this from big Endian to little Endian    # and vice versa.    big_Endian = eval('0x12345678')    little_Endian = eval('0x78563412')         result1 = swap_Endians(big_Endian)     result2 = swap_Endians(little_Endian)     print("big Endian to little: % s\nlittle Endian              to big: % s" %(hex(result1), hex(result2)))
// C# program to print the difference// of Alternate Nodesusing System;     class GFG{ // Function to swap a value from// big Endian to little Endian and// vice versa.static int swap_Endians(int value){     // This var holds the leftmost 8    // bits of the output.    int leftmost_byte;     // This holds the left middle    // 8 bits of the output    int left_middle_byle;     // This holds the right middle    // 8 bits of the output    int right_middle_byte;     // This holds the rightmost    // 8 bits of the output    int rightmost_byte;     // To store the result    // after conversion    int result;     // Get the rightmost 8 bits of the number    // by anding it 0x000000FF. since the last    // 8 bits are all ones, the result will be the    // rightmost 8 bits of the number. this will    // be converted into the leftmost 8 bits for the    // output (swapping)    leftmost_byte = (value & 0x000000FF) >> 0;     // Similarly, get the right middle and left    // middle 8 bits which will become    // the left_middle bits in the output    left_middle_byle = (value & 0x0000FF00) >> 8;     right_middle_byte = (value & 0x00FF0000) >> 16;     // Get the leftmost 8 bits which will be the    // rightmost 8 bits of the output    rightmost_byte = (int)(value & 0xFF000000) >> 24;     // Left shift the 8 bits by 24    // so that it is shifted to the    // leftmost end    leftmost_byte <<= 24;     // Similarly, left shift by 16    // so that it is in the left_middle    // position. i.e, it starts at the    // 9th bit from the left and ends at the    // 16th bit from the left    left_middle_byle <<= 16;     right_middle_byte <<= 8;     // The rightmost bit stays as it is    // as it is in the correct position    rightmost_byte <<= 0;     // Result is the concatenation of all these values.    result = (leftmost_byte | left_middle_byle |              right_middle_byte | rightmost_byte);     return result;} // Driver Codepublic static void Main(String[] args){    // Consider a hexadecimal value    // given below. we are gonna convert    // this from big Endian to little Endian    // and vice versa.    int big_Endian = 0x12345678;    int little_Endian = 0x78563412;     int result1, result2;     result1 = swap_Endians(big_Endian);     result2 = swap_Endians(little_Endian);     Console.Write("big Endian to little: 0x{0:x}\n" +                    "little Endian to big: 0x{1:x}\n",                                   result1, result2);}} // This code is contributed by Rajput-Ji
big Endian to little:0x78563412
little Endian to big: 0x12345678

princiraj1992
Rajput-Ji
SHUBHAMSINGH10
akshaysingh98088
Computer Organization and Architecture
Bit Magic
Computer Organization & Architecture
Data Structures
Data Structures
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Set, Clear and Toggle a given bit of a number in C
Program to find parity
Write an Efficient Method to Check if a Number is Multiple of 3
Hamming code Implementation in C/C++
Check whether K-th bit is set or not
Cache Memory in Computer Organization
Addressing Modes
Program for Decimal to Binary Conversion
Addressing modes in 8085 microprocessor
Logical and Physical Address in Operating System