A Bit About Bits

A Bit About Bits

The next few pages in this tutorial provide a little coding background info that can help you with your ELEV-8 hacking.

If you've programmed the Programmed the Propeller Multicore Microcontroller on the Activity Board or ActivityBot robot, you have have used some functions like:

high(5);
low(4);
button = input(3);
pause(500);

These functions make it easy to do things such as turn on or blink an LED or read a pushbutton.  You can use these functions whenever you have #include "simpletools.h" at the top of your program.

These functions are essentially convenience functions, and they make your code a lot simpler.  99% of the time, they are fast enough for your application and do exactly what you need them to do.  On the ELEV-8 v3, where a LOT has to happen VERY quickly, these functions are simply too slow - or in the case of the pause() function, stop the program entirely while the pause waits to complete.

In order to learn how to set pins high or low without simpletools.h, or set them as inputs and read their state, we have to learn a few things about the Propeller and a little bit about bitwise operators.

Binary Numbers

We are used to seeing numbers represented using digits, specifically base-10 digits.  When we count, we say "1..2..3..4..5..6..7..8..9..10.." and so on.  We probably developed this as a counting system because we have ten fingers.  At its core, a microprocessor like the Propeller is made up of thousands of transistors, and they can only be on or off.  When a computer or microprocessor counts with its "fingers", it only has two to work with.  Here is a comparison of how a person and a microprocessor count to 20:

We humans roll over to the next digit when we hit 10, then 20, 30, and so on.  A microprocessor rolls over after each 1 - every two times.  This counting system is called binary or base-2.  Once you understand it, it's incredibly useful.

Bit Shifting

Let's start with the number 1.  In binary, it's still just 1.  Just like with the decimal numbers we are used to, if we put a bunch of zeros in front of it, it won't change the value.  0000245 is the same as 245.  If you put a bunch of numbers in front of a binary number, it won't change the value.  It will, however, help you understand how binary numbers are stored inside the Propeller.

The Propeller microcontroller is a 32-bit processor.  When it does an operation, it does it with 32 bits at a time.  One of the easiest operations to understand is bit shifting.  If we start with the number one with all 32 bits written out, it looks like this (spaces are added to make it easier to read):

0000 0000 0000 0000 0000 0000 0000 0001

Now, we are going to left shift that number 1 space:

0000 0000 0000 0000 0000 0000 0000 0010

If you look at the table above, the binary number 10 is equal to the decimal number 2.  Let's shift it left 2 more spaces (3 total):

0000 0000 0000 0000 0000 0000 0000 1000

If you look at the chart above, this is equal to the decimal number 8.  If you are good with math, you may have noticed that 8 is 2 × 2 × 2.  Every time we shift to the left, it's the same as multiplying by 2!

We can go the other way as well.  If we start with 1000, and right shift it 2 times, we get 0010.  We went from 8 to 2.  Right shifting is basically dividing by two: 8 ÷ 2 ÷ 2 = 2.

When we are writing programs, we can use the symbols << to left shift and >> to right shift a number.

Try this

Here is a program that lets you try shifting numbers left and right.  Open SimpleIDE and connect your Flight Controller to your computer.  

Open a new C project, copy the code below, and paste it into SimpleIDE:

/*
Bit shifting example
*/

#include "simpletools.h"                           // Include simpletools library

int theNumber;                                     // Declare variables
char shiftDirection;
int shiftAmount;

int main()                                         // Main function
{
  while(1)
  {
    print("Enter a number: ");                     // User prompt to enter the number
    scan("%d\n", &theNumber);                      // Scan what the user types

    print("Enter \"<\" to shift left and \">\" to shift right: ");
    scan("%c\n", &shiftDirection);

    print("Enter the amount to shift: ");
    scan("%d\n", &shiftAmount);

    print("\n(%d)\t%b %c%c %d = ", theNumber, theNumber, shiftDirection, shiftDirection, shiftAmount);
  
    if( shiftDirection == '<' )                    // Determine which way to shift
    {                                              // the number
      theNumber = theNumber << shiftAmount;        // Left shift
    } else {
      theNumber = theNumber >> shiftAmount;        // Right shift
    }

    print("%b\t(%d)\n\n", theNumber, theNumber);   // Print the shifted number
  }
}

Plug your Flight Controller into your computer, make sure the right port is selected, and click Program > Run with Terminal.  You will see this:

Type in a number and hit enter.  Then type "<" or ">" to shift the number left or right.  Then type a number representing how much to shift the number.

Your result will look something like this:

Try different combinations of numbers, directions, and amounts until you have a better understanding of how binary numbers and bit shifting work.