Q:

C program to swap two nibbles of a byte.

0

C program to swap two nibbles of a byte.

This program will swap two nibbles of a byte, as we know that one byte has 8 bits or 2 nibbles. Hence one nibble has 4 bits, by shifting 4, 4 bits we can swap nibbles of a byte.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Swapping nibbles of a byte using C program

/*C program to swap two nibbles of a given byte*/

#include <stdio.h>

/* function : swapTwoNibbles, to swap two nibbles of a given byte.*/
unsigned char swapTwoNibbles(unsigned char n)
{
    unsigned char num;
    num = ((n & 0x0F) << 4 | (n & 0xF0) >> 4);
    return num;
}

int main()
{
    unsigned char number;
    unsigned char revNumber;
    printf("Enter an integer number (One byte number):");
    scanf("%u", &number);

    revNumber = swapTwoNibbles(number);
    printf("\nEntered Number was : %u \nNumber after swapping nibbles : %u", number, revNumber);

    return 0;
}
    Enter an integer number (One byte number):200

    Entered Number was : 200
    Number after swapping nibbles : 140

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C solved programs/examples on Bitwise Operators

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to counter number of 1\'s in an int... >>
<< C program to demonstrate example of left shift (&l...