21
FUNDAMENTAL DATA TYPES FUNDAMENTAL DATA TYPES CSC 171 LECTURE 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

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

FUNDAMENTAL DATA TYPESFUNDAMENTAL DATA TYPES

CSC 171

LECTURE 4

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”

How many possibilities?How many possibilities?

1 switch2 switches3 switches…“n” switches

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

… …

Coding LettersCoding Letters

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

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}

A possible codeA possible code

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

– 00000000000000000010000000 00001000000000000000000000 00010000000000000000000000

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

… …

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}

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”

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)

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)

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

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

““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

AssignmentAssignment

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

Need to draw thisNeed to draw this

Increment and decrementIncrement and decrement

nickles++

is shorthand for

nickles = nickles + 1;

or

Nickles += 1;

Some shorthand optionsSome shorthand options

int count;

count = 0;

count++;

count--;

++count;

--count;

Constant variablesConstant variables

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

“Magic numbers” are bad style

(to the blackboard, robin!)

Static finalsStatic finals

Enable constantsMath.PI

Enable functionsMath.sqrt(x)