The Department of Computer Science & Engineering
cse@buffalo
STUART C. SHAPIRO: CSE 115 C

CSE 115
Introduction To Computer Science for Majors I
Lecture C
Lecture Notes
Stuart C. Shapiro
Spring, 2001


Arithmetic

Reading
Brown U. Notes, Chapter 7
Barnes, Chapter 5, Appendices A, B, C

public String toString()
Used to convert any object to a String, esp. for printing.
Predefined for numbers.
Can be added to any type.

Types of Numbers
Counting Numbers (Integers)
TypePrecisionMin ValueMax ValueExample Literal
byte8 bits-12812712
short2 bytes-32,76832,767-25328
int4 bytes-2,147,483,6482,147,483,647175625
long8 bytes-9,223,372,036,854,775,8089,223,372,036,854,775,807528000L
Measuring Numbers (Reals)
TypePrecisionMin ValueMax ValueExample Literal
float4 bytes±1.4×10-45±3.4028235×1038 -75.23F
double8 bytes±4.9×10-324±1.7976931348623157×10308324E-100

Defaults for literals are int and double.

Primitive (Base) Types
Not objects.
Stored directly in variables.
Most variables we've seen so far store references (pointers) to objects.

Expressions
Java code that evaluates to a reference to an object or to a value of a primitive type.
Where usually found
  • On the right-hand side of an assignment statement
    variable = expression;
  • As an actual parameter (argument) of a method call (message send)
    object.method(expression, ..., expression)
  • In a return statement
    return expression;
What composed of
  • literals
    "Skamper skamper skamper"
  • variables
    length
  • calls of non-void methods
    crossSection.getArea()
  • unary-operator expression
    - numberRemoved
  • expression binary-operator expression
    length * width
  • (expression)
    (3 + 4)

Arithmetic Operators
Unary Operator
- minus
Binary Operators
  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulus
Precedence
Innermost parentheses first
*, /, and % before + and -
else left to right
3 + 4 * 2 = 11
(3 + 4) * 2 = 14
100 / 50 / 2 = 1
100 / (50 / 2) = 4
Shortcuts
  • variable op= expression short for variable = variable op expression
    E.g. x += y; short for x = x + y;
  • ++variable or --variable means increment (decrement) by 1, then use.
    x = ++y; is equivalent to {y = y+1; x = y;}
  • variable++ or variable-- means use, then increment (decrement) by 1.
    x = y--; is equivalent to {x = y; y = y-1;}
Precision
The precision of expression op expression is the precision of the more precise expression. The value of the other is converted (coerced) to a more precise representation first.
bsh % print(10/6);
1
bsh % print(10%6);
4
bsh % print(10/6.0);
1.6666666666666667
bsh % int x = 10;
bsh % double y = 6.0;
bsh % print(x/y);
1.6666666666666667
bsh % print(x);
10

bsh % print(3 / 4 * 256);
0
bsh % print(3 * 256 / 4);
192

bsh % print(5E20F + 10E12F + 10E12F + 10E12F + 10E12F + 10E12F);
5.0E20
bsh % print(10E12F + 10E12F + 10E12F + 10E12F + 10E12F + 5E20F);
5.0000005E20
If x is more precise than expression in x = expression, the value of expression is converted to the precision of x before assignment.
If expression is more precise than x, it is an error.

Program

    public static void main (String[] args) {
	int x = 10;
	double y = 6.0;
	System.out.println(x + "     " + y);
	y = x;
	System.out.println(x + "     " + y);
    }
prints
10     6.0
10     10.0

Representation of Negative Integers
If use 8 bits to represent an integer, and want negatives as well as positives, need one bit to distinguish negatives from positive, leaving 7 bits for the magnitude.

With 7 bits, can represent 128 numbers, e.g. 0, ..., 127.
But what about negatives: -0, ..., -127?

First idea (sign + magnitude):

 3 = 00000011     0 = 00000000
-3 = 10000011    -0 = 10000000
Bad idea: addition is different; -0 is wasteful.

Second idea (biased):
use 00000000 for -127, 11111111 for 128.
That is, to represent n, use binary representation of 127 + n.

 3 = 10000010      0 = 01111111
-3 = 01111100     -0 = 01111111
Problem: 3 + -3 = 254. But see below:

Better idea: Two's Complement
To go from n to -n: complement bits, add 1

            0 = 00000000  1 = 00000001  3 = 00000011  4 = 00000100
complement      11111111      11111110      11111100      11111011
add 1       0 = 00000000 -1 = 11111111 -3 = 11111101 -4 = 11111100
No -0, and it's reversible.
Try some addition:
n + -n          00000000      00000000      00000000      00000000

                4 = 00000100        -3 = 11111101
               -1 = 11111111        -4 = 11111100
                    --------             --------
               sum  00000011 = 3   sum   11111001 = -7

Representation of Reals
Beyond the binary point
Decimal positions: 102 101 100 . 10-1 10-2
Binary positions: 22 21 20 . 2-1 2-2

Retrieve decimal fractional digits by repeated multiplication by 10.

        .32054
       3.2054
       2.054
       0.54
       5.4
       4.
So retrieve binary fractional bits by repeated multiplication by 2.
        .1101
       1.101
       1.01
       0.1
       1.
Even if "bits" are in decimal notation.
        .25
       0.5
       1.
So, .2510 = .012

Try 0.110:

        .1
       0.2
       0.4
       0.8
       1.6
       1.2
       0.4  repeat...
So, .110 = .0001100110011...2

Demonstration: /projects/CSE115/Classlibs/Demos/Counter/App.java

Machine Representation
IEEE 754 standard:
  1. Adjust exponent so mantissa is 1. ...
  2. Use left-most bit to represent sign of number.
  3. Use next several bits to represent exponent, biased.
  4. Use remaining bits to represent mantissa - 1.

First Previous Next

Copyright © 2001 by Stuart C. Shapiro. All rights reserved.

Stuart C. Shapiro <shapiro@cse.buffalo.edu>