Qualcomm Interview Question: how to swap big endian and li... | Glassdoor

Interview Question

Software Engineer Interview Santa Clara, CA

how to swap big endian and little endian?

Answer

Interview Answer

4 Answers

0

swap the bytes one by one

Interview Candidate on Sep 23, 2010
2

unsigned int EndianSwap(unsigned int num)
       {
       unsigned int num, p1,p2, p3,p4 = 0;

       p1 = (num & 0x000000ff) > 8) | (p3 >> 16) | (p4 >> 24);

      return ret;
       }

Somebody on Jul 2, 2011
0

this is a bigger method.. but does ensure it works for 8 byte values.
irrespective of type being input.

void swap_endianess(long long x) {
   char * p;
   char * e;
   char t;
   int size = sizeof(x);

   printf("before = %lX%lX \n", x, *(&x + 1));
   p = (char *)(&x);
   e = (char *)(p + size - 1);

   while(p <= e) {
       t = *p;
      *p = *e;
      *e = t;
      p++;
      e--;
   }
   printf("after = %lX%lX \n", x, *(&x + 1));
}

Anonymous on Oct 27, 2011
2

unsigned int EndianSwap(unsigned int num)
{
  unsigned int swapEndian=0;
  swapEndian |= (num & 0x000000ff) > 8;
  swapEndian |= (num & 0xff000000) >> 24;
  return (swapEndian);
}

2Cents on Apr 8, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.