<<< Home << Web < Arithmetic Top Bottom < Prev Next >

Image Arithmetic
AND and OR

I really hate to do this to you, but there's just no way to understand AND and OR without knowing a little bit about the binary numbering system.

We're all familiar with the decimal numbering system. It consists of 10 digits, 0 through 9. To make bigger numbers than 9, we go to 2-digit numbers. The left-most digit represents a value times 10. So 39 is the same thing as 3*10 + 9. A bigger number such as 246 is 2*10*10 + 4*10 + 6.

Binary works the same way, except there are only the two digits 0 and 1. Instead of powers of 10, the digits represent powers of 2. So B111 (I'll tack a "B" on the front of the number so that you'll know I'm talking about binary 111 and not decimal 111) is 1*2*2 + 1*2 + 1, or 7.

Each of the red, green and blue values in a color uses 8 bits, so the largest value is B11111111, or 255. Since it gets very confusing having 8-digit values, hexadecimal representation is usually used. Hexadecimal (base 16) is again similar to decimal except that there are 16 digits (represented by 0-9 and a-f) and powers of 16 are used instead of powers of 10. So the value x43 is 4*16 + 3, or 67 (I'll tack an "x" on in front of hexadecimal values).

Here's a little table of decimal, hexadecimal and binary equivalents:

0 x0 B0000 4 x4 B0100 8 x8 B1000 12 xc B1100
1 x1 B0001 5 x5 B0101 9 x9 B1001 13 xd B1101
2 x2 B0010 6 x6 B0110 10 xa B1010 14 xe B1110
3 x3 B0011 7 x7 B0111 11 xb B1011 15 xf B1111

When the AND function is applied to two values, each of the corresponding "bits" (binary digits) are compared. If both are 1, the result is 1. Otherwise, the result is 0. Here's an example:

     10001101   x8d  ANDed with
     00101001   x29  Results in:
     00001001   x09

Note that the result of an AND is always a smaller number than either of the two values being ANDed (unless the two values are identical, causing the same value as the result).

When the OR function is applied to two values, each of the corresponding bits are compared. If EITHER is 1, the result is 1. Otherwise, the result is 0. Here's an example:

     10001101   x8d  ORed with
     00101001   x29  Results in:
     10101101   xad

Note that the result of an OR is always a larger number than either of the two values being ORed (unless the two values are identical, causing the same value as the result).

The results of ANDs and ORs are always values between 0 and 255. You will almost always want to use a divisor of 1 and a bias of 0. Remember that you can use a positive bias to lighten the result or a negative bias to darken the result. If you use a non-zero bias, you will usually want to use clipping to prevent colors from wrapping around.

Here's some things to keep in mind:

Here's some examples using divisor=1, bias=0:

Value 1 Value 2 AND OR
x000000 x7f0000 x000000 x7f0000
xffffff x0020c8 x0020c8 xffffff
xff0000 x00ff00 x000000 xffff00
xff00ff x335588 x330088 xff55ff

<<< Home << Web < Arithmetic Top Bottom < Prev Next >