45
Operators & Identifiers The Data Elements

Operators & Identifiers

  • Upload
    annick

  • View
    63

  • Download
    1

Embed Size (px)

DESCRIPTION

Operators & Identifiers. The Data Elements. exponentiation multiplication division ( real ) division ( integer quotient ) division ( integer remainder ) addition Subtraction assignment. ^ * / \ Mod + - =. Arithmetic Operators. Evaluate these expressions. - PowerPoint PPT Presentation

Citation preview

Page 1: Operators & Identifiers

Operators & Identifiers

The Data Elements

Page 2: Operators & Identifiers

Arithmetic Operators

• exponentiation• multiplication• division (real)• division (integer quotient)• division (integer remainder) • addition• Subtraction• assignment

^

*

/

\

Mod

+

-

=

Page 3: Operators & Identifiers

Evaluate these expressions

= 100 / 10 / 5

= 100 / 10 \ 5

= 100 \ 10 / 5

= 100 + 10 Mod 5

= 100 + 10 \ 5

Page 4: Operators & Identifiers

Evaluate these expressions

= 100 / 10 / 5 2

= 100 / 10 \ 5

= 100 \ 10 / 5

= 100 + 10 Mod 5

= 100 + 10 \ 5

Page 5: Operators & Identifiers

Evaluate these expressions

= 100 / 10 / 5 2

= 100 / 10 \ 5 2

= 100 \ 10 / 5

= 100 + 10 Mod 5

= 100 + 10 \ 5

Page 6: Operators & Identifiers

Evaluate these expressions

= 100 / 10 / 5 2

= 100 / 10 \ 5 2

= 100 \ 10 / 5 50

= 100 + 10 Mod 5

= 100 + 10 \ 5

Page 7: Operators & Identifiers

Evaluate these expressions

= 100 / 10 / 5 2

= 100 / 10 \ 5 2

= 100 \ 10 / 5 50

= 100 + 10 Mod 5 100

= 100 + 10 \ 5

Page 8: Operators & Identifiers

Evaluate these expressions

= 100 / 10 / 5 2

= 100 / 10 \ 5 2

= 100 \ 10 / 5 50

= 100 + 10 Mod 5 100

= 100 + 10 \ 5 102

Page 9: Operators & Identifiers

Variables

• A variable is a string used to identify a memory location.

• The amount of memory is determined by the type of data that it will store.

• Typically, variable names need to be declared so the operating system can allocate sufficient space.

• Then values of the specified type can be assigned, i.e. stored in that location.

Page 10: Operators & Identifiers

Variable Names in VB

• Must begin with a letter– Can include letters and numerals– Cannot include spaces

• Use names that are descriptive

• Capitalising convention– InterestRate, InitialCapital

Page 11: Operators & Identifiers

Variables

• Local– Declared within a subprogram– Dim varName As dataType

• Global– Declared at the top of the code listing– Private varName As dataType

Page 12: Operators & Identifiers

Data Types

Type Name Storage size Range of values

Boolean 2 byte True or False (1 or 0)

Byte 1 byte integers in the range 0 to 255

Char 2 bytes 0 to 65,535 representing the Unicode character set

Date 8 bytestime/date between 0:00:00 on 1/1/0001 to 23:59:59 on

12/31/9999

Double 8 bytesa real number; precision is about 14 digits to the right of the

decimal point

Integer 4 bytes integers in the range -2,147,483,648 to 2,147,483,647

Long 8 bytes very big!

Short 2 bytes integers in the range -32,768 to 32,767

Single 4 bytesa real number, i.e. a number with a decimal (fractional) part;

precision is limited to about 6 digits to the right of the decimal point.

String depends on

platformup to approximately 2 billion Unicode characters

Page 13: Operators & Identifiers

The ASCII Character Set

Page 14: Operators & Identifiers

Data Storage (Short type)

27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

1 1 1 1 1 1 1 1

128 +64 +32 +16 +8 +4 + 2 + 1

=255

Page 15: Operators & Identifiers

Data Storage (2nd byte)

215 214 213 212 211 210 29 28

32768 16384 8192 4096 2048 1024 512 256 1 1 1 1 1 1 1 132768+16384 +8192 +4096 +2048 +1024 +512 +256

=65280 +255 65535

The largest Unsigned value that can be stored in 16 bits.

How many patterns are there?

Page 16: Operators & Identifiers

Integer Storage

• To store integers, half the combinations are used to represent negative values.

• The range for Integer type variables is:-32,768 to +32767

• The MSB is used to represent the sign.

• Which value of the sign bit (0 or 1) will represent a negative number?

Page 17: Operators & Identifiers

2’s Complement Notation(examples in 8 bits to save space)

• The notation system that uses 1 to represent negative values.

• Fixed length notation system.• Zero is the first non-negative value:

00000000

• The pattern immediately before zero is -111111111

• The largest value is stored as 01111111• The smallest value is stored as 10000000

Page 18: Operators & Identifiers

Arithmetic in 2’s Complement(remember it’s a fixed length system)

00 + 00 = 00

00 + 01 = 01

01 + 00 = 01

01 + 01 = 10

-1 in 2’s complement 11111111

+ 1 in 2’s complement + 00000001

0 discard the carry bit 1 00000000

Page 19: Operators & Identifiers

Integer Storage (4 bytes)

• High order bit (MSB) is worth 231

• The number of different combinations= 232

=4,294,967,296

• Half are used for negative values, so the range is– 2,147,483,648 to + 2,147,483,647

Page 20: Operators & Identifiers

Long Integers

• In 8 bytes there are 64 bits!

• High order bit (MSB) is worth 263.

• The number of different combinations

=264

=18,446,744,073,709,650,616

Page 21: Operators & Identifiers

Fractions

• A radix separates the integer part from the fraction part of a number.

101.101• Columns to the right of the radix have

negative powers of 2.

Page 22: Operators & Identifiers

Fractions

22 21 20 . 2-1 2-2 2-3

Page 23: Operators & Identifiers

Fractions

22 21 20 . 2-1 2-2 2-3

4 2 1 . ½ ¼ ⅛

Page 24: Operators & Identifiers

Fractions

22 21 20 . 2-1 2-2 2-3

4 2 1 . ½ ¼ ⅛

1 0 1 . 1 0 1

Page 25: Operators & Identifiers

Fractions

22 21 20 . 2-1 2-2 2-3

4 2 1 . ½ ¼ ⅛

1 0 1 . 1 0 1

4 + 1 + ½ + ⅛

Page 26: Operators & Identifiers

Fractions

22 21 20 . 2-1 2-2 2-3

4 2 1 . ½ ¼ ⅛

1 0 1 . 1 0 1

4 + 1 + ½ + ⅛

5⅝

Page 27: Operators & Identifiers

Scientific Notation

Very large and very small numbers are often represented such that their order of magnitude can be compared.

The basic concept is an exponential notation using powers of 10.

a × 10b

Where b is an integer, and a is a real number such that:

1 ≤ |a| < 10

Page 28: Operators & Identifiers

Scientific Notation

An electron's mass is about0.00000000000000000000000000000091093826 kg.

In scientific notation, this is written9.1093826×10−31 kg.

The Earth's mass is about5,973,600,000,000,000,000,000,000 kg.

In scientific notation, this is written5.9736×1024 kg.

Page 29: Operators & Identifiers

E Notation

To allow values like this to be expressed on calculators and early terminals

× 10b

was replaced by Eb

So 9.1093826×10−31

becomes 9.1093826E−31And 5.9736×1024

becomes 5.9736E+24

Page 30: Operators & Identifiers

E Notation

The ‘a’ part of the number is called the mantissa or significand.

The ‘Eb’ part is called the exponent.

Since these numbers could also be negative they would typically have a sign as well.

Page 31: Operators & Identifiers

Floating Point Storage

In floating point notation the bit pattern is divided into 3 components:

Sign – 1 bit (0 for +, 1 for -)

Exponent – stored in Excess notation

Mantissa – must begin with 1

Page 32: Operators & Identifiers

Excess Notation (examples are in 8 bits to save space)

• The notation system that uses 0 to represent negative values.

• Fixed length notation system.• Zero is the first non-negative value:

– 10000000

• The pattern immediately before zero is -1– 01111111

• The largest value is stored as 11111111• The smallest value is stored as 00000000

Page 33: Operators & Identifiers

Mantissa

• Assumes a radix point immediately left of the first digit.

• The exponent will determine how far and in which direction to move the radix.

Page 34: Operators & Identifiers

An example in 8 bits

If the following pattern stores a floating point value, what is it?

01101001

Page 35: Operators & Identifiers

An example in 8 bits

If the following pattern stores a floating point value, what is it?

01101001Separate it into its components:

Page 36: Operators & Identifiers

An example in 8 bits

If the following pattern stores a floating point value, what is it?

01101001Separate it into its components:

signexponentmantissa

Page 37: Operators & Identifiers

An example in 8 bits

If the following pattern stores a floating point value, what is it?

0 110 1001Separate it into its components:

signexponentmantissa

Page 38: Operators & Identifiers

An example in 8 bits

0 110 1001A sign bit of 0 means the number is…?

Page 39: Operators & Identifiers

An example in 8 bits

0 110 1001A sign bit of 0 means the number is positive.

110 in Excess Notation converts to …?

Page 40: Operators & Identifiers

An example in 8 bits

0 110 1001A sign bit of 0 means the number is positive.

110 in Excess Notation converts to +2.

Place the radix in the mantissa …

Page 41: Operators & Identifiers

An example in 8 bits

0 110 1001A sign bit of 0 means the number is positive.

110 in Excess Notation converts to +2.

Place the radix in the mantissa .1001

Put it all together …

Page 42: Operators & Identifiers

An example in 8 bits

0 110 1001A sign bit of 0 means the number is positive.110 in Excess Notation converts to +2.Place the radix in the mantissa .1001

Put it all together …

+ .1001 * 22

Page 43: Operators & Identifiers

An example in 8 bits

+ .1001 * 22

Multiplying a binary number by 2 shifts the bits left (move the radix to the right) one position.

So the exponent tells us to shift the radix 2 positions right.

+ 10.01

= 2¼

Page 44: Operators & Identifiers

Normal Form

• The first bit of the mantissa must be 1 to prevent multiple representations of the same value.

0 100 1000 0 .1000 .1000

0 101 0100 1 .0100 .1000

0 110 0010 2 .0010 .1000

0 111 0001 3 .0001 .1000

Page 45: Operators & Identifiers

Data Type ConversionFunction Return Type Argument

CBool boolean any valid String or numeric expression

CCur currency a string or numeric expression within the range of currency values

CDbl double real number a string or numeric expression within the range of double real values

CInt integer a string or numeric expression within the range of integer values, fractions are rounded

CLng long integer a string or numeric expression within the range of long integer values, fractions are rounded

CSng single real number a string or numeric expression within the range of single real values

CStr string any valid value or expression

CDate date any value that can be interpreted as a date