• Keine Ergebnisse gefunden

Long Bits

8.1. More Data Types

The data types seen up to this point are boolean, integer, and string. The data types introduced in this chapter are long integer, real, long real, bits, and long bits. The data type pointer, which is used to manipulate array and record data structures, is described in Chapter 11. The

discussion of the last two data types, address and charadr, is deferred until Chapter 18.

The procedures "read" and "write" may be used with any of the data types described in this chapter. They treat the other numeric and bits data types in a fashion analogous to the way they treat integers. Complete descriptions of "read" and "write" may be found in the "MAINSAIL Language Manual".

8.1.1. Long Integers

The data type long integer is designated by "LONG INTEGER". Long integer constants have the same form as numeric integer constants except that they are followed by the letter "L" (or lowercase "1"). The guaranteed range of long integers is -2,147,483,647 to +2,147,483,647, as opposed to the -32,767 to +32,767 range of the integer

dita

type.

The operations tI+", "_", tllIc", and "DIV" all operate on long integers as well as integers. Long integers and integers may not be mixed in the same arithmetic expression, however; e.g., "2L + 2" is illegal.

Like integers, long integers may be used in FOR-clauses. The iteration variable, lower limit, and upper limit must all be of the same data type, i.e., all integers or all long integers.

The module TETRA of Example 8.1.1-1 prompts for the value of N, then uses long integers to calculate the Nth tetrahedral number (the sum of the first N triangular numbers). Note that when "read" reads a long data type from a file or a string, it does not expect a trailing "L". For example, execution of TETRA looks as in Example 8.1.1-2.

BEGIN "tetra"

LONG INTEGER PROCEDURE triang (LONG INTEGER ii);

BEGIN

LONG INTEGER jj,sumi sum := OLi

FOR jj

:=

lL UPTO ii DO sum

:=

sum

+

jji RETURN (sum) ;

END;

INITIAL PROCEDURE;

BEGIN

LONG INTEGER ii,jj,sumi STRING s;

write(logFile,"Calculate what tetrahedral number? ");

read(cmdFile,s); read(s,ii); sum

:=

OL;

FOR jj :- lL UPTO i i DO sum :- sum + triang(jj);

write(logFile,"The ",ii,"th tetrahedral number is ",sum, eol) ;

END;

END "tetra"

Example 8.1.1-1. Long Integers to Calculate Tetrahedral Numbers

8.1.2. Reals and Long Reals

Reals and long reals represent floating point numbers. They use standard decimal notation, with a period (". If) separating the integer part from the fractional part (the presence of the period is what distinguishes a real or long real constant from an integer or long integer constant).

Long reals are distinguished from reals by the addition of an "L" at the end of the constant.

Adding an "E" followed by an integer N to a floating point number multiplies by ten-to-the-Nth power, e.g., "1.0E-6L" is the long real fraction one one-millionth.

"+", "_", and "*" are implemented for reals and long reals as for integers and long integers. The real and long real division symbol is "f', not "DIV".

11

;

*tetra<eo1>

Calculate what tetrahedral number? 3<eol>

The 3th tetrahedral number is 10

*tetra<eo1>

Calculate what tetrahedral number? 100<eol>

The 100th tetrahedral number is 171700

*tetra<eol>

Calculate what tetrahedral number? 200<eo1>

The 200th tetrahedral number is 1353400

*

Example 8.1.1-2. The Execution of TETRA

A complete set of trigonometric functions is provided for real numbers. See Example 8.1.2-1.

A more complete specification of the real and long real data types may be found in the

"MAINSAIL Language Manual".

8.1.3. Bits and Long Bits

The data types bits and long bits are used to represent "bit vectors" or small sets of "flags" or

"bits" that may be either "set" or "clear". Each flag resembles a boolean variable in that it may have only two values; the "set" or "1" value corresponds to the boolean "TRUE" and the "clear"

or "0" value to "F ALSEn. A bits or long bits value may also be thought of as a series of binary digits (an unsigned binary number). A bits value may have up to 16 binary digits; a long bits, up to 32 (these are the portably guaranteed ranges; some implementations may provide more binary digits).

A bits constant is written as a single quote mark ("''') followed by a radix letter, "B", "0", or

"H", followed by a binary, octal, or hexadecimal number. The octal forms represent three binary digits with each octal digit; the hexadecimal forms, four binary digits with each hexadecimal digit. The octal digits "0" through "7" represent the binary sequences "000"

through "111"; the hexadecimal digits "0" through "9", "A" through "F" represent the binary sequences "0000" through "1111". Leading zero digits may be omitted. The radix letter may be omitted for octal values. See Example 8.1.3-1.

Long bits constants have the same format as bits constants except that they are followed by the letter "L".

The bit positions in a bits or long bits may be numbered according to the powers of two they represent if the bits or long bits is considered to be an unsigned integer; see Figure 8.1.3-2.

BEGIN "sines"

write(logFile,"Real sin(",r,")

=

",sin(r),eol)i read(t,rr)i

write(logFile,"Long real sin(",rr,")

=

",sin(rr),eol)i ENDi

ENDi

END "sines"

Example 8.1.2-1. Calculation of Sines

The forms on each line represent the same value.

Example 8.1.3-1. Bits Constants

Hexadecimal

'HO

'H36 'HFFFF 'H1A2B

Descriptions of bits and long bits operations often refer to "corresponding bits" in two bits or

long bits values; corresponding bits are the bit positions with the same number in both values.

<-

LEFT RIGHT ->

(most significant in (least significant in

an unsigned integer) an unsigned integer)

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

1151141131121111101 91 81 71 61 51 41 31 21 11 01

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

For example, in the bits value '027, the bits numbered 0, 1, 2, and 4 are set. These bits may also be referred to as the '1, '2, '4, and '20

(or 'HI0) bits, respectively.

Figure 8.1.3-2. Bit Numbers in a Bits

Examples of the use of the bits and long bits data types are deferred until Section 8.5, where the bits and long bits operators are introduced.