vendredi 6 novembre 2020

When i extract bits of a hex number it is only extracting one

I have been developing a very simple and slow VM for my first big C++ project without watching a YouTube tutorial all the way through, and my instructions look like this: "0x01AA0B", where the first 2 are for the instruction, the middle 2 are for the register name, and the last two are the number to store in the register.

memory.mem is just an array uint16_t mem[UINT16_MAX]; When I tried to extract them, it was only taking the first number for the first 2 digits. Here is my code:

int insval = memory.mem[PC];
int ins = (insval >> 16) & 0xff;
int arg1 = (insval >> 8) & 0xff;
int arg2 = (insval) & 0xff;

It is saying that ins = 0. OK. so I did some debugging and I cant tell what is wrong here is some code I wrote to help me

std::cout << std::hex << insval << std::endl;
    std::cout << std::hex << ins << std::endl;
    std::cout << std::hex << arg1 << std::endl;
    std::cout << std::hex << arg2 << std::endl;

the output is

aa0a
0
aa
a

the intruction im providing it is 0x01AA0A

Aucun commentaire:

Enregistrer un commentaire