C ++ - What are the effects of the & in Hexadecimal operator? [duplicate]

0

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

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

First of all, all the references to build it, I'm getting from this site that mapped the gameboy full, and from this cpu handbook (aside from 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's even easy to understand because everything is very well documented, but what I can not understand is what & is doing, from what I read in some books I have it works to get the address of something like reference operator, or turning off bits, which makes me confused, since in practice this is like "if address 0x0143 contain 0x80 then it is a color gameboy game. "

    
asked by anonymous 04.04.2018 / 12:57

1 answer

1

The & operator does a% binary% operation, ie, 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 do a cast depending of the operation, because similarly we have the E which is the binary OR.

It is easier to see in binary (if you are aware of this level). Take as an example | , which in binary is: 0x0143 , and 0b101000011 , which is: 0x80 . As you can see the number 0b10000000 only has a bit "1" (the eighth), logo the other bits will be zeroed, and since the eighth bit of 0x80 is "0", then the result of 0x143 will be 0x143 & 0x80 .

Second example: 0 , 0x146 = 0b101000110 .

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

In other words, the result is 0b10 = 2; different from the & & which results in a Boolean value, the binary & results in a number.

If it were a 0x3=0b11 operation:

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

Result 0b10100111 = 0xA7 = 167

Truth table for OU

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

Truth table for E

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

Note that your code snippet is running OU , so its value is operated with m_ROM[0x0143] and 0x80 .

    
05.04.2018 / 08:50