Skip to content

luc108.c

Problem Statement

Receive an 8-bit number and exchange its higher 4 bits with lower 4 bits.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)

Actions

Raw View on GitHub

💡 You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned char num, swapped;

    printf("Enter an 8-bit number (0-255): ");
    scanf("%hhu", &num);

    // Exchange nibbles
    // (num & 0xF0) >> 4 : High nibble to Low
    // (num & 0x0F) << 4 : Low nibble to High

    swapped = ((num & 0xF0) >> 4) | ((num & 0x0F) << 4);

    printf("Original: %d (Hex: 0x%02X)\n", num, num);
    printf("Swapped:  %d (Hex: 0x%02X)\n", swapped, swapped);

    return 0;
}