The inside story of binary operations...
By MIKE BIBBY
IN previous articles, we've seen that binary numbers can be
added and subtracted just as our more familiar decimal numbers
are. And, of course, we can multiply and divide them.
There are, however, other ways of combining two binary numbers
that are extremely useful in dealing with computers. They're also
easy to use, so let's have a look at them!
Firstly, we'll see how we can NOT a binary number - simple,
one-bit numbers first. By the way, we're going to be dealing exclusively
with binary numbers this month, so we can drop the % sign.
The rules for doing a NOT are simple:
If the bit is 1 then it becomes 0
If the bit is 0 then
it becomes 1
If you like, the NOT converts a bit into its opposite.
So NOT 1 = 0
And NOT 0 = 1
Why do we use the word NOT? Well, mathematicians often use the
number 1 to mean TRUE and 0 to mean FALSE.
So NOT 1 means NOT TRUE, which means FALSE, which is 0. That
is, NOT 1 is 0. And, as NOT FALSE is most certainly TRUE, NOT
0 is 1.
If we are to NOT a binary number consisting of several bits,
we simply apply the rule for NOT to each bit individually.
So NOT 10110010
Some people think of this process as "turning the number
on its head" — so it's sometimes called inverting. Others
call it taking the complement of the number.
NOT just works on a single binary
number. However, there are other sums or operations that have
a set of rules for combining two binary numbers.
For instance we can AND two binary numbers. Let's look at the
rules for ANDing a single bit with another bit. When you think
about it, there are four possible combinations of bits that we
could AND: 0 with 0,0 with 1,1 with 0 and 1 with 1.
We write that we are ANDing, say, 0 with 1 as 0 AND 1.
The rules for ANDing are:
0 AND 0 = 0 (case a)
0 AND 1 = 0 (case b)
1 AND 0 = 0 (case c)
1 AND 1 = 1 (case d)
Notice that the only time the result is 1 (TRUE) is when the
two bits ANDed are both 1 (TRUE). This helps us to see why we
use the word AND to describe the operation.
If you think of the first bit as "this" and the second
bit as "that", what we're doing when we're ANDing is
asking whether "this and that" is true
"This and that" can only be true when both "this"
is true AND "that", is true - hence the use of AND to
describe the process.
For example, consider the statement that it is dry and sunny
This is true only if dry is true and sunny is true (case d).
If either of the two (or both) are false (cases a, b, c) the
whole statement is false, since it isn't both dry and sunny.
We can AND pairs of binary numbers of more than one bit - just
apply the rules of ANDing to each bit individually. For example
We can also OR two binary numbers. The rules for ORing a single
bit with another bit are as follows (again there are four possible
0 OR 0 = 0 (case e)
0 OR 1 = 1 (case f)
1 OR 0 = 1 (case g)
1 OR 1 = 1 (case h)
In this case, you only get a FALSE result (0) when both bits
are FALSE. If either or both bits are TRUE (1) the result is TRUE.
It's easy to see why we use OR to describe this. If one, OR the
other, OR both is true the whole thing is true!
Let's use the meteorological analogy again. Let's consider the
statement that it is dry or sunny
This is only FALSE when it is NOT dry and NOT sunny (case e),
otherwise it is TRUE (cases f, g, h).
To sum up, with OR, the whole thing is true if either or both
the things being ORed is true.
As we did with AND, we can OR pairs of numbers with more than
one bit - we just apply the rules of ORing to each bit individually.
• In the next article in this occasional series, we'll look
at EOR, and the use of masks.