C program to SWAP the nibbles of a character | SWAP the nibbles of given character

Spread the love

Swap-nibbles-of-character-in-c

Program Introduction:

Write a C program to swap nibbles of given character.

As the character datatype size is 8 bits. So we need to swap both nibbles (4 bits)

Here are few examples with expected input and output.

Example 1:

Input:

Input an Character.

Output:

As part of the output, We display the given characters in binary format. Then we swap the nibbles and display the character in Binary format after swapping the nibbles. So that we can easily check if we swapped the nibble properly.

Example 2:

Input:

Output:

Example 3:

Input:

Output:

Swap Nibbles Program Logic:

The nibble means 4 bits memory area. The character datatype size is One Byte or 8 bits in the C programming language.

So we need to swap the First four bits with the last four bits.

Let’s look at an example to understand it better. Let’s say the user entered the character ‘ J'.

Then the Binary equivalent of the 'J' is “ 01001010" Here the first nibble of 'J' is "0100" and thesecond nibble of 'J'is "1010". The program should swap these two nibbles.

The expected output after swapping nibbles is "10100100". If you observe the both nibbles are swapped.

The main logic for swap the nibbles is to use the Bitwise right-shift and Bitwise Left-shift operators. Bitwise opertors are useful to do the bit level operation. Like in our present case also we are trying to move the bits.

📌 Learn more about bitwise operators at the following article – Bit wise Operators in C Language – SillyCodes

So we need to right shift the first four bits (Nibble) and left shift the last four bits (Nibble) and then perform the bitwise OR and assign it back to the given number

Here is logic.

Swap Nibbles example walk through:

Okay, Above logic might look confusing so lets take a example and go through the step by step.

We will take character 'M'.

The binary representation of 'M'is 01001101

First of all, We need to right shift the first four bits (Nibble) of the 'M'character.

After Right shift first four bits ( 'M' >> 4 ) is 00000100. Remember we are not overriding the original character 'M' here.

Then left shift the last four bits. So ( 'M' << 4 ) becomes 11010000.

Now we need to perform the bitwise OR of the above two values.

( 'M' >> 4   |  'M' << 4 ) 

i.e

00000100  | 11010000  = 11010100

Now we have successfully swapped the nibbles of character. and our desired output is 11010100

In summary, We need to Bitwise Right shift the first four bits and then Bitwise Left shift the last four bits then perform the bitwise OR.

📢. In the following program, We are using the Character to Binary conversion logic. We will discuss about character to Binary conversion logic in future articles.

Program: Swap Nibbles of Character:

Output:

We are using GCC compiler on Linux Operating system.

Conclusion:

In this article, We discussed the technique to swap the nibbles of character. We used bitwise operators to perform the swap. We can even apply the same logic to swap the bits of Integer datatype but what do we need to call for Short integers swapping (double nibble swap or maybe swap the bytes) 😛 ).

Related Articles:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply