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.

Coding > question about XOR

#22922 - Darmstadium - Thu Jul 01, 2004 4:05 am

My goal is to clear out the bits that store the x and y in a OAM entry and put in new ones. I tried it like this:
Code:

sprites[0].attribute0 ^= 0xFF;
sprites[0].attribute0 |= test_y;
sprites[0].attribute1 ^= 0x1FF;
sprites[0].attribute1 |= test_x;

but that messes stuff up. Am I crazy or does XOR do the following?:
0 and 1 = 1
0 and 0 = 0
1 and 1 = 0

according to CowBite the y takes bits 0-7 of attribute 0 and the x takes bits 0-8 of attribute 1.

Thanks a whole bunch for your help

#22923 - tepples - Thu Jul 01, 2004 4:47 am

You're correct about the XOR (exclusive or) operator, notated in C as ^
Code:
0 ^ 0 == 0
0 ^ 1 == 1
1 ^ 0 == 1
1 ^ 1 == 0


It appears you probably want to clear a given set of bits in a register. To do that, use the BIC (bit clear) operator, notated in C as a compound operator &~
Code:
0 &~ 0 == 0
0 &~ 1 == 0
1 &~ 0 == 1
1 &~ 1 == 0

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#22929 - Lord Graga - Thu Jul 01, 2004 1:41 pm

I usually do this:

Code:
   whatSprite->attribute1 = (whatSprite->attribute1 & 0xFE00)| x;

   whatSprite->attribute0 = (whatSprite->attribute0 & 0xFF00)| y;

#22932 - jma - Thu Jul 01, 2004 3:32 pm

Just invert the bits you want to clear. Your |/^ is a common first try. The compiler will automatically take care of this think for you:

Code:
// clear bit 2 from 'x'
x &= ~(0x2);


Jeff
_________________
massung@gmail.com
http://www.retrobyte.org

#22933 - Cearn - Thu Jul 01, 2004 3:36 pm

Just be sure that x and y have been cut down to 9 and 8 bits,
respectively.
Code:

sprites[0].attribute0 &= 0xFF00;
sprites[0].attribute0 |= (test_y & 0x00FF);
sprites[0].attribute1 &= 0xFE00;
sprites[0].attribute1 |= (test_x & 0x01FF);

#22952 - Darmstadium - Thu Jul 01, 2004 7:56 pm

thanks a ton you guys

#23154 - SmileyDude - Tue Jul 06, 2004 2:19 am

tepples wrote:
It appears you probably want to clear a given set of bits in a register. To do that, use the BIC (bit clear) operator, notated in C as a compound operator &~


I don't want to be picky (especially with tepples), but actually that's two operators -- the bitwise and operator (&) and the bitwise compliment operator (~). If it was truely one operator, there would probably be a &~= operator as well, but there isn't.

But otherwise, everything is fine :)
_________________
dennis

#23159 - tepples - Tue Jul 06, 2004 3:26 am

SmileyDude wrote:
actually that's two operators -- the bitwise and operator (&) and the bitwise compliment operator (~).

True, which is why I called it a "compound" operator.

Quote:
If it was truely one operator, there would probably be a &~= operator as well, but there isn't.

What does a &=~ b do?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#23166 - SmileyDude - Tue Jul 06, 2004 6:28 am

tepples wrote:
What does a &=~ b do?


heh -- exactly what the &~= operator would do (if it existed, of course) :p
_________________
dennis