Integers are represented in binary (base 2). For example, an 8-bit integer 3 is represented as 00000011. Some operators work on integers by looking directly at their binary representations.
This computes the bitwise and of integers x
and y. It works on each bit separately, treating 0 as false
and 1 as true. For example, the bitwise and of 8-bit integers
00001111 and 01010101 is 00000101. Notice that first bits have been and-ed
together, the second bits have been and-ed together, etc.
This is typically used for masking. One of the operands is the mask; a 0 represents a bit that will be 0 in the result and a 1 indicates a bit that will be taken from the other operand in the result. If x is a 32-bit integer then x & 0x000000FF yields the rightmost 8 bits of x. |
This computes the bitwise or of x and y. For example, 01000000 | 00000100 yields 01000100. This is typically used for combining bits. |
This is the bitwise complement of x. It just replaces each 0 by 1 and each 1 by 0. |
This is the bitwise exclusive-or of x and y. The result has a 1 in each position where exactly one of x and y has a 1. |
This is the result of shifting x to the left by n bits. Typically, the rightmost n bits of the result are 0. For example, using 8 bit numbers, 10101010 << 4 yields 10100000. |
This is the result of shifting xto the right n bits. Typically, the leftmost n bits of the result are the same as the leftmost bit of x. For example, 10101010 >> 4 yields 11111010. |