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++ > bitwise 'and', bitwise 'or'

#25377 - black - Sat Aug 21, 2004 7:00 pm

howdy~

i read some code which comes a few bitwise operators such as 'and' and 'or', i searched some docs for help but could not understand the meaning yet. i'd appreciate anyone could show me some explainations(what about the answer and how they work) for expressions below, thanx in advance~
Code:

1111 & 1001 = ???
1111 | 1001 = ???;

_________________
Never end on learning~

#25381 - phonymike - Sat Aug 21, 2004 7:09 pm

it's a very simple process, you'll smack yourself when you see this:

1 & (AND) 1 = 1 because both 1's are 1's. 1 and 1 = 1
1 & 0 = 0 because only one of them is a 1. they must both be 1 to result in 1

1 | 1 = 1 because either of them is a 1.
1 | 0 = 1 because either of them is a 1.

so 0000 OR'd with 1111 results in 1111 because either are a 1.
1111 AND'd with 1111 results in 1111 because both have 1's.

maybe my explaination isn't the most simple, but it's pretty easy huh?

#25418 - rmy - Sun Aug 22, 2004 4:00 pm

Code:

  1111
& 1001
------
= 1001

  1111
| 1001
------
= 1111

#25422 - Cearn - Sun Aug 22, 2004 6:00 pm

Don't forget about the NOT (~A) and XOR (A^B) operations. NOT will invert all the bits of A. The result of XOR will be 0 if the bits of A and B are the same, and 1 if they are different. You could also see this as a selective NOT: bits of A will only be flipped if the corresponding bits of B are set.
Examples:
Code:

                1010
~ 1010        ^ 1100
------        -------
  0101          0110

The bit-ops are often used to do things to specific bits of a variable, using the second operand B as a mask. If you have certain bits of B set, then
Code:

A |= B      //will set those bits of A (to 1)
A &= ~B     // will clear them (set to 0)
A ^= B      // will toggle them (0 to 1 ; 1 to 0)
leaving all the bits for which B is 0 as they were.

When using bit-ops, don't ever forget they have a very low precedence, so it is wise to use lots of parentheses.

Truth tables:
Code:

 A | NOT
---+-----
 0 |  1
 1 |  0

  A | B ||  OR | AND | XOR
----+---++-----+-----+-----
  0 | 0 ||  0  |  0  |  0
  0 | 1 ||  1  |  0  |  1
  1 | 0 ||  1  |  0  |  1
  1 | 1 ||  1  |  1  |  0

#25423 - sgeos - Sun Aug 22, 2004 6:11 pm

Code:
AND
  0011        0000        1111
& 0101      & 1111      & 1111
------      ------      ------
= 0001      = 0000      = 1111


OR
  0011        0000        1111
| 0101      & 1111      & 1111
------      ------      ------
= 0111      = 1111      = 1111


XOR
  0011        0000        1111
^ 0101      ^ 1111      & 1111
------      ------      ------
= 0110      = 1111      = 0000


NOT
~ 0011      ~ 0000      ~ 1111
------      ------      ------
= 1100      = 1111      = 0000

Only the first column really needs to be studied. The other two are redundant.

-Brendan

#25443 - black - Mon Aug 23, 2004 4:00 am

woh i learn much from your comments ;)
now let me do some experiments on them:
Code:

1101 & 0011 = 0001;
1101 | 0011 = 1111;
1101 ^ 0011 = 1110;
~1101 = 0010;

am i right here ? and, thanx all gurus above !(not including me of course :D)
_________________
Never end on learning~

#25445 - tepples - Mon Aug 23, 2004 4:48 am

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

#25456 - black - Mon Aug 23, 2004 10:21 am

tepples wrote:
Those are correct.


thanx ! :)
_________________
Never end on learning~

#25470 - sajiimori - Mon Aug 23, 2004 7:33 pm

The Windows calculator has these functions in "scientific" mode.

#25477 - Lord Graga - Mon Aug 23, 2004 8:44 pm

A good way to remember AND and OR them:

AND: bit 1 AND 2 must be set
OR: bit 1 OR 2 bit must be set.

XOR is "strange" because only one bit may be set to return 1 :/

#25791 - NEiM0D - Sat Aug 28, 2004 5:44 pm

You remember OR and AND like + and - :).

#25797 - lordmetroid - Sat Aug 28, 2004 6:55 pm

And should be interpreted like multiplication rather then subtraction...
_________________
*Spam*
Open Solutions for an open mind, www.areta.org

Areta is an organization of coders codeing mostly open source project, but there is alot of sections like GBA dev, Language learning communities, RPG communities, etc...

#25851 - f(DarkAngel) - Sun Aug 29, 2004 4:22 pm

Generally, in logic lessons, and is defined as intersection, or as union.
_________________
death scream...