假设我有数字54998,其二进制为:
54998=11010110 11010110
现在我必须把这个数字分成另外两个8字节大小的二进制数,例如:
11010110=214 and 11010110=214
有什么用C语言写的想法吗?
我想过把数字8向右移动并找到第一个数字,但是有没有其他方法可以做到这一点?
number>>8;
您可能正在寻找位屏蔽:
void more_stuff(uint32_t value) { // Example value: 0x010203
uint32_t byte1 = (value >> 16); // 0x010203 >> 16 is 0x01 so
// no masking is necessary
uint32_t byte2 = (value >> 8) & 0xff; // 0x010203 >> 8 is 0x0102 so
// we must mask to get 0x02
uint32_t byte3 = value & 0xff; // here we only mask, no shifting
// is necessary
}