cullener
cullener

Reputation: 113

c unsigned integer bitshift

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

Upvotes: 1

Views: 889

Answers (1)

Steve Cox
Steve Cox

Reputation: 2007

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;

Upvotes: 3

Related Questions