gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > | and the opposite?

#10243 - jenswa - Sun Aug 31, 2003 11:14 am

sprites[0].attribute0 = COLOR_256 | SQUARE;

this line 'combines' the number of COLOR_256 and SQUARE in sprites[0].attribute0
what if i would like to remove the SQUARE part, is there an operator available for it?

Oh and the example made much more sense if i used the display register for it,
because i would like to disable and enable one bg, by writing to there with only BGx_ENABLE
and not my complete display phrase again.

Jenswa
_________________
It seems this wasn't lost after all.

#10246 - antysix - Sun Aug 31, 2003 11:42 am

Use this:

To enable say bg0:

Code:

REG_DISPCNT |= BG0_ENABLE;


To disable:

Code:

REG_DISPCNT &= ~BG0_ENABLE;

_________________
Currently playing: NGC: Metroid Prime
GBA: Golden Sun: The Lost Age

Currently developping: Project ~ [ Phail ]

#10280 - Lupin - Mon Sep 01, 2003 2:42 pm

Maybe you'd like to know what's going on (if you already know this is just for reference ^^) when using the | operator:

| is logical OR, it's an bitwise operator, this means that if you have an bit value like this:

1011 0101 = 181
0001 1101 = 95
...the result of (181 OR 95) would be:
1011 1101 = 189

if you've an gba register and want to write some data to it, it'll most likely be cleared to a sequence of 0 bits, so if you just want to set the first two bits you'd use:
0000 0000 = 0
0000 0011 = 3

very simple though, but I felt like I'd better explain that here ^^

#10281 - jenswa - Mon Sep 01, 2003 3:24 pm

Thanks for the explaination, i got that with informatics.
Just didn't know the c/c++ code for it.

Thanks you guys.
_________________
It seems this wasn't lost after all.

#10286 - antysix - Mon Sep 01, 2003 4:46 pm

Ah well, I just explain my part too :).

Code:


REG_DISPCNT |= BG0_ENABLE;
 



Putting a sequence of bits in the register is obvious, just use the '=' operator, or, if there are bits in there already, use the '|' operator like Lupin explained already.


Code:


REG_DISPCNT &= ~BG0_ENABLE;
 


But if you want to set off your sequence of bits (actually reverse it), it's a bit different (but still easy to understand).
First you use the '~' operator which only takes one operand. It just makes the opposite of your bit sequence like this:

Code:


~1100 1011

gets:

0011 0100



Then you should put it in your register but instead of using bitwise OR
you use bitwise AND ('&'), because you want to put in the opposite value of which there already is. I'll explain now:

Code:


When using '|':

1100 1011
0011 0100

1111 1111    <<<gets this, which is bad

When using '&':

1100 1011
0011 0100

0000 0000  <<<gets this and this is exactly what you want, the previous register value is cleared.



Quote:


| is logical OR, it's an bitwise operator, this means that if you have an bit value like this:



Oh, probably just a typo, but '||' is logical OR, like '&&' is logical AND, '|' is bitwise OR, like '&' bitwise AND.
_________________
Currently playing: NGC: Metroid Prime
GBA: Golden Sun: The Lost Age

Currently developping: Project ~ [ Phail ]