PBASIC Programming/Data Types
Binary Numbers
[edit | edit source]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:
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:
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:
Common Data Types
[edit | edit source]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
[edit | edit source]A bit is a single binary number, either a 0 or a 1.
Nibble
[edit | edit source]A nibble is 4 bits, and can contain values from 0 to 15.
Byte
[edit | edit source]A byte is 8 bits (2 nibbles) and can contain values from 0 to 255.
Word
[edit | edit source]A word is 16 bits long (2 bytes or 4 nibbles) and can contain values from 0 to 65535.
Available Storage
[edit | edit source]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
[edit | edit source]There are a number of limitations when working with computer numbers.
Negative Numbers
[edit | edit source]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
[edit | edit source]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:
However in computers, this is common.
Overflow
[edit | edit source]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:
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 than 65535, then we are in trouble!
Writing Numbers
[edit | edit source]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
[edit | edit source]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.