Basilar Data Types

The first thing that you need to know is the group of the basilar data types. The computer's CPU can handle by default only four types of data (BYTE, WORD, DOUBLE WORD), but all the programing languages have created other custom data-types. The most important are : SINGLE AND DOUBLE FLOATING POINT, INTEGERS, LONG INTEGER and STRING. These types are supported by Qbasic and QuickBasic, and the PDS 7.1 supports the CURRENCY type too.

Now, let examine all of these data types.

BIT

|0|
The bit is the simplest type of data that exists. It can be either a 1 or a zero. A sequence of bit can make all the other data types known on the computer world. For example a sequence of 8 bits generates a byte. All the computers uses a BASE-2 (or binary) number system. For example, 0001 = 1 , 0010 = 2, 0011 = 3 , 1000 = 15.

BYTE - 8 BIT
|0000 0000|

The BYTE is the standard chunk of information. The BYTE is 8 BITs, and it's maximum value is FF (HEXADECIMAL) or 255. Also, the equivalent of 255 is 11111111. Any memory reference is indicated using bytes. For example, is you query how many memory has your computer, the answer is xxxx bytes. Usually the BYTE word is preceded by a "kilo" or "mega" prefix. A kilobytes is 1024 bytes and a megabyte (MB) is 1024 kilobytes (KB), or 1048576 bytes.

WORD - 2 BYTES - 16 BIT
|0000 0000 0000 0000|

A Word is just two bytes together, or 16 bits. The maximum value of a word is 65535 (FFFF HEXADECIMAL), and , with the byte, it's the most used data type. The equivalent of 65535 is 16 '1' together. The word type is the same of the "unsigned integer", but see the Integer data type for more information about signed and unsigned integers.

DOUBLE WORD -4 BYTES - 32 BIT
|0000 0000 0000 0000 0000 0000 0000 0000|

A double word is two words, or 32 bit. The maximum value of a DWord is 4294967295 of FFFFFFFF HEX. The DWord is the same of the 'Unsingned Long integer' or of the 'Signed Long Integer'.

A small consideration about Singed and Unsigned integers (WORDs): an unsigned word have a range from 0 to 65535, and all the 16 bits are used for store the value. Also, 65535 is

1111 1111 1111 1111

But, how store negative numbers? The solution can be only one: use a bit for the sign. So, a signed word have a range from -32768 to 32767. Here's an example:
 
Decimal
Hexadecimal
Binary
32765
7FFD
0111 1111 1111 1101
32766
7FFE
0111 1111 1111 1110
32767
7FFF
0111 1111 1111 1111
-32768
8000
1000 0000 0000 0000
32767
8001
1000 0000 0000 0001
-1
FFFF
1111 1111 1111 1111
0
0000
0000 0000 0000 0000

If the last bit (note that the first bit is on the right) is 0 the value is positive, if the last bit is 1 is negative. This isn't too important in Basic, but every programmer must known this topic, for general knowledge.

Now let see the QuickBasic standard data types. These types are derived from the basilar types (see above), usually used with Assembler. Note that QuickBasic doesn't supports the BYTE type and the unsigned words.

INTEGER. (%)

Is the same thing of a signed word, with a minimum value of -32768 and a maximum value of +32767. Is the most used type of QB.

LONG INTEGER (&)

Is the same thing of a signed double word, with a range from 2,147,483,647 to -2,147,483,648. This type is usually used for scientific and math programs with big numbers.

SINGLE FLOATING POINT (!)

The storage method for this type is totally different than the one of integers. The data is split in three portion: the mantissa, the exponent and the sign bit. The mantissa holds the base value of the number, the exponents holds the power that the mantissa must be raised, and the sign bit, like integers, is used to show if the number is positive or negative.

There's two methods to indicate Floating Point numbers, the MBF (Microsoft Binary Format) or the IEEE format. The IEEE format is the slower but is more accurate. QuickBasic 4.x supports the IEEE format and the PDS too. The Single floating point is a floating point number, with a range from ±3.402823 E+38 (10 ^ 38) to ±1.401298 E-45, and requires 32 bits (4 bytes) like a Long Integer

DOUBLE FLOATING POINT (#)

The Double floating point is a single floating point but more accurate. It requires 64 bits (8 bytes) , and can store number from ±1.7976931 D+308 to ±4.940656 D-324.

A problem for the floating point number is the rounding error. Try this program

For X% = 1 to 10000     'repeats 10000 times
Number! = Number! + 1.1 'add to Number 1.1 every time
Next ;repeat
Print Number!           'show the Number!

Expected result: 11000.00

Returned result: 10999.52

STRINGS.

A string is an alpha numerical sequence of one or more Ascii characters( space included). An example of string can be

AVXSFD0113245&"!·Ì¦ !!!

In Basic strings are delimited from two ". For example, to assign to a variable a text, use this command:

X$ = "ABCDE"

There's are two types of strings in Basic; fixed and variable length strings. A fixed length string is a string that have a constant length, declared with the DIM statement. For example, this program demostrates how a fixed length string is handled by Basic.

CLS
DIM A AS STRING * 3                          'Fixed length string of 3 chars
A ="ABC"                                     'Assign a text of three characters
PRINT A                                      'Prints the string
A = "ABCDEF"                                 'Assign a text with a length major of 3 chars
PRINT A                                      'Prints the string, note the output!
A = "ABC!£$%&/()=?^^?=)(/&%$£!|" 'Another time
PRINT A

Output :

ABC
ABC
ABC

When a fixed length string is used with an assignment instruction (=) only the declared characters are considered. On this example only the first three characters are used.

VARIABLE LENGTH STRINGS.

The size of variable length string can change every time that an assignment instruction is done. Try this example:

CLS
DIM A AS STRING
A = "ABC"
PRINT A
A = "ABCDEF"
PRINT A
A = "ABC!£$%&/()=?^^?=)(/&%$£!|"
PRINT A

The maximum length of a string (fixed or variable length) is 32767, and each character need a byte of memory (in variable length strings four byte are needed for the string descriptor). Also, you can use empty strings too. A string can be easily converted to a numerical variable and from a numerical variable. See the STR$ and the VAL command for future reference.

The Currency type

With the exit of Microsoft PDS, a new type was introduced, the Currency type (scaled integer). The range of this type is -922337203685477.5808 to 922337203685477.5807 , with 15 digits on the left of the sign and four on the right. This number can be used for big integer operations or for very precise floating point operation, simply by scaling per 10000. Also, the currency is called "fixed point" too.

Summary

After reading this text, you should know the common used data types. If you want to check your knowledge, read and answer this text:

  1. What's a BIT?
  2. How many BIT have a word?
  3. What the maximum range of an unsigned Double Word?
  4. How are called the three parts of a floating point number?
  5. What the difference between WORDs and INTEGERs?
Answers:
  1. The BIT is the simplest data type. It can be either 1 or 0
  2. 16
  3. 4.294.967.295
  4. Sign, Mantissa and Exponent
  5. A word is usually unsigned, a Basic Integer is always signed. Also, except this detail, they are the same thing.


Also, you learned these things:
BIT, WORD AND DOUBLE WORD: Basilar datas handled directly by the computer. None of these types is used in basic.
INTERGER, LONG INTEGER: The Basic's types equivalent of word and Dword. They are always signed. SINGLE AND DOUBLE FLOATING POINT: Basic floating point types. These types are handled by Basic, with the IEEE format. STRINGS: Data type that can store strings of characters and numbers. Unknown for the CPU they are totally handled by Basic. A string can be 'fixed length' or 'variable length'
 



This article originally appeared in The BASIX Fanzine Issue 16 from September 1999.