AND, OR, and XOR Combinations

Background

Consider the following:

0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1
2 0 0 2 2 0 0 2 2
3 0 1 2 3 0 1 2 3
4 0 0 0 0 4 4 4 4
5 0 1 0 1 4 5 4 5
6 0 0 2 2 4 4 6 6
7 0 1 2 3 4 5 6 7

Can you see how the table is filled in? This may be easier:

00 01 10 11
00 00 00 00 00
01 00 01 00 01
10 00 00 10 10
11 00 01 10 11

The squares of the table are the “and” of the binary-representation of the numbers. See this great Wikipedia article for more information about bitwise operations. Here, I look at the combinations of “and”, “or” and “xor” in a grid. I once saw a 3D model of the above in which the cells of the table were like little towers and I was fascinated. I've created a similar model here except that the cells are colored from dark to light with black being 0 and white being 255. Though not exactly automata, I find the patterns similar enough and the code to generate them uses my same drawing function on the automata page.

Bitwise AND Combination

The bitwise AND combination.

Bitwise OR Combination

The bitwise OR combination.

Bitwise XOR Combination

The bitwise XOR combination.

Code

Here's the code to create the bitwise “and” combination. If you change the line of code in the doubly nested “for” loop, you can easily produce the other images as well.

// Author: Grant Jenks
// File:   BitAnd.cs
// Build:  csc.exe /R:Automata.Draw.dll BitAnd.cs
// Run:    BitAnd.exe
// View:   BitAnd.jpg
// Copyright 2009
 
using System.Drawing;
 
namespace Automata
{
class BitAnd
{
    public static void Main
    (
        string[] args
    )
    {
        // Since the RGB values can range from 0 to 255, set the size of the
        // array to 256 and initialize the array.
 
        const int size = 256;
        int[,] values = new int[size, size];
 
        // Set the array values to the bitwise AND combination of the row and
        // column values.
 
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                // To compute the bitwise OR and XOR functions, simply replace
                // the "&" below with "|" or "^" respectively.
 
                values[i, j] = i & j;
            }
        }
 
        // This viewer produces a simple gray-scale gradient from the given
        // integer value. Black is (0, 0, 0) and white is (255, 255, 255).
 
        Automata.Draw.Viewer viewer = (i => Color.FromArgb(i, i, i));
        Automata.Draw.Write("BitAnd.jpg", size, 2, values, viewer);
    }
}
}
projects/automata/looking_at_and_or_and_xor_combinations.txt · Last modified: 2009/10/17 20:04 by grant