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 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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

a912cbd4 is reversed to d4cb12a9

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.

## Recommended Posts:

- Check if a string represents a hexadecimal number or not
- Check the divisibility of Hexadecimal numbers
- How to add two Hexadecimal numbers
- Reverse actual bits of the given number
- Reverse String according to the number of words
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Check if a given number is one less than twice its reverse
- Reverse a linked list
- Reverse a stack using recursion
- Reverse words in a given string
- Reverse a Linked List in groups of given size | Set 1
- Reverse alternate K nodes in a Singly Linked List
- Reverse Level Order Traversal
- Given a linked list, reverse alternate nodes and append at the end
- Reverse alternate levels of a perfect binary tree
- Reverse a string without affecting special characters
- Merge two sorted linked lists such that merged list is in reverse order
- Different methods to reverse a string in C/C++
- Reverse an array in groups of given size
- Reverse an array in groups of given size | Set 2 (Variations of Set 1 )

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.