GeekInterview.com
Series: Subject: Topic:
Question: 439 of 828

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 bytes
00000000 00010100

i want the answer as

00010100 00000000

ie 5120 as the answer

Asked by: Interview Candidate | Asked on: Sep 12th, 2006
Showing Answers 1 - 5 of 5 Answers
uddin2000

Answered On : Sep 13th, 2006

View all answers by uddin2000

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

  
Login to rate this answer.
BIJOYA

Answered On : Sep 13th, 2006

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

  
Login to rate this answer.
Anand Krishnan

Answered On : Oct 24th, 2006

i think we can use this code in function int i=52; asm{      rol i,8}

  
Login to rate this answer.
NHKR

Answered On : 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);}

  
Login to rate this answer.
Arup Ratan Banerjee

Answered On : 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);

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.