• 210 likes • 384 Views
FUNDAMENTAL DATA TYPES. CSC 171 LECTURE 4. 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”.
E N D
FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4
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”
How many possibilities? • 1 switch • 2 switches • 3 switches • … • “n” switches
A “code” • Ceasar Cipher • Shift the letters • TED becomes VGF • Something is represented as something else
Coding Letters • How many letters? • How many “switches” (bits)
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 code • Every letter is a 26 bit sequence • A is “10000000000000000000000000” • B is “01000000000000000000000000” • “TED” is : • 00000000000000000010000000 00001000000000000000000000 00010000000000000000000000
A beter code • Use all possible combinations • TED becomes • 010110010000011 • 01011 00100 00011
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) 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 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 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 JAVA • int : integers, no fractional part • 1 • -4 • 0 • double: floating point numbers • 0.5 • 0.0 • -3.1111
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
Assignment • public void addNickels(int count) { nickels = nickels + count; }
Increment and decrement nickles++ is shorthand for nickles = nickles + 1; or Nickles += 1;
Some shorthand options int count; count = 0; count++; count--; ++count; --count;
Constant variables • Sort of like “jumbo shrimp” or “freezer burn”? • “Magic numbers” are bad style (to the blackboard, robin!)
Static finals • Enable constants Math.PI • Enable functions Math.sqrt(x)