I have a pointer of (signed) int8_t
int8_t *data
It comes from a netcdf file, in which data is encoded as a int8 array. To limit the file size and use the cheapest representation as possible, it is in reality a successions of signed integers of different sizes (4,8,16 and 32 bits), whose organization is specified elsewhere and not relevant to that question.
If I want to interpret the data as int16 or int32 I can just do (updated after StoryTeller comment on undefined behaviour of the use of a reinterpret_cast in this case):
int16_t data_16
memcpy(&data_16, &data[index], sizeof(int16_t));
int data_16_32 = data_16
However, if the data is to be interpreted as int4 (two int4 in a single int8 memory space), how can I retrieve the int4 values? The int4 type does not exist in C.
My question in short: Ho to interpret a int8 variable as two int4
I guess this topic could be useful, but i do not really understand: https://codereview.stackexchange.com/questions/30593/split-a-long-integer-into-eight-4-bit-values