ADVERTISEMENT
ADVERTISEMENT

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.

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

C Bitwise Operators Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.