# Swap two nibbles in a byte

A nibble is a four-bit aggregation, or half an octet. There are two nibbles in a byte.

Given a byte, swap the two nibbles in it. For example 100 is be represented as 01100100 in a byte (or 8 bits). The two nibbles are (0110) and (0100). If we swap the two nibbles, we get 01000110 which is 70 in decimal.

To swap the nibbles, we can use bitwise &, bitwise ” operators. A byte can be represented using a unsigned char in C as size of char is 1 byte in a typical C compiler.

Below is the implementation of above idea.

## C++

`// C++ program to swap two ` `// nibbles in a byte ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `swapNibbles(` `int` `x) ` `{ ` ` ` `return` `( (x & 0x0F) << 4 | (x & 0xF0) >> 4 ); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 100; ` ` ` `cout << swapNibbles(x); ` ` ` `return` `0; ` `} ` ` ` `//This code is contributed by Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## C

`#include <stdio.h> ` ` ` `unsigned ` `char` `swapNibbles(unsigned ` `char` `x) ` `{ ` ` ` `return` `( (x & 0x0F)<<4 | (x & 0xF0)>>4 ); ` `} ` ` ` `int` `main() ` `{ ` ` ` `unsigned ` `char` `x = 100; ` ` ` `printf` `(` `"%u"` `, swapNibbles(x)); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to swap two ` `// nibbles in a byte ` ` ` `class` `GFG { ` ` ` `static` `int` `swapNibbles(` `int` `x) ` `{ ` ` ` `return` `((x & ` `0x0F` `) << ` `4` `| (x & ` `0xF0` `) >> ` `4` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String arg[]) ` `{ ` ` ` `int` `x = ` `100` `; ` ` ` `System.out.print(swapNibbles(x)); ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# python program Swap ` `# two nibbles in a byte ` ` ` `def` `swapNibbles(x): ` ` ` `return` `( (x & ` `0x0F` `)<<` `4` `| (x & ` `0xF0` `)>>` `4` `) ` ` ` `# Driver code ` ` ` `x ` `=` `100` `print` `(swapNibbles(x)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to swap two ` `// nibbles in a byte ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function for swapping ` `static` `int` `swapNibbles(` `int` `x) ` `{ ` ` ` `return` `((x & 0x0F) << 4 | ` ` ` `(x & 0xF0) >> 4); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `x = 100; ` ` ` `Console.Write(swapNibbles(x)); ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to swap two ` `// nibbles in a byte ` ` ` `// function to Swap two nibbles ` `// in a byte in php program ` `function` `swapNibbles(` `$x` `) ` `{ ` ` ` `return` `( (` `$x` `& 0x0F) << 4 | ` ` ` `(` `$x` `& 0xF0) >> 4 ); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$x` `= 100; ` ` ` `echo` `swapNibbles(` `$x` `); ` ` ` `// This Code is Contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

70

**Explanation:**

100 is 01100100 in binary. The operation can be split mainly in two parts

**1)** The expression “**x & 0x0F**” gives us last 4 bits of x. For x = 100, the result is 00000100. Using bitwise ‘<<' operator, we shift the last four bits to the left 4 times and make the new last four bits as 0. The result after shift is 01000000.

**2)** The expression “**x & 0xF0**” gives us first four bits of x. For x = 100, the result is 01100000. Using bitwise ‘>>’ operator, we shift the digit to the right 4 times and make the first four bits as 0. The result after shift is 00000110.

At the end we use the bitwise OR ‘|’ operation of the two expressions explained above. The OR operator places first nibble to the end and last nibble to first. For x = 100, the value of (01000000) OR (00000110) gives the result 01000110 which is equal to 70 in decimal.

This article is contributed by **Anuj Garg**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Swap all odd and even bits
- Swap bits in a given number
- Swap every two bits in bytes
- How to swap two bits in a given integer?
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Swap three variables without using temporary variable
- How to swap two numbers without using a temporary variable?
- Sum of Bitwise OR of all pairs in a given array
- Maximum subset sum such that no two elements in set have same digit in them
- Count pairs of elements such that number of set bits in their AND is B[i]
- Number of 0s and 1s at prime positions in the given array
- Find uncommon characters of the two strings | Set 2
- Fibonacci Power
- Find N distinct numbers whose bitwise Or is equal to K