Search code examples
cmemorybit-manipulationbyte

reversing byte order in c


I got problem solving 'reverseBytes' in cs:app datalab.

i have to make code that returns reversed byte order.

example : input=0x123456, returns=0x563412

when i used my code, it can't take a score..

int reverseBytes(int x) {
  int mask=0xff;
  int byte1=x>>24;
  int byte2=(x>>16)&mask;
  int byte3=(x>>8)&mask;
  int byte4=x&mask;
  int result=(byte4<<24)|(byte3<<16)|(byte2<<8)|(byte1);

  return result;
}

but, when i used other people's code, it takes a score.

int reverseBytes(int x) {
  int t2=~(0xff<<24);
  int s1=(0xff<<16)+0xff;
  int s2=0xff<<8;
  int s3=(s2<<16)+s2;
  int temp=(x&s1)<<8|((x&s3)>>8&t2);
  int q1=(0xff<<8)+0xff;
  int q2=q1<<16;
  int temp2=(temp&q1)<<16|((temp&q2)>>16&(~q2));

  return temp2;
}

i don't know why my code can't works.. i tested my code and other people's code. but i can't find difference between my code's result and another code's result. please help me..


Solution

  • The following functions swap bytes-strings of whichever length you want. The swapbytes() swaps all the bytes contained in the inp parameter inside inp thus modifying the contents of inp. The swapbytesout() swaps the contents of the inp paramenter inside the out parameter, then it doesn't modify the contents of inp.

    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    void * swapbytes(void *inp, size_t len);
    void * swapbytesout(void * inp, void *out, size_t len);
    
    /* This function swaps and modifies the content of inp
     *
     * Returns: a pointer to the output value (inp) */
    void * swapbytes(void *inp, size_t len)
    {
        unsigned int i;
        unsigned char *in=(unsigned char *)inp,tmp;
    
        for(i=0;i<len/2;i++) {
            tmp=*(in+i);
            *(in+i)=*(in+len-i-1);
            *(in+len-i-1)=tmp;
        }
    
        return inp;
    }
    
    /* This function doesn't swap and doesn't modify the content
     * of inp, the output is computed on out.
     *
     * Returns: a pointer to the output value (out) */
    void * swapbytesout(void *inp, void *out, size_t len)
    {
        unsigned int i;
        unsigned char *o=(unsigned char *)out;
        unsigned char *in=(unsigned char *)inp;
    
        for(i=0;i<len;i++) {
            *(o+len-i-1)=*(in+i);
        }
    
        return out;
    }
    
    int main(void)
    {
        uint32_t a0,a1;
        uint64_t b0,b1;
    
        a0=0x12345678;
        printf("%08X\n",*(uint32_t *)swapbytesout(&a0,&a1,sizeof(a0)));
        printf("%08X %08X\n",a0,a1);
    
        printf("%08X\n",*(uint32_t *)swapbytes(&a0,sizeof(a0)));
        printf("%08X\n",a0);
    
        puts("");
    
        b0=0x123456789ABCDEF0ULL;
        printf("%016"PRIX64"\n",*(uint64_t *)swapbytesout(&b0,&b1,sizeof(b0)));
        printf("%016"PRIX64" %016"PRIX64"\n",b0,b1);
    
        printf("%016"PRIX64"\n",*(uint64_t *)swapbytes(&b0,sizeof(b0)));
        printf("%016"PRIX64"\n",b0);
        return 0;
    }