我最近写了一些代码,它使用相同的无符号 short 来存储两个值,一个结果和一个 id,例如:
unsigned short data = new_id();
// result is either 0 or 1 so store it in the rightmost bit and move the id left
data = (data << 1) + get_result();
// ... later ...
// now we can print results like
printf("%u: %u\n", data & 1, data >> 1);
只使用结构来保存两个值会更好,还是这种类型的东西常见/可接受?该程序已经存储了如此多的内存,我想我会开始寻找减少它使用的内存的方法。