# Bitwise Boolean Logic

There is an interactive page on
bitwise logic in the *Interactive*
section. This page contains more detail about how the technique can be used to manipulate binary flags in your programs.

As well as applying Boolean Logic to two-state variables, you can also apply Boolean logical operators to integers (whole numbers). This is known as a bit-wise logical operation.

## Bit-wise Operations

Applying logical operators to integers is quite simple really - just write down the number in *
binary* form (see number bases) and perform the operation on each bit in turn (having first ensured that the *least significant bits* - i.e. the units - are lined up).

For example, what is *75 AND 125*?

75_{10} = |
0 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | |

125_{10} = |
0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | |

75_{10} AND 125_{10} = |
0 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | = 73_{10} |

So we can see that 75 AND 125 = 73 - not the answer you may have expected! You can do the same thing with OR:

75_{10} = |
0 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | |

125_{10} = |
0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | |

75_{10} OR 125_{10} = |
0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | = 127_{10} |

i.e. 75 OR 125 = 127 - generally using OR will give a larger answer than AND as more bits are going to be set.

## Complement

Bytes also have something called a *complement*. There are two types - 1s complement and 2s complement. To find the 1s complement of a number, simply invert all of the bits (i.e. change the 0s to 1s and 1s to 0s). The 2s complement is the 1s complement plus 1 (i.e. find the complement and add 1 to the answer). Two's complement is a common way of representing *negative numbers* in binary.

## Setting and Unsetting Bits

You can also use bit-wise logic to set and unset individual bits within a byte. To set a particular bit in a byte, simply use bit-wise OR with that byte, and a second byte with only that bit set. For example, to set the least significant bit:

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

OR | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |

= | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 1 |

You can unset the bit using a similar technique - only this time use AND and the complement of the bit you want to unset:

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

AND | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |

= | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |

## Combining and Masking Bits

Another use of bit-wise logic would be to combine groups of bits - for example to store two 4-bit numbers (or *nibbles*) in the same byte. This can be done by multiplying one of the nibbles by 16 to shift it along four bits (see number bases for an explanation of why), and then use OR. For example, to store 1011 and 1101 in the same byte:

_{2}x 16

_{10}= 10110000

_{2}

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

OR | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |

= | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 1 |

To separate the nibbles again, you can mask off the unwanted part using AND, e.g.

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

AND | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |

= | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |

As the ASCII codes for upper and lower case differ by 32 (which is a power of 2), you can use bitwise logic to change the case of text.

## What's the Point?

Good question! Well, back in the olden days, memory was very expensive, and there wasn't much of it in the average personal computer. In fact I quite clearly remember someone on *Micro Live* in the early 80s saying that 32k of memory is all you'd ever need. And he was partially right - what couldn't you do with a BBC Model B?

This led to cunning programming practices, and the popular use of bit-wise logic to store several *
flags* (or Boolean values) in the same *byte*. Now that computers have more memory, people don't really see the need.

Or, as Dijkstra (quasi-famous computery chap) predicted in 1979, "The advent of cheap and powerful devices will set computer science back 25 years!"

You can still use bit-wise logic to do other things, however, such as checking whether numbers are odd (because a *number* is odd if *number* AND 1 is true), or in combination with shifting techniques (see number bases), for example, to combine the separate red, green and blue components to produce a single
colour value (e.g. for HTML or VisualBASIC).