Remove this slide
Download
Report
Transcript Remove this slide
More On Variables and Operators,
And Maths Functions
In this section we will learn more about variables in memory, more on the
operators in Java and something about the maths functions available:
Binary and hexadecimal numbers
Floating Point Numbers
Text
The division operator
Type conversion operators
Prefix and postfix operators
Assignment operators
Maths functions
PHY281
Variables operators and math
functions
Slide 1
Binary Numbers
Memory consists of thousands of
switches (transistors) which are
either on (=1) or off (=0) - called
binary digits or bits.
Eight such bits = One byte.
Most significant bit
Least significant bit
1 Byte:
Bit:
7 6 5 4 3 2
Value:
2 7 26 25 24 23 22
Decimal: 128 64 32 16 8 4
1 0
21 20
2 1
1 byte can represent 0 to 255
decimal (256 values in total).
Larger numbers are stored in
words which consist of several
bytes (often 4). Hence Windows
which uses 4 bytes per word is
known as a 32 bit (8 X 4)
operating system.
PHY281
Hence
3 is 2 + 1
= 0000 0011
25 is 16 +8 + 1 = 0001 1001
Variables operators and math
functions
Slide 2
Hexadecimal Numbers
With larger binary numbers it is better to use Hexadecimal (base
16) notation.
Each digit can have values from 0 to 15 (0 to 9 then A, B, C, D, E, F).
Each four binary digits is represented by one hexadecimal digit.
Digit:
5
4
3
4
3
Value:
16
16 162
Decimal: 65536 4096 256
2
161
16
1
160
1
e.g. 16,103,905 decimal is
1111 0101 1011 1001 1110 0001 Binary
F
5
B
9
E 1
i.e. F5B9E1 in Hexadecimal
Check :
15 X 165 + 5 X 164 + 11 X 163 + 9 X 162 + 14 X 16 + 1
= 16,103,905
PHY281
Variables operators and math
functions
Dec Hex
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 A
11 B
12 C
13 D
14 E
15 F
Slide 3
Binary and Hexadecimal
Decimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PHY281
Binary Hexadecimal
0000 0000
00
0000 0001
01
0000 0010
02
0000 0011
03
0000 0100
04
0000 0101
05
0000 0110
06
0000 0111
07
0000 1000
08
0000 1001
09
0000 1010
0A
0000 1011
0B
0000 1100
0C
0000 1101
0D
0000 1110
0E
0000 1111
0F
Decimal
16
17
18
Binary Hexadecimal
0001 0000
10
0001 0001
11
0001 0010
12
32
33
34
0010 0000
0010 0001
0010 0010
20
21
22
64
65
66
0100 0000
0100 0001
0100 0010
40
41
42
128
129
1000 0000
1000 0001
80
81
255
1111 1111
FF
Variables operators and math
functions
Slide 4
Binary Arithmetic
At their lowest level computers cannot Subtract, Multiply or
Divide - only Add (but very fast). Addition is done bit wise in
bytes or words.
Addition
6 + 5 = 11
0000 0110
+0000 0101
0000 1011
Computers cannot subtract. However, they can negate a
number and then add it to achieve a subtraction e.g.
6 - 5 becomes 6 + (-5)
PHY281
Variables operators and math
functions
Slide 5
Binary Subtraction
In order to allow negative numbers, the
leftmost (most significant) bit is designated
the sign bit. Cannot simply set the sign bit to
make a number negative.
6: 0000 0110
-5: +1000 0101
1000 1011 = -11
Wrong
Use 2’s compliment - which is 1’s compliment
(change all 0 to 1 and vice versa) plus 1.
5:
1’s complement:
Add 1
Hence -5 is:
Subtract
5-5=0
0000 0101
1111 1010
+0000 0001
1111 1011
5: 0000 0101
-5: +1111 1011
0000 0000 = 0
Instead of representing 0 to 255
decimal 1 byte now represents -128
to +127 (Still 256 values in total).
Subtract
6-5=1
6: 0000 0110
-5: +1111 1011
0000 0001 = 1
Carry over to left is thrown away
PHY281
Variables operators and math
functions
Slide 6
Binary Multiplication
6: 0000 0110
+6: +0000 0110
12 0000 1100
+6: +0000 0110
18 0001 0010
+6: +0000 0110
24 0011 0000
Since computers can only add, the simplest
way to multiply is to add repeatedly:
i.e. 4 X 6 = 6 + 6 + 6 + 6 = 24
Could be very time consuming for large
numbers. A better algorithm is to use
partial fractions.
In decimal 23 X 125 is 23 X 1 X 100 plus
23 X 2 X 10 plus 23 X 5 X 1 = 2875.
In binary one can use this technique by
taking one number and bit shifting it
according to the position of each bit in
the other number and then summing
them all up.
PHY281
23 X 125 is 0001 0111 X 0111 1101
0001 0111
0 0000 000
00 0101 11
000 1011 1
0001 0111
0 0010 111
00 0101 11
000 0000 0
1011 0011 1011 = 2875
Variables operators and math
functions
Slide 7
Binary Division
Division
Simplest method to divide 42 by 7: keep subtracting 7 from (adding -7 to)
42 till it reaches 0 and count how many times you did it.
In reality modern computers have dedicated hardware to perform
arithmetic calculations such as multiplication and division.
PHY281
Variables operators and math
functions
Slide 8
Floating Point Numbers
The numbers we have represented in binary so far such as 0, 6,
125, -5 are whole numbers known as Integers.
How do computers represent Floating Point numbers such as
12.2, 3.142, 0.5, -0.001, 1.602 x 10-19?
E for Exponent - nothing to do
with base e (natural logarithms)
First they are converted to a standard form:
0.122 x 102, 0.31 x 101, 0.5 x 100, -0.1 x 10-2, 0.1602 x 10-18
which are usually written
0.122E02, 0.31E01, 0.5E00, -0.1E-02, 0.1602E-18
i.e. (Mantissa)E(Exponent).
The actual representation varies with computer and
programming language. In Java floating point numbers are
stored as Sign x Mantissa x 2Exponent with the different parts
of the word storing the different components.
PHY281
Variables operators and math
functions
Slide 9
Floating Point Numbers
For example, the representation of 32 bit floating point numbers in Java is:
Bit 31
SEEE EEEE EMMM MMMM MMMM MMMM MMMM
One Sign bit
Eight bits for the Exponent
Bit 0
23 bits for the Mantissa
The 8 bits for the exponent part allow 256 different values (0 and 255 have
special meanings.) The actual exponent (power of 2) is given by the EEEEEEEE
part - 126 (bias) and hence range from -125 to 128 i.e. 2-125 (= 2.3 x 10-38) to 2128
(= 3.4 x 1038).
The Mantissa is calculated as:
2-1 + bit 23 x 2-2 + bit 22 x 2-3 + bit 22 x 2-4 + … + bit 2 x 2-23 + bit1 x 2-24.
The least significant bit (bit1) gives a value of 2-24 = 0.000000060 so these
numbers are accurate to approximately 7 digits.
In this system p would be represented as 0 10000000 10010010000111111011011
The exponent part is 128 - 126 = 2. The mantissa is 2-1 + 2-2 + 2-5 + 2-8 + ... =
0.5 + 0.25 + 0.03125 + ... = 0.78125 + … = 0.785398186 x 22 = 3.141592744
PHY281
Variables operators and math
functions
Slide 10
Representing Text
Computers can store integers and floating point numbers in binary but what about
text e.g. your Word Document?
Text is stored as individual characters A,a,B,b etc. Each character is stored in one
byte (8 bits) according to the ASCII (American Standard Code for Information
Exchange) Table. The normal characters, numbers and symbols, plus some control
codes are stored in the first 128 characters (7 bits). Some languages such as
Japanese cannot be accommodated in 8 bits so there is an extended version call
Unicode which uses 2 bytes (16 bits or 65,535 characters).
Each character, both uppercase and lowercase letters, even the space, has its own
unique ASCII code. Note that the ASCII code for numerals is not the same
as the integer representation of that number. The ASCII code for the character
‘9’ is 0011 1001 (decimal 57) whereas the integer representation is 0000 1001.
If you press ‘A’ on your keyboard the ASCII value 0100 0001 is sent to the
computer and stored maybe in your Word Document or sent to the printer. The
printer looks up which character the 0100 0001 corresponds to before printing it.
PHY281
Variables operators and math
functions
Slide 11
ASCII Codes
Dec Char
000 NUL
001 SOH
002 STX
003 ETX
004 EOT
005 ENQ
006 ACK
007 BEL
008 BS
009 TAB
010 LF
011 VT
012 FF
013 CR
014 SO
015 SI
Dec Char
016 DLE
017 DC1
018 DC2
019 DC3
020 DC4
021 NAK
022 SYN
023 ETB
024 CAN
025 EM
026 SUB
027 ESC
028 FS
029 GS
030 RS
031 US
Dec Char
032
033 !
034 “
035 #
036 $
037 %
038 &
039
‘
040 (
041 )
042 *
043 +
044 ,
045 046 .
047 /
Dec Char
048 0
049 1
050 2
051 3
052 4
053 5
054 6
055 7
056 8
057 9
058 :
059 ;
060 <
061 =
062 >
063 ?
Dec Char
064 @
065 A
066 B
067 C
068 D
069 E
070 F
071 G
072 H
073 I
074 J
075 K
076 L
077 M
078 N
079 O
Dec Char
080 P
081 Q
082 R
083 S
084 T
085 U
086 V
087 W
088 X
089 Y
090 Z
091 [
092 \
093 ]
094 ^
095
Dec Char
096 `
097 a
098 b
099 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
Dec Char
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123 {
124 |
125 }
126 ~
127 DEL
The first 32 codes are control characters (mostly historical) - useful ones are TAB, LF
(line feed or new line), FF (Form feed), CR (Carriage return).
PHY281
Variables operators and math
functions
Slide 12
More on Operators
• You met your fist operators last week; we will
now learn a bit more about them. Some of the
things may seem a bit abstract for now, but
they will be useful in the future.
PHY281
Variables operators and math
functions
Slide 13
Integer Division
If you divide two integers, the answer will be truncated
to an integer value - this is usually NOT what you want.
Try this:
import java.awt.*;
import java.applet.Applet;
public class IntDiv extends Applet {
public void paint(Graphics
int i = 2/3;
double d1 = 2/3;
double d2 = 2.0/3.0;
g.drawString("i = " +
g.drawString("d1 = " +
g.drawString("d2 = " +
}
g) {
i, 50, 50);
d1, 50, 75);
d2, 50, 100);
}
PHY281
Variables operators and math
functions
Slide 14
Integer Division
import java.awt.*;
import java.applet.Applet;
public class IntDiv extends Applet {
public void paint(Graphics
int i = 2/3;
double d1 = 2/3;
double d2 = 2.0/3.0;
g.drawString("i = " +
g.drawString("d1 = " +
g.drawString("d2 = " +
}
g) {
i, 50, 50);
d1, 50, 75);
d2, 50, 100);
}
PHY281
Divides 2 by 3 then
truncates it to store it
as an integer.
Variables operators and math
functions
Since the RHS are both
integers does an integer
division as before. Then
converts the result to a
double.
Slide 15
Type Conversion
Sometimes you need to convert from one type to another. This is called casting
and is done by putting the required type in brackets before the variable.
int i = 2;
double x;
x = (double)i;
Converts (casts) i to a
double.
This can be used to solve the integer division problem:
double d1 = (double)2/3;
Converts integer 2 to a
double 2.0 before the
division. The result is
then a double.
If you do the reverse and cast a double to an integer you truncate it and
lose the decimal places.
double x = 2.4;
int i;
i = (int)x;
PHY281
Variables operators and math
functions
i becomes 2 and the 0.4
is lost.
Slide 16
Type Conversion 2
A special case is converting a String into a number. Remember, a String is an
object, not a variable, so you might have expected something different.
String text =“21.22”;
double x;
x =
Double.valueOf(text).doubleValue();
Converts (casts) i to a
double.
There are similar methods for converting the string to
Floats, Boolians etc:
PHY281
Variables operators and math
functions
Slide 17
Incrementing and Decrementing
A common task is to increase a number by 1 (incrementing) or decrease it by 1
(decrementing). There are two operators for this:
++ Increment
total++;
is equivalent to
total = total + 1;
is equivalent to
total = total - 1;
-- Decrement
total--;
PHY281
Variables operators and math
functions
Slide 18
Prefix and Postfix
If the ++ or -- comes after the variable (postfixed) the number is updated after
any calculation. If they come before the variable (prefixed) the number is updated
before the calculation.
Increments x before multiplication.
int x = 3;
int y = 3;
int pre = ++x * 10;
int post = y++ * 10;
g.drawString("pre = " + pre + " x = " + x, 50, 50);
g.drawString("post = " + post + " y = " + y, 50, 75);
pre = 40 x = 4
post = 30 y = 4
Increments y after multiplication.
To avoid confusion suggest you stick to postfix and don't use it in expressions.
int post = y * 10;
y++;
PHY281
Variables operators and math
functions
Slide 19
Operator Precedence
What value is x?
int y = 10;
int x = y * 3 + 5;
10 x 3 = 30 plus 5 = 35 or 3 + 5 = 8 x 10 = 80? Answer 35.
The following order (precedence) is used:
Incrementing and Decrementing first then
Multiplication, Division and Remainder then
Addition and Subtraction then
The equals sign to set a value.
30 + 20 = 50
5 x 6 = 30
4 x 5 = 20
int x = 4;
int number = ++x * 6 + 4 * 10 /2;
Operators with equal precedence are evaluated
left to right
If you want to change the precedence use brackets ( ).
int y = 10;
int x = y * (3 + 5);
PHY281
Variables operators and math
functions
Now gives 80
Slide 20
Assignment Operators
The simple assignment operator = sets the variable on the left of the = sign to
the value of the variable or expression on the right of the = sign.
y = x;
In addition there are a set of 'assignment and operator' operators of the form
<variable> <operator> = <expression or value>
These are equivalent to
<variable> = <variable> <operator> <expression or value>
x
x
x
x
x
+=
-=
*=
/=
%=
2;
2;
2;
2;
2;
myRatherLongName += 2;
PHY281
are equivalent to
x
x
x
x
x
=
=
=
=
=
x
x
x
x
x
+
*
/
%
2;
2;
2;
2;
2;
myRatherLongName = myRatherLongName + 2;
Variables operators and math
functions
Slide 21
Other Operators
In addition there are:
Comparison Operators - used to compare variables or objects
<
<=
>
>=
==
!=
less than
less than or equal to
greater than
greater or equal to
equal to
not equal to
Logical Operators - used to perform logical operations
&&
||
AND
OR
Bitwise Operators - used to perform binary operations.
We shall use some of these when we make decisions later on.
PHY281
Variables operators and math
functions
Slide 22
Mathematical Functions
In scientific applications you will need to use certain mathematical functions like
sine, cosine and log. In Java these are provided in the maths library. To use one of
these functions you do not need an import statement as it is already included but
you do need to precede the name with Math. like this:
y = Math.sqrt(x);
which calculates the square root of the parameter x.
The most widely used functions are these (x is a floating point number):
• Math.cos(x)
cosine of the angle x where x is in radians
• Math. sin(x)
sine of the angle x where x is in radians
• Math. tan(x)
tangent of the angle x where x is in radians
• Math. abs(x)
the absolute value of x i.e. |x| in mathematics
• Math. min(x,y) the smaller of x and y.
• Math. max(x,y) the larger of x and y.
• Math. round(x) rounds a floating point number to the nearest integer.
• Math. log(x)
natural (base e) logarithm of x.
• Math. random( ) a pseudo random number in the range 0.0 to 0.9999…
• Math. sqrt(x)
the positive square root of x
• Math. pow(x,y) x raised to the power y i.e xy.
• Math. exp(x)
ex .
The library also provides the constants Math.E and Math.PI for the values of e and p.
PHY281
Variables operators and math
functions
Slide 23