Top Banner
FUNDAMENTAL DATA TYPES FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4
21

FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

FUNDAMENTAL DATA TYPESFUNDAMENTAL DATA TYPES

CSC 171

LECTURE 4

Page 2: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

How do Computers represent How do Computers represent information?information?

Computers are switches

Switches are “on” or “off”

Suppose we want to represent something

We can decide to interpret (semantics)

an “on” switch as a “1”

and an “off” switch as a “0”

Page 3: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

How many possibilities?How many possibilities?

1 switch2 switches3 switches…“n” switches

Page 4: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

A “code”A “code”

Ceasar Cipher– Shift the letters

TED becomes VGF

Something is represented as something else

A C

B D

C E

D F

E G

F H

… …

Page 5: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

Coding LettersCoding Letters

How many letters?How many “switches” (bits)

Page 6: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

What is a number?What is a number?324

A number is a shorthand for a polynomial

324 “means” (3*100)+(2*10)+(4*1)

Or : (3*102)+(2*101)+(4*100)

Why do we use 10 numerals?

{0,1,2,3,4,5,6,7,8,9}

Page 7: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

A possible codeA possible code

Every letter is a 26 bit sequenceA is “10000000000000000000000000”B is “01000000000000000000000000”“TED” is :

– 00000000000000000010000000 00001000000000000000000000 00010000000000000000000000

Page 8: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

A beter codeA beter code

Use all possible combinations

TED becomes 010110010000011 01011 00100 00011

A 00000

B 00001

C 00010

D 00011

E 00100

F 00101

… …

Page 9: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

What is a number?What is a number?324

A number is a shorthand for a polynomial

324 “means” (3*100)+(2*10)+(4*1)

Or : (3*102)+(2*101)+(4*100)

Why do we use 10 numerals?

{0,1,2,3,4,5,6,7,8,9}

Page 10: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

Binary Digits (bits)Binary Digits (bits)

Since computers only have two states they count in base 2 (binary)

11012

11012= (1*8)+(1*4)+(0*2)+(1*1)

Or = (1*23)+(1*22)+(0*21)+(1*20)

Or = 1310

Note: 8 “bits” is a “byte”

Page 11: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

PAIRS EXCERCISEPAIRS EXCERCISE

Convert the following binary numbers into base 10

10101 00111 11010 01010

abcde = (a*24)+(b*23)+(c*22)+(d*21)+(e*20)

Page 12: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

And, of courseAnd, of course

Convert the following base 10 numbers into binary

7, 12, 5, 19

abcde=(a*24)+(b*23)+(c*22)+(d*21)+(e*20)

Page 13: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

NUMBER TYPES IN JAVANUMBER TYPES IN JAVA

int : integers, no fractional part– 1– -4 – 0

double: floating point numbers– 0.5 – 0.0– -3.1111

Page 14: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

Operations depend on typeOperations depend on type

“/” is the division operator7.0 / 4.0 yields 1.757 / 4 yields 1

Note the “remainder” operator is useful for integers

7 % 4 yields 3

Page 15: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

““PRIMITIVE” types in JAVAPRIMITIVE” types in JAVA

boolean 8 bits

char 16 bits

byte 8 bits

short 16 bits

int 32 bits

long 64 bits

float 32 bits

double 64 bits

Page 16: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

AssignmentAssignment

public void addNickels(int count) {      nickels = nickels + count;   }

Page 17: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

Need to draw thisNeed to draw this

Page 18: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

Increment and decrementIncrement and decrement

nickles++

is shorthand for

nickles = nickles + 1;

or

Nickles += 1;

Page 19: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

Some shorthand optionsSome shorthand options

int count;

count = 0;

count++;

count--;

++count;

--count;

Page 20: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

Constant variablesConstant variables

Sort of like “jumbo shrimp” or “freezer burn”?

“Magic numbers” are bad style

(to the blackboard, robin!)

Page 21: FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.

Static finalsStatic finals

Enable constantsMath.PI

Enable functionsMath.sqrt(x)