Boolean Variables
|
The Boolean data type uses 8 bits to store a variable whose value would
be set as true (1) or false (0). To declare such a value, you use the
Boolean keyword. Here is an example
var IsMarried: Boolean; |
Short Integers
|
An algebraic natural number is also called an integer. A byte can be
used to store a natural number provided it uses a small value. The data
type used to declare such a variable is Shortint. The value stored in a
Shortint variable must range from –128 to 127. The variable can be
declared as follows:
var BookLength: Shortint;
If the values of the variable must be unsigned, you can declare it
using the Byte keyword. In this case, the values of the variable must
range between 0 and 255. Here is an example of declaring such a
variable:
var MemberAge: Byte; |
A Word
|
Representing a Word
|
A Word is a group of 16 consecutive bits. The bits are counted from right to left starting at 0:
|
Considered as a group of 16 bits, the most right bit of a word, bit 0,
is called the least significant bit, or Low Order bit, or LO bit, or
LOBIT. The most left bit, bit 15, is called the most significant bit, or
High Order bit, or HI bit, or HIBIT. The other bits are referred to
using their positions: bit 1, bit 2, bit 3, etc.
Considering that a word is made of two bytes, the group of the right 8 bits is called the least significant byte, or Low Order byte, or LO byte, or LOBYTE. The other group is called the most significant byte, or High Order byte, or HI byte, or HIBYTE. The most fundamental representation of a word in binary format is 0000000000000000. To make it easier to read, you can group bits in 4, like this: 0000 0000 0000 0000. Therefore, the minimum binary value represented by a word is 0000 0000 0000 0000. The maximum binary value represented by a word is 1111 1111 1111 1111. The minimum decimal value of a word is 0. To find out the maximum decimal value of a word, you can use the base 2 formula, filling out each bit with 1: 1*215 + 1*214 + 1*213 + 1*212 + 1*211 + 1*210 + 1*29 + 1*28 + 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20 = 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 65535 The minimum hexadecimal value you can store in a word is 0x0000000000000000. This is also represented as 0x00000000, or 0x0000, or 0x0. All these numbers produce the same value, which is 0x0. To find out the maximum hexadecimal number you can store in a word, replace every group of 4 bits with an f or F: 1111 1111 1111 1111 f f f f = 0xffff = 0xFFFF = 0Xffff = 0XFFFF |
Wide Characters
|
Object Pascal provides a data type to represent Unicode or international
characters. These characters are represented using 16-bits. The data
type to do this is called
WideChar.
|
Small Integers
|
A word, which is a group of 16 contiguous bits or 2 bytes is used to
represent a natural number that uses twice the space of a short integer.
As we have studied, the maximum numeric value that can fit in a word is
65535. Based on this, a word can store a variable declared with the
Smallint keyword. Here is an example:
var WholeDistance: Smallint;
A variable declared as Smallint can store a number between
–32768 and 32767. If the number must be unsigned, you can declare it
using the Word data type. In this case, the number must range from 0 to
65535.
|
A Double-Word
|
Representing a Double-Word
|
A double-word is a group of two consecutive Words. This means that a
double-word combines 4 bytes or 32 bits. The bits, counted from right to
left, start at 0 and end at 31.
|
The most right bit, bit 0, is called the Low Order bit or LO bit or
LOBIT. The most left bit, bit 31, is called the High Order bit or HI bit
or
HIBIT. The other bits are called using their positions.
The group of the first 8 bits (from bit 0 to bit 7), which is the right byte, is called the Low Order Byte, or LO Byte. It is sometimes referred to as LOBYTE. The group of the last 8 bits (from bit 24 to bit 31), which is the left byte, is called the High Order Byte, or HI Byte or HIBYTE. The other bytes are called by their positions. The group of the right 16 bits, or the right Word, is called the Low Order Word, or LO Word, or LOWORD. The group of the left 16 bits, or the left Word, is called the High Order Word, or HI Word, or HIWORD. |
The minimum binary number you can represent with a double-word is 0x
The minimum decimal value of a double-word is 0. To find out the maximum decimal value of a word, you can use the base 2 formula giving a 1 value to each bit: 2n-1 230 229 228 227 226 225 224 etc 1,073,741,824 536,870,912 268,435,456 134,217,728 67,108,864 33,554,432 16,777,216 223 222 221 220 219 218 217 216 8,388,608 4,194,304 2,097,152 1,048,576 524,288 262,144 131,072 65,536 215 214 213 212 211 210 29 28 32,768 16,384 8,192 4,096 2,048 1,024 512 256 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 1*231+1*230+1*229 + 1*228 + 1*227 + 1*226 + 1*225 + 1*224 + 1*223 + 1*222 + 1*221 + 1*220 + 1*219 + 1*218 + 1*217 + 1*216 + 1*215 + 1*214 + 1*213 + 1*212 + 1*211 + 1*210 + 1*29 + 1*28 + 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20 = 2,147,483,648 + 1,073,741,824 + 536,870,912 + 268,435,456 + 134,217,728 + 67,108,864 + 33,554,432 + 16,777,216 + 8,388,608 + 4,194,304 + 2,097,152 + 1,048,576 + 524,288 + 262,144 + 131,072 + 65,536 + 32,768 + 16,384 + 8,192 + 4,096 + 2,048 + 1,024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 4,286,578,708 The minimum hexadecimal value you can store in a word is 0x0. To find out the maximum hexadecimal number you can represent with a word, replace every group of 4-bits with an f or F: 1111 1111 1111 1111 1111 1111 1111 1111 F F F F F F F F = 0xffffffff = 0XFFFFFFFF = 0xFFFFFFFF |
Integers
|
A double-word provides twice the amount of space of a word. This is
equivalent to 32 bits or 4 bytes or 4294967295. Therefore, a double-word
is used for large numbers that would not fit in a word. If your program
needs an integer variable whose value requires more memory space than a
word can hold, declare it using the Integer keyword. The integer data
type is used for a variable whose value can range from –2,1474,83,648 to
2,147,484,647.
Here is an example: var AgeRange: Integer;
Alternatively, you can use the Longint
keyword to declare a variable whose value must be able to fit in a
32-bit space. If the variable must be positive, you can declared it
using the Cardinal keyword. A Cardinal variable is used for a 32-bit
positive integer whose value would range from 0 to 2,147,484,647. Here
is an example:
var ColonyPopulation: Cardinal;
Besides Cardinal, to declare an unsigned natural variable, you can use the
Longword keyword.
If you want to use a natural number whose value is very large, you can use the Int64 keyword to declare it. |
The other integer data types available in object Pascal are:
|
Floating-Point Numbers
|
Floating-Point Variables
|
The integers we have used so far have the main limitation of not
allowing decimal places in their values. Object Pascal provides floating
data types that would solve this problem. The most fundamental floating
variable is declared with the Single keyword. A Single variable
occupies 4 bytes of space and therefore can store a number that ranges
from 1.5 x 10–45 and 3.4 x 10+38. To declare such a floating-point
variable, use the Single keyword followed by the name of the variable.
Here are examples:
var Age: Single;
When a variable is larger than the Single data
type can handle and requires more precision, you should declare it
using the Double keyword. The double-precision type is an 8 Byte decimal
or fractional number ranging from 5.0 x 10–324 and 1.7 x 10308.
For an even larger variable, use the 10 bytes real data type call Extended. An Extended variable can store a number that ranges from 3.4 x 10-4932 to 1.1 x 104932. Object Pascal also provides the Real48 data type used to declare a flowing pointer variable. It can store a value between 2.9 x 10–39 and 1.7 x 1038. Alternatively, to declare a variable that represents a monetary value, you can use the Currency data type. It can be used to a store a number between –922337203685477.5808 and 922337203685477.5807. Previous versions of Object Pascal were also using the Comp data type that would be used to store numbers that range between –263+1 and 263 –1. |
Strings Fundamentals
|
A string is an empty spave or a group of
characters that form an entity or common value. To declare a string as a
variable, you can use the string data type. Here is an example:
var FirstName: string;
Object Pascal also provides the AnsiString data type to declare a string variable. Its variable can be declared as follows:
var FirstName: AnsiString;
In most cases, when you declare a variable as string, Object Pascal implemented by Borland Delphi considers the variable as
AnsiString. Such a variable can store characters or text to fit in 4 bytes to 2GB of space.
No comments:
Post a Comment