Write a c program for reversing a nibbles a given character
Write a c program to reverse nibbles of a given number
A nibble is a unit of digital information that consists of 4 bits, or half a byte. In computer science and information technology, nibbles are often used as a convenient way to work with individual groups of 4 bits in a larger word.
For example, consider an 8-bit number 0xF0. This number can be divided into two nibbles: 0xF (the upper nibble) and 0x0 (the lower nibble). By manipulating these nibbles independently, we can perform certain operations, such as reversing the order of the nibbles, that would be more difficult or cumbersome to perform on the entire 8-bit word.
C- Program to reverse nibbles of a given number without function
#include<stdio.h>
int main()
{
int num,pos,i,j,m,n;
printf("Enter any number:");
scanf("%d",&num);
printf("Before reversing the number:\n");
for(pos=31;pos>=0;pos--)
{
printf("%d",num>>pos&1);
}
printf("\n");
for(i=0,j=4;i<4;i++,j++)
{
m=num>>i&1;
n=num>>j&1;
if(m!=n)
{
num=num^1<<i;
num=num^1<<j;
}
}
printf("After reversing the number:\n");
for(pos=31;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
}
Output:
Enter any number: 65
Before reversing the number:
0000 0000 0000 0000 0000 0000 0100 0001
After reversing the number:
0000 0000 0000 0000 0000 0000 0001 0100
C- Program to reverse nibbles of a given number with function
#include<stdio.h>
unsigned char reverseNibble(unsigned char num) {
return ((num & 0x0F) << 4) | ((num & 0xF0) >> 4);
}
void printBinary(unsigned char num) {
for (int i = 7; i >= 0; i--) {
printf("%d", (num & (1 << i)) ? 1 : 0);
}
}
int main() {
unsigned char num;
printf("Enter a number: ");
scanf("%hhu", &num);
printf("Given number in binary: ");
printBinary(num);
printf("\n");
num = reverseNibble(num);
printf("Reversed nibble: ");
printBinary(num);
printf("\n");
return 0;
}
Output:
Enter a number: 65
Given a number in binary: 0100 0001
Reversed nibble: 0001 0100
Conclusion
In this program, reverseNibble
function takes an unsigned char
as input and returns the reversed nibble of it by swapping the first and fourth bits, and the second and third bits. The printBinary
function takes an
as input and prints its binary representation. The unsigned char
main
the function takes the user's input using scanf
and stores it in the unsigned char
variable num
. The nibble of the given number is then reversed using the reverseNibble
function, and finally, the binary representation of the reversed nibble is printed using the printBinary
function.
Top Resources
Copy in a reverse manner in c
Write a program to reverse printing not reversing of 10 elements of array in c
Write a program to reverse the bits of a given number using for loop
Design a function to reverse the given string
Design a function to reverse the word in given string
write a program to copy one array element to another array element in reverse manner.
write a program to reverse adjacent elements of an array.
write a program to reverse half of the elements of an array.
Further Reading:
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!