Reverse bytes of a Hexadecimal Number Last Updated : 16 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an unsigned integer N. The task is to reverse all bytes of N without using a temporary variable and print the reversed number. Examples: Input: N = 0xaabbccdd Output: 0xddccbbaa Input: N = 0xa912cbd4 Output: 0xd4cb12a9 The naive approach is to extract the appropriate byte is to use mask(&) with shift operators. #define REV(x) ( ((x&0xff000000)>>24) | (((x&0x00ff0000)<<8)>>16) | (((x&0x0000ff00)>>8)<<16) | ((x&0x000000ff) << 24) ) Efficient approach: The idea is to use shift operators only. Move the position of the last byte to the first byte using left shift operator(<<).Move the position of the first byte to the last byte using right shift operator(>>).Move the middle bytes using the combination of left shift and right shift operator.Apply logical OR (|) to the output of all the above expression, to get the desired output. Below is the implementation of the above approach : C // C program to reverse bytes of a hexadecimal number #include <stdio.h> // macro which reverse the hexadecimal integer #define REV(n) ((n << 24) | (((n>>16)<<24)>>16) | (((n<<16)>>24)<<16) | (n>>24)) // Driver code int main() { unsigned int n = 0xa912cbd4; // n = 0xaabbccdd // (n >> 24) - 0x000000aa // (n << 24) - 0xdd000000 // (((n >> 16) << 24) >> 16) - 0xbb00 // (((n >> 8) << 24) >> 8) - 0xcc0000 // If output of all the above expression is // OR'ed then it results in 0xddccbbaa printf("%x is reversed to %x", n, REV(n)); return 0; } C++ // C++ program to reverse bytes of a hexadecimal number #include <iostream> using namespace std; int REV(int n) { return ((n >> 24) & 0xff) | ((n << 8) & 0xff0000) | ((n >> 8) & 0xff00) | ((n << 24) & 0xff000000); // If output of all the above expression is // OR'ed then it results in 0xddccbbaa } int main() { int n = 0xa912cbd4; cout << hex << n << " is reversed to " << REV(n) << endl; return 0; } Java // Java program to reverse bytes of a hexadecimal number class GFG { public static int REV(int n) { return ((n >> 24) & 0xff) | // (n >> 24) - 0x000000aa ((n << 8) & 0xff0000) | // (n << 24) - 0xdd000000 ((n >> 8) & 0xff00) | // (((n >> 16) << 24) >> 16) - 0xbb00 ((n << 24) & 0xff000000); // (((n >> 8) << 24) // >> 8) - 0xcc0000 // If output of all the above expression is // OR'ed then it results in 0xddccbbaa } public static void main(String[] args) { int n = 0xa912cbd4; System.out.println(Integer.toHexString(n) + " is reversed to " + Integer.toHexString(REV(n))); } } //this code is contributed by phasing17 C# // C# program to reverse bytes of a hexadecimal number using System; class GFG { public static ulong REV(uint n) { return (ulong)((n >> 24) & 0xff) | // (n >> 24) - 0x000000aa (ulong)((n << 8) & 0xff0000) | // (n << 24) - 0xdd000000 (ulong)((n >> 8) & 0xff00) | // (((n >> 16) << 24) >> 16) - 0xbb00 (ulong)((n << 24) & 0xff000000); // (((n >> 8) << 24) // >> 8) - 0xcc0000 // If output of all the above expression is // OR'ed then it results in 0xddccbbaa } // Driver code public static void Main(string[] args) { uint n = 0xa912cbd4; // Function call Console.WriteLine(n.ToString("X") + " is reversed to " + REV(n).ToString("X")); } } // This code is contributed by phasing17 Python3 def REV(n: int) -> int: return ((n >> 24) & 0xff) | ((n << 8) & 0xff0000) | ((n >> 8) & 0xff00) | ((n << 24) & 0xff000000) # If output of all the above expression is # OR'ed then it results in 0xddccbbaa if __name__ == '__main__': n = 0xa912cbd4 print(f"{n:x} is reversed to {REV(n):x}") JavaScript // JavaScript program to reverse bytes of a hexadecimal number function REV(n) { return (BigInt(n) >> 24n & 0xffn) | // (n >> 24) - 0x000000aa (BigInt(n) << 8n & 0xff0000n) | // (n << 24) - 0xdd000000 (BigInt(n) >> 8n & 0xff00n) | // (((n >> 16) << 24) >> 16) - 0xbb00 (BigInt(n) << 24n & 0xff000000n); // (((n >> 8) << 24) // >> 8) - 0xcc0000 // If output of all the above expression is // OR'ed then it results in 0xddccbbaa } const n = 0xa912cbd4; // Function call console.log(n.toString(16) + " is reversed to " + REV(n).toString(16)); // This code is contributed by phasing17 Output:a912cbd4 is reversed to d4cb12a9 Time complexity: O(1)Auxiliary space: O(1) Comment More infoAdvertise with us Next Article Program to convert a binary number to hexadecimal number S strive_to_learn Follow Improve Article Tags : Mathematical DSA Reverse Bit Algorithms Numbers +1 More Practice Tags : MathematicalNumbersReverse Similar Reads Reverse actual bits of the given number Given a non-negative integer n, the task is to reverse the bits in its binary representation and return the resulting decimal number. The reversal should consider only the actual binary digits without any leading zeros.Examples : Input : 11Output : 13Explanation: (11)10 = (1011)2.After reversing the 4 min read Check if a HexaDecimal number is Even or Odd Given a HexaDecimal number, check whether it is even or odd.Examples: Input: N = ABC7787CC87AA Output: Even Input: N = 9322DEFCD Output: Odd Naive Approach: Convert the number from Hexadecimal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. 4 min read Modulus of two Hexadecimal Numbers Given two hexadecimal numbers N and K, the task is to find N modulo K. Examples: Input: N = 3E8, K = 13 Output: C Explanation: Decimal representation of N( = 3E8) is 1000 Decimal representation of K( = 13) is 19 Decimal representation of (N % K) = 1000 % 19 = 12 ( = C). Therefore, the required outpu 6 min read Program to Convert Hexadecimal Number to Binary Given a Hexadecimal number as an input, the task is to convert that number to a Binary number.Examples: Input: Hexadecimal = 1AC5 Output: Binary = 0001101011000101 Explanation: Equivalent binary value of 1: 0001 Equivalent binary value of A: 1010 Equivalent binary value of C: 1100 Equivalent binary 14 min read Program to convert a binary number to hexadecimal number Given a Binary Number, the task is to convert the given binary number to its equivalent hexadecimal number. The input could be very large and may not fit even into an unsigned long long int.Examples:Â Input: 110001110Output: 18EInput: 1111001010010100001.010110110011011Output: 794A1.5B36 794A1D9B App 13 min read Reverse bits of a positive integer number in Python Given an positive integer and size of bits, reverse all bits of it and return the number with reversed bits.Examples: Input : n = 1, bitSize=32 Output : 2147483648 On a machine with size of bit as 32. Reverse of 0....001 is 100....0. Input : n = 2147483648, bitSize=32 Output : 1 We can solve this pr 4 min read Like