문제

i have this code to split an unsigned 32 bit integer into a char array with each element of the array relating to 8 bits of the integer:

unsigned char result[4];
result[0] = (value >> 24) & 0xFF;
result[1] = (value >> 16) & 0xFF;
result[2] = (value >> 8) & 0xFF;
result[3] = value & 0xFF;

what changes would i need to make to split the 32 bit unsigned integer into 3 values: the 1st value relating to the first 8 bits, the 2nd value relating to the 2nd 8 bits and the 3rd value relating to the last 16 bits?

thanks

도움이 되었습니까?

해결책

Dont call them the first/last bits. Refer to them by their significance.

unsigned short result[3];
result[0] = (value >> 24) & 0xFF;
result[1] = (value >> 16) & 0xFF;
result[2] = value & 0xFFFF;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top