Boolean Operators in OpenScad – Counting in Binary

There are a few different categories of operators that exist in most typical programming languages. The most common are the arithmetic operators (+, -, *, /, etc). With these operators you can manipulate numbers, doing rudimentary arithmetic.

Then there are the logical operators (and: &&, or: ||, not: !). With these you can do some basic logic operations.

OpenScad, like most languages, supports many of these operators. The one category it does not support natively are the boolean operators. These are the operators that apply to numbers, in their boolean form (‘1’s, and ‘0’s).

For example. The number ‘5’ represented in boolean form is ‘1 0 1’. The number ‘3’ is represented as ‘1 1’. Here, the least significant bit is on the right.

Let’s say I want to perform an ‘and’ operation between these two binary representations. That is, I want: result = ‘1 0 1’ and ‘0 1 1’

The answer should look at each bit from each of these numbers, and when they are both ‘1’, the answer is ‘1’, otherwise, the answer is ‘0’. So, in this case, the result = ‘0 0 1’.

There are many situations where this becomes a useful construct. In typical programming languages, this is represented with a single ‘&’ character, as in: result = 5 & 3

The challenge with OpenScad is the boolean operators are not supported natively, so I want to construct them, using what is at hand. In the end, I want to be able to print a binary representation of a number.

Ideally what I would do is simply go through a loop, testing each bit of the number to see if it is ‘0’ or ‘1’ and printing a block accordingly.  That is in fact what I do, but it needs a little help.

First, let’s consider what binary representations are.  Our usual ‘base 10’ number system allows us to use 10 symbols to represent an infinite variety of numbers : 0,1,2,3,4,5,6,7,8,9.  In boolean, or ‘base 2’, we only have two symbols with which to represent numbers: ‘0, 1’.

What do these numbers represent though?  What’s a power of ‘2’?  The simplest way to understand it is to take the pow() function and apply it.  If you do the following:

pow(2,0) == 1
pow(2,1) == 2
pow(2,2) == 4
pow(2,3) == 8

By definition, pow(number, 0) == 1.  For all other instances, the series is this:
pow(number, 1) == number
pow(number, 2) == number * number
pow(number, 3) == number * number * number

Why is this significant and relevant?  Because, I can now use this binary representation to encode numbers larger than ‘0’ and ‘1’.

If I use a visual representation, we have the following:

 

 

First, we have the representation of ‘0’, or in binary ‘0 0 0 0’.  There are a couple of things of note here.  First is that we are representing 4 ‘bits’ here.  Each of those empty little boxes can be either empty, or full, which represents the two states of our binary system ‘0’ – empty, ‘1’ – full.  Also of note, the positions (starting from ‘0’ on the right) represents the second number we would stick into the pow() function that we saw earlier.  In the case of ‘0 0 0 0’, we have nothing represented.  We can not tell at the moment, but the ‘least significant bit’ is on the right.  That is, lower numbers are on the right, and higher numbers extend to the left.

 

 

 

Now we want to represent the decimal number ‘1’, which has a binary representation of ‘0 0 0 1’.  As we was previously, this is pow(2,0).  That means, that the 0th position should be filled.  So, we see a full box in the rightmost position.

 

 

Now for ‘2’.  It has a binary representation of ‘0 0 1 0’.  Easy enough, just fill in the next box over.  This is represented by pow(2,1).

 

 

And last, we want to represent the value of decimal ‘4’, which has a binary representation of ‘0 1 0 0’.  Again, just fill in the box where the ‘1’ exists in the binary form, and it’s represented by pow(2,2).

In very simple terms, the easiest way to see a boolean representation of a decimal number is to pull out your calculator and simply type the number in, and switch to ‘programmer binary’ mode, and see what number it shows you.  At least on Windows and Mac, you can do this with the built in calculator.

Last, is to add two numbers together.  For very simple numbers, we can use this visual representation:

Let’s say we want ‘5’.  Well, that’s simply 1 + 4, so we have:

+

==

And that’s that.  Very simple visual representation of binary numbers, and how to do simple arithmetic with them.

Now that we see how easy it is to represent binary numbers visually, the question is, how do you actually do that in OpenScad?

 

 

 

 

Advertisements

One Comment on “Boolean Operators in OpenScad – Counting in Binary”

  1. Andre says:

    function bit_set(b, n) = floor(n / pow(2, b)) % 2;

    echo( “BS 0, 5 =”, bit_set(0,5));
    echo( “BS 1, 5 =”, bit_set(1,5));
    echo( “BS 2, 5 =”, bit_set(2,5));
    echo( “BS 0, 2 =”, bit_set(0,2));
    echo( “BS 1, 2 =”, bit_set(1,2));
    echo( “BS 2, 2 =”, bit_set(2,2));


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s