# How to swap low-order byte & the high order byte in an integer without using temporary variable?for ex: int a=20;It is represented in bits as (i.e 16 bit format)since integer is of two bytes00000000 00010100i want the answer as00010100 00000000ie 5120 as the answer

#### uddin2000 Profile Answers by uddin2000

• Sep 13th, 2006

Hi,

Can we write a program like this

void main(void)
{
int a=20;
printf("%dn",a);
a= (a<<8) | (a>>8);
printf("%dn",a);
}

#### BIJOYA

• Sep 13th, 2006

Better will be no = ( ( no & 0xff ) << 8) | ( no >> 8 )

#### Anand Krishnan

• Oct 24th, 2006

i think we can use this code in function

int i=52;

asm{

rol i,8

}

#### NHKR

• Oct 30th, 2006

void swaploworder(){ unsigned int r=20; unsigned short *p; printf("Inside swaplowordern"); printf("the size of short is %dn",sizeof(short)); p=(short*)&r; printf("The value of r is %dn",r); printf("The value at upper half is %dn",*p); printf("The value at lower half is %dn",*(p+1)); *p=*p+*(p+1); *(p+1)=*p-*(p+1); *p=*p-*(p+1); printf("The value at upper half is %dn",*p); printf("The value at lower half is %dn",*(p+1)); printf("the new value of r is %dn",r);}

#### Arup Ratan Banerjee

• Jan 29th, 2007

1) int x=5; int y =100; x=x ^ y; y=y^x; x=x^y; printf("%d , %d ", x,y);2) int x=5; int y=100; x=y-x; y=y-x; x=y+x; printf("%d , %d", x,y);

Send   Reset