C++ - What are the effects of & Hexadecimal operator?

Asked

Viewed 88 times

0

I’m with a project of a Gameboy emulator for android, so far what I got was:

  • Identify the game type (Gameboy, Super Gameboy, Gameboy Color, etc).
  • Identify the game region.
  • Extract the game title.

First of all all all all the references to build it I’m taking of this website who mapped the entire Gameboy, and this cpu manual (out some open-source emulators).

In the code snippet I use to identify the type of game I do as follows:

if (m_ROM[0x0143] & 0x80){
        __android_log_write(ANDROID_LOG_DEBUG, "Cartridge", "GameBoy Color Game");
        return 1;
    }else if(m_ROM[0x0146] & 0x03){
        __android_log_write(ANDROID_LOG_DEBUG, "Cartridge", "Supper GameBoy Game");
        return 2;
    }else{
        __android_log_write(ANDROID_LOG_DEBUG, "Cartridge", "GameBoy or other");
        return 0;
}

It works perfectly, it is even easy to understand because it is all very well documented, but what I cannot understand is what & is doing, from what I read in some books that I have it works to get the address of something as a reference operator, or turning bits off, that leaves me confused, because in practice this as "if the address 0x0143 contain 0x80 then it is a game of Gameboy color".

  • 1

    @Jeffersonquesado even the mentioned question is in Javascript, the answer would be for C++? I’m asking because I don’t know deeply Javascript.

  • Thank you for warning !

  • 1

    @lemoce bitwise operators are the same. Perhaps some change in the amount of bits of the variables involved, which in C++ will occur integer promotion, and endianism would interfere. Apart from these corner cases (which do not affect this specific issue), the behavior is exactly the same

1 answer

1

The operator & do an operation E binary, i.e., does the operation on each bit (bit by bit) for this, the two variables must have the same amount of bits, or the compiler will cast depending on the operation, because similarly we have the | which is the binary OR.

It is easier to see in binary (if you know this level). Take as an example 0x0143, which in binary is: 0b101000011, and 0x80, that is: 0b10000000. As you can see the number 0x80 has only one bit "1" (the eighth), so the remaining bits will be zeroed, and as the eighth bit of 0x143 is "0", so the result of 0x143 & 0x80 will be 0.

Second example: 0x146 = 0b101000110, 0x3=0b11.

  101000110
& 000000011
------------
  000000010

I.e., the result is 0b10 = 2; unlike the logical operator && which results in a boolean value, the binary operator & results in a number.

If it was an operation OU:

  101000110
| 000000011
------------
  101000111

Result 0b10100111 = 0xA7 = 167

Truth table for E

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

Truth table for OU

1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0

Note that your code snippet is operating m_ROM[0x0143], then the value of this is operated with 0x80 and 0x03.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.