# Using Bitwise Operators

### Using Bitwise Operators

Before you begin reading this section, you might want to download the source files linked to on the first page of this article so that you can reference examples of these operators in action as you read.

Bitwise operators are utilized in a similar fashion to any other mathematical operator. What makes them unique is that rather than operating on an entire number, they affect each bit (or digit) in a binary number individually. That is, they are wise to the bits—bitwise!

For instance, if I were to apply a boolean NOT operator to the number 9, I would get “false” back because 9 would be converted to a single boolean value (“true”), and then have the NOT operator applied. With a bitwise NOT operator, I would get 6 back. This is because the bitwise NOT flips each bit in the binary representation of 9 separately, so 1001 (9 in binary) becomes 0110, which is 6 in decimal.

Just as with most mathematical operators, you can apply all bitwise operators except ~ (NOT) normally, such as:

result = source1 & source2 result = source << 3

Or by using shortcut assignment operators like the following:

source1 &= source2 source <<= 3

In the next sections, Tables 1 and 2 explain most of the bitwise operators available in Flash. The first two examples for each operator appear in pseudo code because there is no way to type a binary value directly into ActionScript. The third example uses hex values in ActionScript.

#### Combinational Operators

The operators in Table 1 are used to combine two binary numbers. For each bit in the two numbers, there are four possible combinations (1 and 1, 1 and 0, 0 and 1, 0 and 0). The truth table for each operator shows these combinations and their outcomes.

Name | Description | Truth Table | Examples | ||||
---|---|---|---|---|---|---|---|

A | _ | B | = | ||||

& | AND | Sets the result bit to 1 only if both source bits are 1. | 0 | & | 0 | 0 |
1001 & 1100 equals 1000 11 & 101 equals 1 (001) 0xF0F & 0xFF0 == 0xF00 |

0 | & | 1 | 0 | ||||

1 | & | 0 | 0 | ||||

1 | & | 1 | 1 | ||||

| | OR | Sets the result bit to 1 if either of the source bits is 1. | 0 | | | 0 | 0 |
1001 | 1100 equals 1101 11 | 101 equals 111 0xF0F & 0xFF0 == 0xFFF |

0 | | | 1 | 1 | ||||

1 | | | 0 | 1 | ||||

1 | | | 1 | 1 | ||||

^ | XOR | Sets the result bit to 1 only if either, but not both, of the source bits is 1. Hence the name eXclusive OR. | 0 | ^ | 0 | 0 |
1001 | 1100 equals 1101 11 | 101 equals 111 0xF0F & 0xFF0 == 0x0FF |

0 | ^ | 1 | 1 | ||||

1 | ^ | 0 | 1 | ||||

1 | ^ | 1 | 0 |

#### Other Operators

The operators in Table 2 all manipulate a single binary number.

Name | Description | Examples | |
---|---|---|---|

~ | NOT | Flips each bit. Ones become zeroes and zeroes become ones. |
~10010 equals 1101 (01101) ~111 equals 0 (000) ~0xF0F == 0xF0 (0x0F0) |

<< | Left shift | Shifts each bit the specified number of positions to the left, filling the new (least significant) positions with zeroes. | 10010 << 1 equals 100100
10010 << 2 equals 1001000 0xF0F << 2 == 0xF0F00 |

>> | Right shift | Shifts each bit the specified number of position to the right, discarding the rightmost (least significant) bits. |
10010 >> 1 equals 1001 10010 >> 2 equals 100 0xF0F >> 2 == 0xF |

By combining these operators, you can isolate and manipulate individual bits—and ultimately individual color channels—efficiently.

## Comments