Download presentation
Presentation is loading. Please wait.
1
FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4
2
How do Computers represent 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”
3
How many possibilities? 1 switch 2 switches 3 switches … “n” switches
4
A “code” Ceasar Cipher – Shift the letters TED becomes VGF Something is represented as something else AC BD CE DF EG FH ……
5
Coding Letters How many letters? How many “switches” (bits)
6
What is a number? 324 A number is a shorthand for a polynomial 324 “means” (3*100)+(2*10)+(4*1) Or : (3*10 2 )+(2*10 1 )+(4*10 0 ) Why do we use 10 numerals? {0,1,2,3,4,5,6,7,8,9}
7
A possible code Every letter is a 26 bit sequence A is “10000000000000000000000000” B is “01000000000000000000000000” “TED” is : – 00000000000000000010000000 00001000000000000000000000 00010000000000000000000000
8
A beter code Use all possible combinations TED becomes 010110010000011 A00000 B00001 C00010 D00011 E00100 F00101 ……
9
What is a number? 324 A number is a shorthand for a polynomial 324 “means” (3*100)+(2*10)+(4*1) Or : (3*10 2 )+(2*10 1 )+(4*10 0 ) Why do we use 10 numerals? {0,1,2,3,4,5,6,7,8,9}
10
Binary Digits (bits) Since computers only have two states they count in base 2 (binary) 1101 2 1101 2 = (1*8)+(1*4)+(0*2)+(1*1) Or = (1*2 3 )+(1*2 2 )+(0*2 1 )+(1*2 0 ) Or = 13 10 Note: 8 “bits” is a “byte”
11
PAIRS EXCERCISE Convert the following binary numbers into base 10 10101 00111 11010 01010 abcde = (a*2 4 )+(b*2 3 )+(c*2 2 )+(d*2 1 )+(e*2 0 )
12
And, of course Convert the following base 10 numbers into binary 7, 12, 5, 19 abcde=(a*2 4 )+(b*2 3 )+(c*2 2 )+(d*2 1 )+(e*2 0 )
13
NUMBER TYPES IN JAVA int : integers, no fractional part – 1 – -4 – 0 double: floating point numbers – 0.5 – 0.0 – -3.1111
14
Operations depend on type “/” is the division operator 7.0 / 4.0 yields 1.75 7 / 4 yields 1 Note the “remainder” operator is useful for integers 7 % 4 yields 3
15
“PRIMITIVE” types in JAVA boolean8 bits char16 bits byte8 bits short16 bits int32 bits long64 bits float32 bits double64 bits
16
Assignment public void addNickels(int count) { nickels = nickels + count; }
17
Need to draw this
18
Increment and decrement nickles++ is shorthand for nickles = nickles + 1; or Nickles += 1;
19
Some shorthand options int count; count = 0; count++; count--; ++count; --count;
20
Constant variables Sort of like “jumbo shrimp” or “freezer burn”? “Magic numbers” are bad style (to the blackboard, robin!)
21
Static finals Enable constants Math.PI Enable functions Math.sqrt(x)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.