Title: Computer Math
1Computer Math
2Binary and Computers
Binary computers have storage units called binary
digits or bits
Low Voltage 0 High Voltage 1 all
bits have 0 or 1
22
3Binary and Computers
8 bits 1 byte The number of bytes in a word
determines the word length of the computer
32-bit machines 64-bit machines etc.
23
4Binary Representations
- One bit can be either 0 or 1. Therefore, one bit
can represent only two things. - To represent more than two things, we need
multiple bits. Two bits can represent four things
because there are four combinations of 0 and 1
that can be made from two bits 00, 01, 10,11.
5Binary Representations (Contd)
- If we want to represent more than four things, we
need more than two bits. Three bits can represent
eight things because there are eight combinations
of 0 and 1 that can be made from three bits.
6Binary Alphanumeric Codes
- A binary code is a group of n bits that assume up
to 2n distinct combinations of 1s and 0s with
each combination representing one element of the
set that is being coded- i.e. permutations - With two bits we can form a set of four elements
- With three bits we can represent 8 elements
- With four bits we can represent 16 elements
7Binary Representations (Contd)
Figure 3.4 Bit combinations
8Binary Representations (Contd)
- In general, ? bits can represent 2? things
because there are 2? combinations of 0 and 1 that
can be made from n bits. Note that every time we
increase the number of bits by 1, we double the
number of things we can represent.
9Memory Units
- 1 nibble
- 1 byte
- 1 word
- 1 long word
- 1 quad word
- 1 octa-word
- 4 consecutive bits
- 8 consecutive bits
- 2 consecutive bytes
- 4 consecutive bytes
- 8 consecutive bytes
- 16 consecutive bytes
10Larger Units of Memory
- 1 Kilobyte
- 1 Megabyte
- 1 Gigabyte
- 1 Terabyte
- 1 Petabyte
- 1 Exabyte
- 1024 bytes
- 106 bytes
- 109 bytes
- 1012 bytes
- 1015 bytes
- 1018 bytes
32 Mb 32103 Kb 32 103 1024 bytes
32,768,000 bytes
11Representing Data
- The computer knows the type of data stored in a
particular location from the context in which the
data are being used - i.e. individual bytes, a word, a longword, etc
- 01100011 01100101 01000100 01000000
- Bytes 99(10, 101 (10, 68 (10, 64(10
- Two byte words 24,445 (10 and 17,472 (10
- Longword 1,667,580,992 (10
12Numbers
Natural Numbers Zero and any number obtained by
repeatedly adding one to it. Examples 100, 0,
45645, 32
Negative Numbers A value less than 0, with a
sign Examples -24, -1, -45645, -32
2
13Numbers (Contd)
Integers A natural number, a negative number,
zero Examples 249, 0, - 45645, - 32
Rational Numbers An integer or the quotient of
two integers Examples -249, -1, 0, ¼ , - ½
3
14Natural Numbers
How many ones are there in 642?
600 40 2 ? Or is it 384 32 2 ? --
Octal Or maybe 1536 64 2 ? -- Hexadecimal
4
15Natural Numbers
642 is 600 40 2 in BASE 10 The base of a
number determines the number of digits and the
value of digit positions
5
16Positional Notation
Continuing with our example 642 in base 10
positional notation is 6 x 10² 6 x
100 600 4 x 10¹ 4 x 10
40 2 x 10º 2 x 1 2
642 in base 10
The power indicates the position of the number
This number is in base 10
6
17Positional Notation
R is the base of the number
As a formula
dn Rn-1 dn-1 Rn-2 ... d2 R d1
n is the number of digits in the number
d is the digit in the ith position in the
number
642 is 63 102 42 10 21
7
18Positional Notation
What if 642 has the base of 13? 642
in base 13 is equivalent to 1068 in base 10
6 x 13² 6 x 169 1014 4 x 13¹
4 x 13 52 2 x 13º 2 x 1
2 1068 in base 10
6
8
19Binary
Decimal is base 10 and has 10 digits
0,1,2,3,4,5,6,7,8,9 Binary is base 2 and has 2
digits 0,1
For a number to exist in a given number system,
the number system must include those digits. For
example The number 284 only exists in base 9 and
higher.
9
20Codes
- Given any positive integer base (RADIX) N, there
are N different individual symbols that can be
used to write numbers in the system. The value of
these symbols range from 0 to N-1 - All systems we use in computing are positional
systems - 495 400 90 5
21Number Systems
- We use the DECIMAL (10 system
- Computers use BINARY (2 or some shorthand for it
like OCTAL (8 or HEXADECIMAL (16
22Power of 2 Number System
Binary Octal Decimal
000 0 0
001 1 1
010 2 2
011 3 3
100 4 4
101 5 5
110 6 6
111 7 7
100 10 8
1001 11 9
1010 12 10
16
23Bases Higher than 10
How are digits in bases higher than 10
represented?
Base 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E, and F
10
24Conversions
25Decimal Equivalents
- Assuming the bits are unsigned, the decimal value
represented by the bits of a byte can be
calculated as follows - Number the bits beginning on the right using
superscripts beginning with 0 and increasing as
you move left - Note 20, by definition is 1
- Use each superscript as an exponent of a power of
2 - Multiply the value of each bit by its
corresponding power of 2 - Add the products obtained
26Converting Octal to Decimal
What is the decimal equivalent of the octal
number 642?
6 x 8² 6 x 64 384 4 x 8¹
4 x 8 32 2 x 8º 2 x 1
2 418 in base 10
11
27Converting Hexadecimal to Decimal
What is the decimal equivalent of the hexadecimal
number DEF?
D x 16² 13 x 256 3328 E x 16¹
14 x 16 224 F x 16º 15 x 1
15 3567 in base 10
Remember, base 16 is 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E
,F
28Converting Binary to Decimal
What is the decimal equivalent of the binary
number 1101110?
1 x 26 1 x 64 64 1 x 25 1
x 32 32 0 x 24 0 x 16 0 1
x 23 1 x 8 8 1 x 22 1 x 4
4 1 x 21 1 x 2 2 0 x 2º
0 x 1 0 112 in base 10
13
29Horners Method
- Another procedure to calculate the decimal
equivalent of a binary number - Note This method works with any base
- Horners Method
- Step 1 Start with the first digit on the left
- Step 2 Multiply it by the base
- Step 3 Add the next digit
- Step 4 Multiply the sum by the base
- Step 5 Continue the process until you add the
last digit
30Converting Binary to Octal
- Groups of Three (from right)
- Convert each group
- 10101011 10 101 011
- 2 5 3
- 10101011 is 253 in base 8
-
17
31Binary to Hex
- Step 1 Form four-bit groups beginning from the
rightmost bit of the binary number - If the last group (at the leftmost position) has
less than four bits, add extra zeros to the left
of the group to make it a four-bit group - 0110011110101010100111 becomes
- 0001 1001 1110 1010 1010 0111
- Step 2 Replace each four-bit group by its
hexadecimal equivalent - 19EAA7(16
32Converting Decimal to Other Bases
- Step 1 Divide the number by the base you are
converting to (r) - Step 2 Successively divide the quotients by (r)
until a zero quotient is obtained - Step 3 The decimal equivalent is obtained by
writing the remainders of the successive division
in the opposite order in which they were obtained - Know as modulus arithmetic
- Step 4 Verify the result by multiplying it out
33Converting Decimal to Hexadecimal
222 13 0
16 3567 16 222 16 13 32
16 0
36 62 13 32 48
47 14 32
15
D E F
21
34Addition Subtraction Terms
- A B
- A is the augend
- B is the addend
- C D
- C is the minuend
- D is the subtrahend
35Addition Rules All Bases
- Addition
- Step 1 Add a column of numbers
- Step 2 Determine if there is a single symbol for
the result - Step 3 If so, write it and go to the next
column. If not, write the accompanying number
and carry the appropriate value to the next column
36Addition of Binary Numbers
- Rules for adding or subtracting very similar to
the ones in decimal system - Limited to only two digits
- 0 0 0
- 0 1 1
- 1 0 1
- 1 1 0 carry 1
37Arithmetic in Binary
Remember there are only 2 digits in binary 0
and 1 Position is key, carry values are
used
Carry Values
1 1 1 1 1 1 1 0 1 0 1 1 1
1 0 0 1 0 1 1 1 0 1 0 0 0 1 0
14
38Number Overflow
- Overflow occurs when the value that we compute
cannot fit into the number of bits we have
allocated for the result. For example, if each
value is stored using eight bits, adding 127 to 3
overflow - 01111111
- 00000011
- 10000010
- Overflow is a classic example of the type of
problems we encounter by mapping an infinite
world onto a finite machine.
39Subtraction Rules All Bases
- Step1 Start with the rightmost column, if the
column of the minuend is greater than that of the
subtrahend, do the subtraction, if not - Step 2 Borrow one unit from the digit to the
left of the once being processed - The borrowed unit is equal to borrowing the
radix - Step 4 Decrease the column form which you
borrowed by one - Step 3 Subtract the subtrahend from the minuend
and go to the next column
40Subtracting Binary Numbers
Remember borrowing? Apply that concept here
1 2
2 0 2 1 0 1 0 1 1 1
- 1 1 1 0 1 1
0 0 1 1 1 0 0
15
41Addition Subtraction of Hex
- Due to the propensity for errors in binary, it is
preferable to carry out arithmetic in hexadecimal
and convert back to binary - If we need to borrow in hex, we borrow 16
- It is convenient to think in decimal and then
translate the results back to hex
42Representing Real Numbers
- Real numbers have a whole part and a fractional
part. For example 104.32, 0.999999, 357.0, and
3.14159 the digits represent values according to
their position, and those position values are
relative to the base. - The positions to the right of the decimal point
are the tenths position (10-1 or one tenth), the
hundredths position (10-2 or one hundredth), etc.
43Representing Real Numbers (Contd)
- In binary, the same rules apply but the base
value is 2. Since we are not working in base 10,
the decimal point is referred to as a radix
point. - The positions to the right of the radix point in
binary are the halves position (2-1 or one half),
the quarters position (2-2 or one quarter), etc.
44Representing Real Numbers (Contd)
- A real value in base 10 can be defined by the
following formula -
- The representation is called floating point
because the number of digits is fixed but the
radix point floats.
45Representing Real Numbers (Contd)
- Likewise, a binary floating point value is
defined by the following formula - sign mantissa 2exp
46Representing Real Numbers (Contd)
- Scientific notation is a term with which you may
already be familiar, so we mention it here.
Scientific notation is a form of floating-point
representation in which the decimal point is kept
to the right of the leftmost digit. - For example, 12001.32708 would be written as
1.200132708E4 in scientific notation.
47Representing Text
- To represent a text document in digital form, we
simply need to be able to represent every
possible character that may appear. - There are finite number of characters to
represent. So the general approach for
representing characters is to list them all and
assign each a binary string. - A character set is simply a list of characters
and the codes used to represent each one. By
agreeing to use a particular character set,
computer manufacturers have made the processing
of text data easier.
48Alphanumeric Codes
- American Standard Code for Information
Interchange (ASCII) - 7-bit code
- Since the unit of storage is a bit, all ASCII
codes are represented by 8 bits, with a zero in
the most significant digit - H e l l o W o r l d
- 48 65 6C 6C 6F 20 57 6F 72 6C 64
- Extended Binary Coded Decimal Interchange Code
(EBCDIC)
49The ASCII Character Set
- ASCII stands for American Standard Code for
Information Interchange. The ASCII character set
originally used seven bits to represent each
character, allowing for 128 unique characters. - Later ASCII evolved so that all eight bits were
used which allows for 256 characters.
50The ASCII Character Set (Contd)
51The ASCII Character Set (Contd)
- Note that the first 32 characters in the ASCII
character chart do not have a simple character
representation that you could print to the
screen.
52The Unicode Character Set
- The extended version of the ASCII character set
is not enough for international use. - The Unicode character set uses 16 bits per
character. Therefore, the Unicode character set
can represent 216, or over 65 thousand,
characters. - Unicode was designed to be a superset of ASCII.
That is, the first 256 characters in the Unicode
character set correspond exactly to the extended
ASCII character set.
53The Unicode Character Set (Contd)
A few characters in the Unicode character set
54Representing Signed Numbers
- Remember, all numeric data is represented inside
the computer as 1s and 0s - Arithmetic operations, particularly subtraction
raise the possibility that the result might be
negative - Any numerical convention needs to differentiate
two basic elements of any given number, its sign
and its magnitude - Conventions
- Sign-magnitude
- Ten's complement
- Twos complement
- Ones complement
55Representing Negatives
- It is necessary to choose one of the bits of the
basic unit as a sign bit - Usually the leftmost bit
- By convention, 0 is positive and 1 is negative
- Positive values have the same representation in
all conventions - However, in order to interpret the content of any
memory location correctly, it necessary to know
the convention being used used for negative
numbers
56Comparing the Conventions
57Representing Negative Values
- You have used the signed-magnitude representation
of numbers since grade school. The sign
represents the ordering, and the digits represent
the magnitude of the number.
58Representing Negative Values (Contd)
- There is a problem with the sign-magnitude
representation - There are two representations of zero.
- There is plus zero and minus zero. Two
representations of zero within a computer can
cause unnecessary complexity.
59Sign-Magnitude
- For a basic unit of N bits, the leftmost bit is
used exclusively to represent the sign - The remaining (N-1) bits are used for the
magnitude - The range of number represented in this
convention is 2 N1 to 2 N-1 -1
60Sign-magnitude Operations
- Addition of two numbers in sign-magnitude is
carried out using the usual conventions of binary
arithmetic - If both numbers are the same sign, we add their
magnitude and copy the same sign - If different signs, determine which number has
the larger magnitude and subtract the other from
it. The sign of the result is the sign of the
operand with the larger magnitude - If the result is outside the bounds of 2 n1 to
2 n-1 1, an overflow results
61Other Ways of Representing Negative Values
- If we allow only a fixed number of values, we can
represent numbers as just integer values, where
half of them represent negative numbers. - For example, if the maximum number of decimal
digits we can represent is two, we can let 1
through 49 be the positive numbers 1 through 49
and let 50 through 99 represent the negative
numbers -50 through -1.
62Representing Negative Values (Contd)
- To perform addition within this scheme, you just
add the numbers together and discard any carry.
63Representing Negative Values (Contd)
- A-BA(-B). We can subtract one number from
another by adding the negative of the second to
the first.
64Representing Negative Values (Contd)
- There is a formula that you can use to compute
the negative representation - This representation of negative numbers is called
the tens complement.
65Representing Negative Values (Contd)
Twos Complement To make it easier to look at
long binary numbers, we make the number line
vertical.
66Representing Negative Values (Contd)
- Addition and subtraction are accomplished the
same way as in 10s complement arithmetic - -127 10000001
- 1 00000001
- -126 10000010
- Notice that with this representation, the
leftmost bit in a negative number is always a 1.
67Twos Complement Convention
- A positive number is represented using a
procedure similar to sign-magnitude - To express a negative number
- Express the absolute value of the number in
binary - Change all the zeros to ones and all the ones to
zeros (called complementing the bits) - Add one to the number obtained in Step 2
- The range of negative numbers is one larger than
the range of positive numbers - Given a negative number, to find its positive
counterpart, use steps 2 3 above
68Twos Complement Operations
- Addition
- Treat the numbers as unsigned integers
- The sign bit is treated as any other number
- Ignore any carry on the leftmost position
- Subtraction
- Treat the numbers as unsigned integers
- If a "borrow" is necessary in the leftmost place,
borrow as if there were another invisible
one-bit to the left of the minuend
69Overflows in Twos Complement
- The range of values in twos-complement is 2
n1 to 2 n-1 1 - Results outside this band are overflows
- In all overflow conditions, the sign of the
result of the operation is different than that of
the operands - If the operands are positive, the result is
negative - If the operands are negative, the result is
positive
70Ones Complement
- Devised to make the addition of two numbers with
different signs the same as two numbers with the
same sign - Positive numbers are represented in the usual way
- For negatives
- STEP 1 Start with the binary representation of
the absolute value - STEP 2 Complement all of its bits
71One's Complement Operations
- Treat the sign bit as any other bit
- For addition, carry out of the leftmost bit is
added to the rightmost bit end-around carry