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





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.