Introduction to Boolean Logic

A Guide to Using AND OR NOT XOR in True/False and Bitwise Operations

© Guy Lecky-Thompson

Introduction guide to Boolean Logic for programmers with common operators such as AND, OR, NOT and XOR using bitwise operations for illustration purposes.

Introduction

This tutorial guide details the main Boolean Logic operators, and gives some useful information about how they can be used in bitwise operations. We shall also cover uses for bitwise operations and Boolean Logic in programming. The reader should be familiar with:

Boolean Logic

Boolean Logic is based around the evaluation of binary values. Examples of binary values are:

Any decision making machine will essentially boil down the vast majority of conditions to a binary evaluation. In programming we talk about operators that take two inputs and return a single output. This is modeled on the first binary computing systems called logic gates, of which only one (the AND gate) is required in order to build every other kind of decision making mechanism. At the heart of modern electronics are many millions of such gates, of which there are four distinct types that have been implemented in programming languages:

With these four logical operators, we can build complex comparison statements, and manipulate data using bitwise operations in which each bit in a given byte is evaluated with reference to the equivalent bit in a second byte, using logical operations, to yield the result. The key is in understanding the logical operators, or gates.

A quick note : although we are using the term gates and operators interchangeably, any electronic engineer will know that the presentation of the four operators here does not actually represent the equivalent in electronic terms. The oversimplification is due to the fact that only these four are typically implemented in programming.

The AND Operator

The simplest of the logic operators is the AND gate:

  "The output is True if both inputs are true."

This can be represented by the table:

0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1

The OR Operator

The OR gate can be expressed as:

  "The output is True if one of the inputs are true."

This can be represented by the table:

0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1

Note that if both inputs are true, then the condition stated above is satisfied.

The XOR Operator

An XOR gate has the following table:

0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

The full name for this is the eXclusive OR gate:

  "The output is True if exactly one of the inputs are true."

The NOT Operator

This is a simple inversion operator:

  "The output is False if the input is True, and True if the input is False."

Bitwise Operations

Given that a byte comprises 8 bits, arranged in such a fashion that a number between 0 and 255 can be represented (or -127 to +127 signed), we can apply these tables to individual bits in a byte. This is known as a bitwise operation. For example, supposing we had two numbers : 42 and 224. These would be represented as:

42 = 0 0 1 0 1 0 1 0
224 = 1 1 1 0 0 0 0 0

If we wished, we could combine these by way of an XOR (sometimes known as a masking) operation:

0 0 1 0 1 0 1 0
XOR 1 1 1 0 0 0 0 0
= 1 1 0 0 1 0 1 0

The result is that, everywhere that there are two corresponding bits in the bytes set to 1, the corresponding output bit is also set to 1. So, 42 XOR 224 is 202. We can do the same for AND and OR:

0 0 1 0 1 0 1 0
AND 1 1 1 0 0 0 0 0
= 0 0 1 0 0 0 0 0 (32)
0 0 1 0 1 0 1 0
OR 1 1 0 0 0 0 0 0
= 1 1 1 0 1 0 1 0 (234)

Putting a NOT gate behind the output inverses the above results, and is left as an exercise for the reader.


The copyright of the article Introduction to Boolean Logic in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Introduction to Boolean Logic must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo