PBASIC Programming/Data Types

Binary Numbers
When we think about numbers that we use every day, we are thinking about numbers in base 10. This means that there are 10 different digits that we can use: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. Using these 10 digits and powers of 10, we can create any number in our system:


 * $$123 = 1 \times 10^2 + 2 \times 10^1 + 3 \times 10^0$$

In a binary or "base 2" system, we only have two digits: 0 and 1. To make a number in binary, we use powers of 2 instead of powers of 10:


 * $$10101 = 1 \times 2^4 + 0 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0 = 21$$

When talking about numbers in different systems, it is common to use subscripts. A subscript of 10 means the number is in base 10, and a subscript of 2 means the number is in base 2. For instance:


 * $$1010_2 = 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 0 \times 2^0 = 10_{10}$$

Common Data Types
Computers are very limited, and the space that they have to store numbers is fixed. This means that there are a certain number of bits available, no more and no less. Certain sizes of numbers are so common that they have special names.

Bit
A bit is a single binary number, either a 0 or a 1.

Nibble
A nibble is 4 bits, and can contain values from 0 to 15.

Byte
A byte is 8 bits (2 nibbles) and can contain values from 0 to 255.

Word
A word is 16 bits long (2 bytes or 4 nibbles) and can contain values from 0 to 65535.

Available Storage
The BASIC Stamp has 128 bytes (64 words) of data storage that can be used by your program as variables. We will discuss variables later.

Limitations
There are a number of limitations when working with computer numbers.

Negative Numbers
In order to support both positive and negative numbers, PBASIC needs to "steal" one of the bits from the number to represent the sign. Consequently, the range of all of our data types gets smaller:


 * Bits:Bits cannot be signed.
 * Nibble:Signed nibbles can go from -8 to 7
 * Byte:Signed bytes can go from -128 to 127
 * Word:Signed words can go from -32768 to 32767

Negative numbers are stored in a special way called two's complement. We will not cover this here.

Fractions
Some computers are capable of dealing with fractional numbers, called "floating-point numbers". However, BASIC Stamps cannot deal with fractions. This means that whenever we try to use a fraction, the BASIC Stamp will always round down. This means that the number 1.9 will become 1 inside the BASIC Stamp. Some people may find it strange that:


 * $$\frac{3}{2} = 1$$

However in computers, this is common.

Overflow
Datatypes have a fixed width, and they cannot hold numbers that are too big. If we try to store a number too big in a variable, the top-most bits will be "chopped off". If we are talking about bytes, we can write:


 * $$255 + 2 = 1$$

This is called overflow, and is something that we need to pay attention to when doing arithmetic. If a byte or a nibble is too small, we can always use words instead. But if we want to store a number that is greater then 65535, then we are in trouble!

Writing Numbers
There are several ways that a programmer may wish to specify a number: Binary, Decimal, or Hexadecimal. Hexadecimal, which we won't discuss in this book, is another useful number system using a base of 16.

To write a decimal number, we just write the number with no prefix, such as: "99". If we want to write a binary number, we use the "%" prefix, such as: "%110011". To write in hexadecimal, we would use the "$" prefix as so: "$AF99".

ASCII
In a computer, everything is stored in binary. This means that even things that aren't numbers, such as letters or other characters, are stored using binary numbers. There is a special code that assigns every character to a special binary pattern called ASCII. To write a letter in ASCII, we use the double-quotes such as:

MyVariable = "A"

Notice that a number and the ASCII code for that digit are not the same value:

MyVariable = 1

MyVariable = "1"

These two are not the same! However, ASCII has a special property that all the characters and digits are arranged from lowest to highest, and so we can do math with ASCII characters in some instances:

MyVariable = "A" + 1

Now MyVariable is equal to "B"! Also, we can convert digits into regular binary numbers by using a simple relation:

MyVariable = "1" - "0"

Now MyVariable is equal to the binary number 1. We will discuss variables more later, but these examples should be self-explanatory for now.