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

Showing Answers 1 - 5 of 5 Answers


  • Sep 13th, 2006


  Can we write a program like this

 void main(void)
  int a=20;
   a= (a<<8) | (a>>8);

  Was this answer useful?  Yes


  • Sep 13th, 2006

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

  Was this answer useful?  Yes

Anand Krishnan

  • Oct 24th, 2006

i think we can use this code in function

 int i=52;


      rol i,8


  Was this answer useful?  Yes


  • 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);}

  Was this answer useful?  Yes

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);

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account

Send   Reset


Related Answered Questions


Related Open Questions