Home » C programs » C programs on Bitwise Operators

C program to swap two words/bytes.

This program will swap two bytes/words of an integer number, here this operation is implemented using bitwise shifting and bit masking.

Swapping two Bytes/Words using C program

    /* C program to swap bytes/words of integer number.*/
    #include <stdio.h>

    int main()
    {
	    unsigned int data=0x1234;
	    printf("\ndata before swapping : %04X",data);

	    data= ((data<<8)&0xff00)|((data>>8)&0x00ff);

	    printf("\ndata after swapping  : %04X",data);

	    return 0;
    }

    data before swapping : 1234
    data after swapping  : 3412




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




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.