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..
int/unsignedhaving 32 bits. Useuint32_tinstead. Left shifting a signed value chainging th sign is undefined behaviour even.