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++ > ORing & ANDing bits together...

#22445 - Wriggler - Mon Jun 21, 2004 1:25 pm

Hi guys,

Whilst relatively new to GBA programming, I have a small bit of C experience. However, I just want someone to clarify for me what this means (code taken from Pern Project Sprite tutorial):

Code:

sp->attribute1 = sp->attribute1 & 0xFE00; //clear the old x value
sp->attribute1 = sp->attribute1 | x;


Now I can see that the first line ANDs the hex value and attribute one. The comment informs that this clears the memory there. How exactly does this work? I don't understand how the "&" actually manages the memory and clears the pointer?

Also, the second line ORs the variable "x" to the attribute1 pointer. I assume this "adds" the value of x to the memory at attribute1. Again, what exactly is going on here?

I know this is fairly basic C, so apologies if I'm wasting anyone's time. I'm just after an explanation of the functionality of AND and OR, and a description of the difference between them.

Thanks a lot in advance!

Cheers,

Ben

#22446 - ProblemBaby - Mon Jun 21, 2004 1:43 pm

0xFE00 = 1111 1110 0000 0000
in bits
the attribute is 16 bits big and the x-coordinate takes the eleven first
bits

when you do the AND-operation
you simple check two values if the both bits are same the result is 1
else 0

maybe it look something like this:

1001 1010 0001 0000 x-coordinate = 32
and then AND with
1111 1110 0000 0000
=
1001 1010 0000 0000
the first 7bits stay the same but the last clears out to zero

OR is if one of the bits is true

BUT you must do the &-operation first to clear out everything
then you can write the new data!

You use the OR to dont destroy the 7 first bits

Hope you got that, I didnt;)

#22484 - Wriggler - Tue Jun 22, 2004 9:08 pm

Hi ProblemBaby, thanks for that explanation, it really helped me out.

Just to clarify, would I be right in saying that AND checks if both values are 1 (instead of just the same)? I ask because in your example, if both bits are 0 then the result was still 0. However, they were the same, so I would have expected one.

AND- True if both bits are equal to 1
OR- True if either bit is equal to 1

Is that correct?

Thanks,

Ben

#22485 - tepples - Tue Jun 22, 2004 9:21 pm

Wriggler wrote:
AND- True if both bits are equal to 1
OR- True if either bit is equal to 1

Is that correct?

Yes. There exist even more bit operators:
  • XOR (a ^ b): True if only ONE of the two bits is equal to 1 (that is, 1 if they're different)
  • XNOR or EQV or IFF or <=> (a ^ ~b): True if NEITHER or BOTH bits is equal to 1 (that is, 1 if they're the same)
  • IMPLIES or => (~a | b) : True except when A is 1 and B is 0 (unlike most other common bit ops, this one is not commutative)

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

#22487 - Wriggler - Tue Jun 22, 2004 10:08 pm

Excellent, thanks a lot to both of you for your help! Is there any way of leaving good feedback to those on this forum who have done such a good deed? (I'm new here y'see!)

Cheers,

Ben