【发布时间】:2016-05-12 01:59:44
【问题描述】:
我正在为 Intel 8080 制作一个模拟器。其中一个操作码需要 16 位地址,方法是结合 b 和 c 寄存器(均为 1 字节)。我有一个结构体,其中的寄存器彼此相邻。我结合这两个寄存器的方式是:
using byte = char;
struct {
... code
byte b;
byte c;
... code
} state;
...somewhere in code
// memory is an array of byte with a size of 65535
memory[*reinterpret_cast<short*>(&state.b)]
我想我可以OR他们在一起,但那行不通。
short address = state.b | state.c
我尝试这样做的另一种方法是创建一个短字节,并分别设置 2 个字节。
short address;
*reinterpret_cast<byte*>(&address) = state.b;
*(reinterpret_cast<byte*>(&address) + 1) = state.c;
有没有更好/更安全的方法来实现我想要做的事情?
【问题讨论】:
标签: c++ pointers casting byte short