If you can not see the iframe below then click here to view the program code.
 Binary Operations         Electronic computers use binary (base 2) to store numbers in memory, for the simple reason that each
binary digit (bit) can be represented by an electronic 9or optical) switch as on (1) or off (0).

In familiar base 10 each number consists of digits taking values in the range 0 - 9, with each digit
increasing in value by a factor of ten from right to left: Similarly, in binary each digit can take a value of 0 or 1 and each digit-column increases 2-fold from right
to left. In the above example, the decimal number 89 is represented in binary as 1011001.

Understanding binary numbers, including binary arithmetic and operations that switch selected bit values,
is important in certain aspects of computing. The small C++ program below illustrates some elementary
bitwise operations, such as AND, OR, XOR, NOT and bitwise shifts and also binary arithmetic. It uses the
itoa (integer to character conversion) function to display the results in binary format. I have compiled and
tested it using the GNU gcc compiler in CodeBlocks on a Windows platform.
Tricks with Binary Numbers

1. Logical Operators

An operator represents a mathematical operation, such as multiplication and addition. Binary operators,
like addition operate on two numbers or operands, a unary operator on just one.

The AND operator, symbol &, a binary operator, compares two binary numbers, by comparing each
equivalent digit at a time, and sets the corresponding digit in the result to 1, if both digits in the operands
are 1, and 0 otherwise:

using the two operands: 111000 (56 in base 10) and 101010 (42 in base 10):

e.g. 111000 & 101010 = 101000 (40)

OR (inclusive OR, symbol |) assigns a bit value of one in the result if either or both of the bits in the two
operands are equal to 1:

e.g. 111000 OR 101010 = 111010 (58)

XOR (exclusive OR, symbol ^) assigns a bit value of 1 either one or the other, but not both, of the
corresponding digits in the operands is 1:

e.g. 111000 ^ 101010 = 010010 (18)

NOT (or one's complement, symbol !) switches the value of each bit and operates on a single operand:

e.g. !111000 = 000111 (7)
e.g. NOT(101010) = 010101 (21)

2. Powers of two

To test whether or not a number is a power of two, one can subtract the number - 1 from itself and AND
the result with the original number, if the result is zero, then the number is a power of two.

If you have compiled the program above then you can try things like:

E.g. 1

72 AND (72 - 1), that is 72 AND 71:

72 in binary is: 1001000
71 in binary is: 1000111
(1001000 - 0000001 = 1000111)

1001000 AND 1000111 = 1000000,

as the result is not zero, 72 is not a power of 2.

E.g. 2

64 AND (64 - 1), that is 64 AND 63:

64 in binary is: 1000000
63 in binary is: 0111111
(1000000 - 0000001 = 0111111)

1000000 AND 0111111 = 0000000 = 0

as the result is zero, 64 is a power of 2.

3. Setting bits

OR can be used to set specific bits (that is to switch them on or change their value to 1):

E.g.
To set the rightmost bit:
000000 OR 000001 = 000001

To set the 16's bit:
000001 OR 010000 = 010001

4. Unsetting bits

AND NOT (AND the value with its one's complement) can be used to unset bits (that is switch it off or set
its value to 0):

E.g.
starting with 010000
!010000 = 101111
010000 AND 101111 = 000000

5. Toggling bits

XOR can be used to flip bits:

E.g.
initial state: 010001
Switch second bit from left off:
010001 XOR 010000 = 000001

Switch second bit from left on:
000001 XOR 010000 = 010001

Color is a good example. In 24-bit colour we have three colour channels: red, green and blue (RGB) or
BGR in some systems, in which the intensity of each of the
three colour channels is represented by
one byte of 8 bits. In
32-bit colour, we have a fourth channel, the alpha-channel, which can be used to
represent transparency (strictly translucency) as RGBa or aBGR. Using the aBGR system we represent
100% opaque deep-sky blue as #00BFFF. The # means that this number is in base 16 (hexadecimal,
sometimes indicated by the prefix 0x) which is convenient as two digits of a hex number can represent 8
digits of a binary number (one hex digit represents one
nibble or 4 bits, 2 nibbles = one byte). In
hexadecimal each digit can span the values 0-15, with 0-9 then a, b, c, d, e, f, for 10, 11, 12, 13, 14 and
15 respectively. This is using the RGB 24-bit colour system. In aBGR this would become: #00FFBF00. In
base 10 this latter value is breaks down into: alpha = 0, blue = 255, BF = (11 x 16) + 15 = 191, and in
binary this value is:

aBGR:   00000000   11111111   10111111   00000000

We can extract just a single colour channel by using the following bit masks: