Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! ๐ ๐นLearn advanced coding techniques in C& C++ ๐นMaster Linux internals & shell scripting ๐นDeep dive into Data Structures & Algorithms ๐นExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) ๐นGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
Nibble Swapping in C (Hexadecimal Input) in C
๐ถ Nibble Swapping in C (Hexadecimal Input)
#include<stdio.h>
int main( )
{
unsigned int num;
printf("Enter the number(in hex):");
scanf("%x",&num);
num = num & 0xFF;
unsigned int swapped = ((num & 0x0F) << 4) | ((num & 0xF0) >> 4);
printf("After swapping number is: %02X\\n", swapped);
return 0;
}
๐ Explanation:
This C program swaps the upper and lower 4-bit nibbles of an 8-bit hexadecimal number.
๐น `scanf("%x", &num);` reads a hexadecimal value.
๐น `num & 0xFF` ensures the value is within 8 bits.
๐น `(num & 0x0F) << 4` moves the lower nibble to the upper nibble position.
๐น `(num & 0xF0) >> 4` moves the upper nibble to the lower nibble position.
๐น The final result is obtained by combining both using bitwise OR (`|`).
๐น `%02X` prints the swapped result in uppercase hexadecimal format with leading zero if needed.
๐ Sample Output:
Enter the number(in hex):3C
After swapping number is: C3
Enter the number(in hex):F0
After swapping number is: 0F
๐ท️ Keywords:
bitwise operations in C, swap nibbles C, C program hex input, 8-bit nibble swap, binary logic in C, nibble masking, hex manipulation
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment