47
Variables and Expressions http://independencescience.com/blog/tvi-tips-4-strategies-to-prepare-a-student-with-visual impairments-for-success-in-science-and-math-courses/

Variables and Expressions science-and-math-courses

Embed Size (px)

Citation preview

Page 1: Variables and Expressions  science-and-math-courses

Variables and Expressions

http://independencescience.com/blog/tvi-tips-4-strategies-to-prepare-a-student-with-visual-impairments-for-success-in-science-and-math-courses/

Page 2: Variables and Expressions  science-and-math-courses

Outline

I. Base2 & Base16 and its connection to CPU/RAM

II. Variables / Types in C/C++ (we'll see more later)

III. Statements & Expressions in C/C++

IV. Executable file format and the role of the OS

Page 3: Variables and Expressions  science-and-math-courses

RAM

● Recall: Storage for numbers currently being manipulated– Images– Text– Programs– …

● Volatile● Made of billions of transistors or capacitors

http://en.wikipedia.org/wiki/File:Siliconchip_by_shapeshifter.png

Page 4: Variables and Expressions  science-and-math-courses

Part I

● Bits, Bytes and Nybbles● Base2 (binary) and Base16 (hexadecimal) number

systems

http://scottkantner.com/bits-bytes-and-bandwidth/

Page 5: Variables and Expressions  science-and-math-courses

Bits

● A bit is– One transistor / capacitor (in a larger Integrated Circuit)– Either on or off

● Kind of like a light-bulb

● As programmers we– don't worry about the physical structure of a bit– Think of it as being true (on, 1) or false (off, 0)

http://www.nerditorial.com/newsandopinion/it%E2%80%99s-a-blinking-conspiracy.html

Page 6: Variables and Expressions  science-and-math-courses

Groups of bits

● With one bit, we can store one of two values– 0 = off– 1 = on

● However, with a group of bits we– Can have multiple combinations of on / off's– Can assign an (arbitrary) value to each combination

Page 7: Variables and Expressions  science-and-math-courses

Two bits

● With two bits, we have...

4 possible combinations

Combination# (Arbitrary)

Bit1 Bit2

0 0 0

1 0 1

2 1 0

3 1 1

Page 8: Variables and Expressions  science-and-math-courses

Three bits

● With three bits we have...

8 possible combinations

Combination# (Arbitrary)

Bit1 Bit2 Bit3

0 0 0 0

1 0 0 1

2 0 1 0

3 0 1 1

4 1 0 0

5 1 0 1

6 1 1 0

7 1 1 1

Page 9: Variables and Expressions  science-and-math-courses

Four bits● With four bits, we have

16 possible combinationsCombination# (Arbitrary) Bit1 Bit2 Bit3 Bit4

0 0 0 0 0

1 0 0 0 1

2 0 0 1 0

3 0 0 1 1

4 0 1 0 0

5 0 1 0 1

6 0 1 1 0

7 0 1 1 1

8 1 0 0 0

9 1 0 0 1

10 1 0 1 0

11 1 0 1 1

12 1 1 0 0

13 1 1 0 1

14 1 1 1 0

15 1 1 1 1

Page 10: Variables and Expressions  science-and-math-courses

See a pattern?

● If we have n bits, we have

2n

possible combinations● We can think of this combination as being a

number in the base2 (binary) number system

Page 11: Variables and Expressions  science-and-math-courses

Comparison to base-10 (decimal)● Base-10

– Digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9– Place-values:

● Right-most digit (1' place. 10^0)● Next right-most digit (10's place. 10^1)● Next right-most digit (100's place. 10^2)● …

– E.g. 3472 is the same as 3*1000 + 4*100 + 7*10 + 2*1● Base-2

– Digits are 0 and 1– Place-values:

● Right-most digit (1's place. 2^0)● Next right-most digit (2's place. 2^1)● Next right-most digit (4's place. 2^2)● …

– E.g. 10110 is the same as 1*16 + 0*8 + 1 * 4 + 1 * 2 + 0 * 1 ● Which is 22 (in base 10)

Page 12: Variables and Expressions  science-and-math-courses

Base10 => Base2 conversion

● Method I:

1. Come up with the (only) sum of powers-of-two that equal the original number

2. Create a binary string with ● 1's in the place-values you used in step1.● 0's everywhere else● As many 0's in the higher-place values that you want

– E.g. 00000101 is the same values as 101– Just like 0342 is the same as 342 in base-10

– Example: 4833

Page 13: Variables and Expressions  science-and-math-courses

Base10 => Base2 conversion, cont.

● Method II (An algorithm)

def decimal2binary(base10Num): n = base10Num s = "" while n > 0: if n is odd: s = "1" + s if n is even: s = "0" + s n = n / 2 (ignore remainder) return s

def decimal2binary(base10Num): n = base10Num s = "" while n > 0: if n is odd: s = "1" + s if n is even: s = "0" + s n = n / 2 (ignore remainder) return s

Page 14: Variables and Expressions  science-and-math-courses

Base16

● Digits are– 0, 1, 2, 3, 4, 5, 6, 7, 8, 9– Problem: We need some symbols for the other 6!

● Solution: use the letter symbols A, B, C, D, E, F– DOESN'T represent text!

● For programmers, used as a shorthand for (long) binary strings

Page 15: Variables and Expressions  science-and-math-courses

Base16, cont.Binary Digit Base-10

equivalentBase16

0000 0 0

0001 1 1

0010 2 2

0011 3 3

0100 4 4

0101 5 5

0110 6 6

0111 7 7

1000 8 8

1001 9 9

1010 10 A

1011 11 B

1100 12 C

1101 13 D

1110 14 E

1111 15 F

Page 16: Variables and Expressions  science-and-math-courses

Base16, cont.

● 1 base16 (hexadecimal) digit can store the same range of values as 4 binary digits.

● An easy way to convert from base2 => base16● Example:

1101 0001 1010 0111 1000 (binary)

D 1 C 7 8 (hexadecimal)● Hexadecimal values are often written as 0xD1C78

– To make it clear we have a hex number– E.g. is 754 decimal or hexadecimal?

● Don't know!● 754 hexadecimal is 1876 decimal● 0x754 is definitely hexadecimal

Page 17: Variables and Expressions  science-and-math-courses

Back to computer architecture...

● The fundamental storage unit is a byte– 8 bits

● 256 different combinations

● A kilobyte is 1024 (2^10) bytes● A megabyte is 1024 kilobytes (2^20 bytes)● A gigabyte is 1024 megabytes (2^30 bytes)● Each byte has an address

– Addresses start at 0...– ...Go up to #bytes-1

● So...on a 4GB machine, we'd have address from 0-4,294,967,295

Page 18: Variables and Expressions  science-and-math-courses

Part II

● Variables in C/C++– int

● Signed / Unsigned

– float (IEEE 754)● A few new printf / cout

features

http://www.wisegeek.org/what-is-a-card-catalog.htm

Page 19: Variables and Expressions  science-and-math-courses

Variables

● In our program, we'll nearly-always want to allocate some memory to store things that our program is working with. This is a variable.

● To allocate a single byte we do something like:int main(){char c; // Can store values -128 to 127unsigned char uc; // Can store values 0 to 255

return 0;}

int main(){char c; // Can store values -128 to 127unsigned char uc; // Can store values 0 to 255

return 0;}

● This allocates 1 byte of storage in RAM.● A variable must be declared before using

(unlike Python / MathPiper)

Page 20: Variables and Expressions  science-and-math-courses

Modifying / using variables

● We can change the value stored by the variable a number of ways:

int main(){char c = 75; // Assigning with declarationunsigned char uc;

c = -15; // Modifying valueuc = c; // Copying value of c

// (sets uc to 241 – why?)

c = 'a'; // Sets c to 97 – why?// Hint: 'a' ISN'T a

string

return 0;}

int main(){char c = 75; // Assigning with declarationunsigned char uc;

c = -15; // Modifying valueuc = c; // Copying value of c

// (sets uc to 241 – why?)

c = 'a'; // Sets c to 97 – why?// Hint: 'a' ISN'T a

string

return 0;}

Page 21: Variables and Expressions  science-and-math-courses

Negative numbers

● A char's bits can be in one of 256 different states

0-255: Just interpret the binary bits as a base-2 number

-128-127: How is this represented?● Answer: Two's Complement (or one's

complement)

Page 22: Variables and Expressions  science-and-math-courses

Two's complement

● Take the base-10 number 73

1001001 in base-2 ● In two's complement, to represent -73:

– Invert the bits– Add one (in binary) – make sure you do the carrying

0100 1001 +73 in base10

1011 0110 Inverted bits

1011 0111 After adding the one

Page 23: Variables and Expressions  science-and-math-courses

Two's complement, cont.

● Q: Why do this?● A1: The highest-order bit will be a 1 if negative● A2: The computer can use the same “ADD”

circuitry (in the ALU part of the CPU) to add signed and unsigned numbers.

● A3: In other schemes (sign-bit, 1's complement), there are 2 definitions of 0 (-0 and +0).

Page 24: Variables and Expressions  science-and-math-courses

Two's complement, cont.

(Unsigned) Look at 15 + 49 (which is 64) in binary

0000 1111 (15)

0011 0001 + (49)

0100 0000 (64)

(Signed) Look at -74 + 29 (which is -45)

1011 0110 (-74 in 2's complement)

0001 1101 + (+29)

1101 0011 The high-order bit is 1 so we know it's

negative. To get the actual value we 1) Subtract one (borrowing if necessary) then 2)invert the bits

Page 25: Variables and Expressions  science-and-math-courses

“Undoing” Two's complement

● To get the negated number,– Add a bit string of all 1's (you'll lose the high values

when carrying)● Note: this is -1 in two's complement

– Invert the result

1101 00111111 1111 + Add this1101 0010 Then invert...0010 1101 (Same as before)Thanks to Jeremy McCleese for this one

Page 26: Variables and Expressions  science-and-math-courses

Integers

● A char is capable of storing an integer– But only in the range 0-255 (or -128-127 with two's

complement)● To store a wider range of values, we need more bits

– Or...group multiple bytes togetherC/C++ type #bytes Range of values

short (or short int) 2 -215 to +(215 - 1)

unsigned short (or unsigned short int) 2 0 to +(216 - 1)

int 4 -231 to +(231 - 1)

unsigned int 4 0 to +(232 - 1)

long (or long int) varies (usually the same as int)

unsigned long (or unsigned long int) varies (usually the same as unsigned int)

Page 27: Variables and Expressions  science-and-math-courses

IEEE 754 (float's)

● Floats are used to store floating-point numbers– i.e. those with a decimal part– Can only approximate:

● Many rational numbers– 1/3 and 1/10 are approximated (as well as any prime denominator besides 2)– ½ can be represented exactly (and ¼, 1/8, etc)

● Irrational numbers– e (2.719...)– π (3.14159...)

● Significantly more complex than integers– Big range of values– Works well with small numbers (0.00002143)– And big numbers (1.3415x10^34)

Page 28: Variables and Expressions  science-and-math-courses

Float's, cont.

● A float (in IEE754 and most C/C++ compilerss) is 4 bytes

Higest-order bit: sign (0=positive, 1=negative)

Next 8 bits: exponent

Last 23 bits: mantissa (always in the range 1.0 – 2.0)● A float constant is represented as:

sign * 2exponent * mantissa● Ex. 0.40625 is stored as

+1 * 2-2 * 1.625● A good reference:

http://www.h-schmidt.net/FloatConverter/IEEE754.html

Page 29: Variables and Expressions  science-and-math-courses

Floating Point's in C/C++

● Another type: double and long double– doubles are generally 8 bytes (more precision, memory)– long doubles are often also 8 bytes (on my C::B it is

12??)– Try this:

void main(){int numBytes;float f;

numBytes = sizeof(float);numBytes = sizeof(long double)numBytes = sizeof(f);

}

void main(){int numBytes;float f;

numBytes = sizeof(float);numBytes = sizeof(long double)numBytes = sizeof(f);

}

Page 30: Variables and Expressions  science-and-math-courses

Floating Points in C/C++

● Another code snippet...int main(){int i = 9;float f;double d;const float rate = 0.15f;

f = 3.14f; // Identifies 3.14 as a float constant // (vs. a double constant)

f = i; // Implicit type-cast from int to floatf = (float)i; // Explicit (c-style) type-cast from int to floatf = 1.275693e-5f;// Scientific notation float constant (1.27..x10^-5)d = 76.123e12; // Scientific notation double constant (76.123x10^12)

rate = 0.16f; // ERROR!return 0;

}

Page 31: Variables and Expressions  science-and-math-courses

Strings

● This'll be our next topic – after we look at arrays.

Page 32: Variables and Expressions  science-and-math-courses

More cmd-line output commands

int i = 9;float f;

// Cstyle printf("C STYLE\n=======\n"); f = 3.75f; printf("The float is %0.1f\n", f); f = 0.153; printf("The float is %0.1f\n", f); printf("The zero-padded float %015.2f\n", f); printf("The padded float %15.2f\n", f); printf("The zero-padded int %015d\n", i); printf("The padded int %15d\n\n", i);

Page 33: Variables and Expressions  science-and-math-courses

More cmd-line output commandsint i = 9;float f; // C++ style cout << "C++ STYLE" << endl << "=========" << endl; f = 3.75f; cout.precision(1); cout.setf(ios::fixed); cout << "The float is " << f << endl; f = 0.153; cout << "The float is " << f << endl; cout.setf(ios::right | ios::fixed); cout.precision(2); cout.fill('0'); cout << "The zero-padded float "; cout.width(15); // Only affects next cout. cout << f << endl; cout.fill(' '); cout << "The padded float "; cout.width(15); cout << f << endl; cout << "The zero-padded int "; cout.width(15); cout.fill('0'); cout << i << endl; cout << "The padded int "; cout.width(15); cout.fill(' '); cout << i << endl;

Page 34: Variables and Expressions  science-and-math-courses

Part III

● Statements● Building-blocks of statements

http://barkersbite.blogspot.com/2010/10/alex-box.html

Page 35: Variables and Expressions  science-and-math-courses

Statements

● A single “command” in C/C++– May be made of several parts– Sometimes quite complex

● Made of combinations of:– Variable Declarations– Expressions

● Operators● Constants● Values● Function Calls

– Structure Definitions (classes, struct's, etc.)– Function definitions– Template descriptions– ...

Page 36: Variables and Expressions  science-and-math-courses

Variable Declarations

● We've seen these already...– Usually placed at the top of a code block– In C++ you can place them almost anywhere.

int main(){int i, w=9, z;float j = 3.1;

i = 0;float q = 9.0; // C++ onlywhile (i < 5){

float k = j * i;i++;

}}

Page 37: Variables and Expressions  science-and-math-courses

Operators

● Three types– Unary (++ and -- and – (negation))– Binary (+, - (subtraction), *, /, %, >>, <<, |, &, ~, ^, =)

● (There are more)

– Ternary ( ?: )

Page 38: Variables and Expressions  science-and-math-courses

Unary operatorsint main(){int i, j, k = -15; // Note the '-' here is a unary operatorfloat f;

i = 5;i++; // i is now holding a 6++i; // i is now holding a 7j = i++; // i is now holding an 8, j has a 7j = ++i; // i is now holding a 9, j has a 9i--; // now i's holding a 8f = 3.1f;f++;

i = -j; // The unary operator again

j = 22; // 0000 ... 0001 0110 in binary k = ~j; // bitwise INVERT. Bits in k are the "opposite" of j. // 1111 .... 1110 1001 (-23 decimal)

return 0;}

Page 39: Variables and Expressions  science-and-math-courses

Binary Operators● The usual operators:

+

-

*

/● Type of operands affects the result of the operator (the other

operand is implicitly converted to be of the “bigger” type)Type Note

long double

double

float

unsigned long / long A unsigned long is picked if both values are positive. Otherwise, a long is used.

unsigned int / int “ditto”

unsigned short / short “ditto”

unsigned char / char “ditto”

bigger

Page 40: Variables and Expressions  science-and-math-courses

Binary Operators, cont.● Assignment

– Uses the equal (=) operator– The left-hand operand must always be a variable– The right-hand side can be an arbitrarily complex

expression● Must evaluate to a value

– Implicit Type Conversion● For built-in types, done automatically● May result in a loss of data

int main(){int i, j;float f;char c; i = 500; j = 301; f = i + j; // f will store 801.0f c = i + j; // c will store 33 return 0;}

Page 41: Variables and Expressions  science-and-math-courses

Binary Operators, cont.

● Modulo– The “remainder” operator– Only works with integer types (char, short, int, …)

int main(){int i, j;

i = 10 % 3; // i will hold a 1i = -15 % 2; // i will hold a -1/* … other code (changing i and j) … */i = i % j; // i will be set to the remainder of i / j/* ...more code...*/if (i % 2 == 0){

// We'll get into this block if i is even}return 0;

}

Page 42: Variables and Expressions  science-and-math-courses

Binary Operators, cont.● Bitwise operators (&, |, ~, ^)

– Only work with integer types– Manipulate the binary number encoded in the integer.

int main(){int i, j, k;

i = 0x00000005; // A hexadecimal constant. 0000 ... 0000 0101 in binary j = 22; // 0000 ... 0001 0110 in binary

k = i & j; // bitwise AND (set bits in k to 1 if they are 1 in // BOTH i AND j) // 0000 .... 0000 0100 (4 decimal) k = i | j; // bitwise OR (set bits in k to 1 if they are 1 in // EITHER i OR j) // 0000 .... 0001 0111 (23 decimal)

k = i ^ j; // bitwise XOR (exclusive OR). Sets bits in k to 1 // if they are different in j and k

// 0000 .... 0001 0011 (19 decimal)

return 0;}

Page 43: Variables and Expressions  science-and-math-courses

Binary Operators, cont.

int main(){int i, j, k;

i = 0x00000005; // A hexadecimal constant. 0000 ... 0000 0101 in binary j = 22; // 0000 ... 0001 0110 in binary

k = j >> 1; // bitwise RSHIFT (shifts all bits to the right // (they fall off the "edge")) // 0000 .... 0000 1011 (11 decimal) // Note: equivalent (but faster) than k = j / 2; k = j >> 2; // 0000 .... 0000 0101 (5 decimal) // Note: equivalent (but faster) than k = j / 4; k = j >> 3; // 0000 .... 0000 0010 (2 decimal) // Note: equivalent (but faster) than k = j / 8; k = j << 1; // bitwise LSHIFT (shifts all bits to the left // (adding 0's to the right)) // 0000 .... 0010 1100 (44 decimal) // Note: equivalent (but faster) than k = j * 2;

return 0;}

int main(){int i, j, k;

i = 0x00000005; // A hexadecimal constant. 0000 ... 0000 0101 in binary j = 22; // 0000 ... 0001 0110 in binary

k = j >> 1; // bitwise RSHIFT (shifts all bits to the right // (they fall off the "edge")) // 0000 .... 0000 1011 (11 decimal) // Note: equivalent (but faster) than k = j / 2; k = j >> 2; // 0000 .... 0000 0101 (5 decimal) // Note: equivalent (but faster) than k = j / 4; k = j >> 3; // 0000 .... 0000 0010 (2 decimal) // Note: equivalent (but faster) than k = j / 8; k = j << 1; // bitwise LSHIFT (shifts all bits to the left // (adding 0's to the right)) // 0000 .... 0010 1100 (44 decimal) // Note: equivalent (but faster) than k = j * 2;

return 0;}

Page 44: Variables and Expressions  science-and-math-courses

Ternary Operator

● Syntax:

[boolean-expr] ? [true-expr] : [false-expr]

int main(){int i, j, k;

cout << “Enter two integers: “;cin >> i >> j;

// Using the ternary operator to find the square of the biggerk = i > j ? i * i : j * j; // We'll cover comparison operators next

time.

return 0;}

Page 45: Variables and Expressions  science-and-math-courses

Order of operations● Like in math, some operators are evaluated first● We can change the order of operations by using parentheses

1. ()

2. ~ - (unary) & (address-of) ++ --

3. * / %

4. + (addition) - (subtraction)

5. << >>

6. & (bitwise and)

7. ^ (bitwise XOR)

8. | (bitwise OR)

9. ?:

10. =● (There are more which we'll look at later)

Performed sooner

Page 46: Variables and Expressions  science-and-math-courses

Part IV

● Executable File Format

Page 47: Variables and Expressions  science-and-math-courses

Major sections

Stack

Global Variables

Import Table

Text

Heap

Stack: All variables we've been using so far exist here. Later (when we get to functions), we'll see it also is integral in function calls.

Heap: A different way to allocate variables / data. We'll see this when we get to dynamic allocation (malloc & new).

GlobalVariables: A third way to allocate variables. These are in existence the entire duration of the program's run.

Import Table: Used to call OS functionality (in DLL's or shared libraries). We'll look at this when we import external libraries (e.g. OpenGL).

Text: Holds the machine code instructions (encoded as numbers).