C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

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

Liked this article? Do share with your friends :)