©2018 by memoryfaults.com. Proudly created with Wix.com

Search

Swap even & odd position bit

/*****************************************************************************************************/

/*This program swap the even position bit and odd position bit using bitwise operator................*/

#include <stdio.h>

#include <limits.h>

void SwapAdjBits(int num);

short Conversion(int num);

int main()

{

int num;

TEST:printf("enter the number....\n");

scanf("%d",&num);

if( num < 0)

{

printf("please enter the positive number....\n");

goto TEST;

}

printf("Number before swap adjecent bits....\n");

Conversion(num);

SwapAdjBits(num);

}

/******************************************************************************************/

/*Function Name :Conversion() */

/*Parameter :int number */

/*Return Value :short on successful conversion 1,on error 0 */

/*Description :This function convert the decimal number into binary ,hex and octal*/

/*******************************************************************************************/

short Conversion(int num)

{

int i;

//for(i=31;i>=0;i--)

/*for more portable program......*/

for(i=sizeof(int) *CHAR_BIT - 1;i>=0;i--)

{

printf("%d",(num >> i)&1);

if(i%8==0)

{

printf(" ");

}


}

printf("\n");

}

/******************************************************************************************/

/*Function Name :SwapAdjBits() */

/*Parameter :int number */

/*Return Value :void */

/*Description :This function swap the adjecent bits...............................*/

/*******************************************************************************************/

void SwapAdjBits(int num)

{

num = ((num & 0x55555555) << 1) | ((num & 0xAAAAAAAA) >> 1);

printf("Number after swaping adjecent bits....\n");

Conversion(num);

}

7 views