CSC152 2006S, Class 10: Binary Representation
Admin:
* Syllabus rearranged
* No programming homework for tomorrow
* Reading: Conditionals in Java (ready later today)
* Don't worry about understanding all of Javadoc; just be able to extract key methods
Overview:
* Numbers wrapup
* About binary representation
* Lab
Numbers:
* Questions?
* Tell us more about doubles and Doubles and the ilk
* In "normal math": integer and real (can have fractional part)
* In computers, various kinds of integers and reals
* Integers: int, short, long, byte
* Reals: double, float
* Generally a fixed number of bits.
* IN Java, 32 for int, 16 for short, 8 for byte, 64 for long
* For reals, something like 32 for float and 64 for double
* The limited number of bits means that we don't represent
most reals exactly; we simply approximate
* Tell us more about the crappy problem you wrote with both doubles and floats
* By default, if you write something that looks like a real, Java assumes you mean a double
3.1, "ah hah, that's a double"
* Doubles are more accurate than floats, so Java is loathe to treat a double as a float. (The reverse is not true.)
* Math.sqrt returns a double
* When you tried to treat the return value as a float, Java got upset.
* Solution
float f;
f = (float) Math.sqrt(2.0);
* So why did you write that crappy problem?
* I'm an idito.
* Why use floats if doubles are more accurate?
* Sometimes people care a lot about their memory usage, and don't need the accuracy
* Not an issue in this class. Use double.
* How do we read integers?
* Read a string
* Use Integer.parseInt to convert the string to an int
String in;
pen.print("Please enter a number: ");
pen.flush();
in = eyes.readLine();
int userinput;
userinput = Integer.parseInt(in);
* Similarly
pen.print("Please enter a number: "); pen.flush();
int userinput = Integer.parseInt(eyes.readLine());
Important lessons
* When you exceed the largest int (long, short, etc.) value,
you have unclear results.
* When you exceed the largest double, you get "infinity"
* Reading numeric input (see above)
Back to questions:
* How do you include the factional component?
* Use num.remainder(divisor); (from BigInteger)
Binary Representation
* Is it really the case that all of the other representations don't get quite the right answer in at least one case?
* Yes. That's why most computer scientists like two's complement.
* Can you explain inverting in two's complement?
* Think about it as a "process" issue: Make a positive number negative or a negative number positive.
* Two's complement: Two-step negation process: Flip the bits, add 1
* How do you tell whether a number is positive or negative?
* The leading bit tells you. If it's 0, the number is positive (or 0), if it's 1, the number is negative.