How can I set a variable to use exactly X bits?

355 Views Asked by At

I'm writing an MOS6502 emulator in Rust and I want to represent the condition codes as a struct.

so I have the following struct:

pub struct conditionCodes
{
    c: u8,
    z: u8,
    i: u8,
    d: u8,
    b: u8,
    pad: u8,
    v: u8,
    n: u8
}

I've already written a similar emulator in C and I used this method:

typedef strcut conditionCodes
{
    char c: 1;
    char z: 1;
    char i: 1;
    char d: 1;
    char b: 1;
    char pad: 1;
    char v: 1;
    char n: 1;
} conditionCodes;

To make every variable exactly 1 bit and to make the struct as a whole exactly 1 byte. I would like to use the same logic in Rust and I wanted to ask if there's a way to do this in Rust.

0

There are 0 best solutions below