Title: ENG1602 Engineering Computing
1 BACHELOR of ENGINEERING ENGINEERING COMPUTING
ENG1602
LECTURES Semester 1 2004
2ENGINEERING COMPUTING ENG1602
Week 4 - Lecture 1 Number and Character
Representations, Arithmetic
3Computer Arithmetic Representing Integers
- Suppose our basic word-size is N bits
- ( typically N is 8, 16, 32 or 64)
- If we want to represent non negative numbers,
then the most obvious representation is one that
covers a range of values from 0 .. 2N-1 . - E.g. for 8 bits the range of positive numbers
able to be represented becomes 0 .. 28-1 0
.. 255
4Decimal Binary Representation
- Consider the Number 1010
- In the normal Decimal system this may be written
as 101010 and is evaluated as follows
1x1030x1021x1010x100 - Value 101010 100001000 1010
- In the Binary system 1010 is a number that may be
written as 10102 and is evaluated as follows
1x230x221x210x20 - Value 10102 8020 1010 a totally
different value
58 Bit Unsigned Integer
- Extending the idea to 8 bits or 1 Byte gives the
following values - 00000000 0 0x270x26 ....0x210x20
- 00000001 1
- 00000010 2
- 00000011 3
- ....
- 11111111 255 1x271x26 ....1x211x20
6Handling Negative Values - 1.
- If we wish to represent the sign of the integer
then we can use one bit to represent the sign. By
convention a 0 in the sign bit means the number
is ve. - This leaves (N - 1) bits to represent the rest of
the number. - Usually the Highest Order bit is used for the
sign and the most obvious mapping for positive
numbers is used.
7Handling Negative Values - 2.
- Again using a Byte ( 8 bits)
First bit for Sign
next 7 bits for
numeric value, then
numeric range becomes - 0 0000000 0
- 0 0000001 1
- 0 0000010 2
- 0 ....
- 0 1111111 127
8Handling Negative Values - 3.
- How can we handle Negative Numbers ?
- The most common process for negative numbers is
subtraction. - To make the process simple computers do
subtraction by using the addition process - If we require to determine (a - b)
- Then the CPU-ALU will rather do it by addition ,
whereupon the process becomes (a (-b))
9Handling Negative Values - 4.
- For this to work we want for example to arrange
things so that
1 - 1 1
(-1) 0 - we must now represent -1 as
1 ??????? - The bit pattern ??????? must be chosen so that
the process of binary addition is implemented the
same for positive and negative numbers.
10Handling Negative Values - 5.
- Try the obvious solution
1 0000001 -1 - Then
0 0000001
1 0000001
1 00000102 -210 Clearly
Wrong
11Handling Negative Values - 6.
- Try the less obvious solution
1 1111111 -1 - Then
0 0000001
1 1111111
10
00000002 010 Correct if we neglect
the
extra leading digit.
12Handling Negative Values - 6.
- Expanding this idea for the full range of
possible Binary numbers leads to
0 0000000 0
0 0000001 1
..
0 1111111 127
1 0000000 -128
1 0000001
-127
..
1 1111111 -1
13Handling Negative Values - 7.
- This sequence is cyclic similar to the digits on
the odometer of a car. - Adding 1 to the numbers will roll the odometer
in one direction. - Subtracting 1 from the numbers will roll the
odometer in the reverse direction. - This representation of negative numbers is called
the 2s COMPLEMENT
14Determination of the 2s Complement for a given
Number
- If the number is ve and less than 2N-1 - 1
- Use standard Binary representation and set
leading bit to 0 - If the number is -ve (and has magnitude less than
2N-1 ) - Determine the Binary representation of the
magnitude - Take the 1s complement of the Binary Bit Pattern
( i.e. change 1 to 0 and
0 to 1 ) - Add 1 to give the 2s Complement
152s Complement for a given Number Example
- Determine the 2s Complement of -1010
- Binary Bit pattern for magnitude 10
0 00010102 1010 - Determine the 1s Complement by switching 1s to
0s and 0s to 1s
0 00010102
1 1110101 1s Complement - Add 1 to 1s Complement
1 1110101 (1s Complement)
0 0000001 (12 )
1 11101102 (2s
Complement ) -1010
16Representing Floating Point Numbers - 1.
- REAL Numbers have a decimal or Fractional part.
- One Decimal Representation
- 3.1416, 0.001, 123.456
- Scientific Notation
- 3.1416E00, 1.0E-3, 1.234E2
- The first part is called the Mantissa
- The second part is called the Exponent
- ( excluding the E)
17Representing Floating Point Numbers - 2.
- Separate bit patterns must be used for Mantissa
and Exponent within the computer memory and the
internal representation is thus more complicated
than that used for Integers. - As a rule of thumb, if 32 Bits (4 Bytes are used)
to represent a floating point Number, then for
positive or negative numbers - Accuracy will be 6-8 digits
- Range roughly from -1038 .. 1038
- if 64 Bits (8 Bytes are used)
- Accuracy will be 15-16 digits
- Range roughly from -10308 .. 10308
18Properties Integer / Floating Point
- Integers are of Ordinal Type given any integer
you can always state the next or the previous
integer in the sequence. - Floating point numbers are NOT of ordinal type -
there is no next number to, say 10.01.
19Character Representation - 1.
- Characters include
- alphanumeric (alphabet numerals)
- Other printable
- Non-printable (bell, line-feed etc)
20Character Representation - 2.
- Most common Character Mapping is the ASCII
character set. Recently expanded to Unicode. - Printable characters start after 32 ( Space)
- Digits 0 .. 9 are Characters 48 .. 57
- Letters A .. Z are Characters 65 .. 90
- Letters a .. z are Characters 97 ..122
- This is an ordered representation e.g. Character
B has a code one more than Character A
21Character Representation - 3
- A sequence of characters is called a String
- Since strings may have varying lengths they need
some special provision to tell where they end in
memory.
22Binary Representation of Variables
- Decimal Pure Binary 5 00000101
- Character Binary (ASCII Code) 5 00110
101 - A 01000001
- Z 10010000
- a 01100001
23ENGINEERING COMPUTING ENG1602
- End Of Week 4 Lecture 1
- Next Week
- Java Language Syntax
- Variables
- Expressions
- Input Output
- Simple program.