问题
Is there a simple way to split one 64-bit (unsigned long long
) variable into eight int8_t
values?
For example:
//1001000100011001100100010001100110010001000110011001000110011111
unsigned long long bigNumber = 10455547548911899039;
int8_t parts[8] = splitULongLong(bigNumber);
parts
would be something along the lines of:
[0] 10011111
[1] 10010001
[2] 00011001
...
[7] 10010001
回答1:
{
uint64_t v= _64bitVariable;
uint8_t i=0,parts[8]={0};
do parts[i++]=v&0xFF; while (v>>=8);
}
回答2:
First you shouldn't play such games with signed values, this only complicates the issue. Then you shouldn't use unsigned long long
directly, but the appropriate fixed width type uint64_t
. This may be unsigned long long
, but not necessarily.
Any byte (assuming 8 bit) in such an integer you may obtain by shifting and masking the value:
#define byteOf(V, I) (((V) >> (I)*8)&UINT64_C(0xFF))
To initialize your array you would place calls to that macro inside an initializer.
BTW there is no standard "binary" format for integers as you seem to be assuming.
回答3:
I think if it can be this: 64bit num %8 ,save this result,and then minus the result then divide the result by 8 last save divided num and save (64bit num %8) num, and last you get two 8bit num , and you can use this two num to replace 64bit num 。 but when you need to operate , you may need to operate 8bit num to 64 bit mun
回答4:
You should be able to use a Union to split the data up without any movement or processing.
This leaves you with the problem of the resulting table being in hte wrong order which can be easily solved with a macro (if you have lots of hard coded values) or a simple "8-x" subscript calculation.
#define rv(ss) = 8 - ss;
union SameSpace {
unsigned long long _64bitVariable;
int8_t _8bit[8];
} samespace;
_64bitVariable = 0x1001000100011001100100010001100110010001000110011001000110011111;
if (_8bit[rv(1)] == 0x10011111) {
printf("\n correct");
}
来源:https://stackoverflow.com/questions/20041899/how-to-split-a-64-bit-number-into-eight-8-bit-values