Download number systems

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Approximations of π wikipedia , lookup

Arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Location arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
NUMBER SYSTEMS
Decimal Number System
 Digits (or symbols) allowed: 0-9
 Base (or radix) – number of unique digits = 10
 The digits are usually place in positions with the digit in the
most significant position having the greatest value
e.g. 345 is
3 x 102 + 4 x 101 + 5 x 100
 This means that the value of a digit in a given position is
calculated by multiplying the digit by radix raised to the power
of the position, where the least significant position is at
position zero
Binary Number System
 Digits (symbols) allowed: 0, 1
 Base (radix)= 2
 Example
 101
 Actual value = 1 x 22 +0 x 21 +1 x 20=5 (this is the decimal
system value)
Get the value of 1001
Get the value of 11000
 We can represent the same value using the binary number
system or the decimal number system
 Computers represent values using the binary system while
humans use the decimal system
Octal Number System
 Digits (symbols) allowed: 0-7
 Base (radix)= 8
 Example octal 77
 Decimal Value = 781+7  80=63
 Get the decimal values of the following octal numbers
 10001
 570
 Some representations of octal numbers on code
 q77, 0o203, @250
 Different programming languages will use different
representations
Hexadecimal Number System
 Digits (symbols) allowed: 0-9, A-F
 Base (radix): 16
 Actual Digits
0–9
 10-A
 11-B
 12-C
 13-D
 14-E
 15-F
Hexadecimal Number System
 E.g. Convert Hexadecimal Number AF to Decimal
 10  161+15160=175
 Convert the following Hexadecimal numbers to
decimal
 FF
 9F
 970
 23C
Hexadecimal Number System
 A common syntax used to represent hexadecimal values (in
code) is to place the symbols "0x" as a prefix to the value.
 Example:
 0x8D
 0x430
 The prefix 0x implies that the values are in hexadecimal
Hexadecimal Number System
 A second common syntax is to place a suffix of 'h' after a
value indicating that it is hexadecimal
 e.g. 88h, 30Fh
 Intel architectures do this in their assembly languages.
 This representation is actually more time consuming
(meaning the execution time of the code) to interpret, since
the entire number must be read before it can be decided
what number system is being used.
Conversion from decimal to any other base
examples:
36 (base 10) to base 2 (binary)
36/2 = 18 r=0 -- LSB
18/2 = 9 r=0
9/2 = 4 r=1
4/2 = 2 r=0
2/2 = 1 r=0
1/2 = 0 r=1 -- MSB
=100100
 I.e. Divide until you have a quotient of zero
 The binary value is made from the remainders with the last
remainder being the most significant bit and the first remainder
the least significant bit
Conversion from decimal to any other base
38 (base 10) to base 3
38/3 = 12 r=2 <-- ls digit
12/3 = 4 r=0
4/3 = 1 r=1
1/3 = 0 r=1 <-- ms digit
38 (base 10) == 1102 (base 3)
100 (base 10) to base 5
100/5 = 20 r=0
20/5 = 4 r=0
4/5 = 0 r=4
100 (base 10) = 400 (base 5)
Binary to Octal
 Group the bits into 3's starting at least significant bit
 If the number of bits is not evenly divisible by 3, then
add 0's at the most significant end
 Write 1 octal digit for each group
example:
100 010 111 (binary) = 4 2 7 (octal)
10 101 110 (binary) =2 5 6 (octal)
Binary to Hex
 Group the bits into groups of 4 bits starting at least
significant symbol
 If the number of bits is not evenly divisible by 4, then
add 0's at the most significant end
 Write 1 hex digit for each group
example:
1001 1110 0111 0000
9
E
7
0
1 1111 1010 0011
1 F
A 3
Hex to Binary
 Just write down the 4 bit binary code for each
hexadecimal digit
example:
3 9 c 8
0011 1001 1100 1000
Octal to Binary
 Just write down the 3 bit binary code for each octal
digit
example:
5 0 1
101 000 001
Hex to Octal
 Do it in 2 steps,
 hex  binary  octal
 E.g.
 FF  1111 1111  377
Octal to Hex
 Do it in 2 steps,
 octal  binary  hex
 E.g.
 605  110000101  185
Binary Fractions
 Example: Binary Fraction to Decimal
101.001 (binary)
122 + 0 21 + 120 + 02-1+ 02-2 + 12-3
4
+ 1
+ 1/8
5 1/8 = 5.125 (decimal)
Decimal to Binary Fraction
 Consider left and right of the decimal point separately.
 The number to the left can be converted to binary as before.
 Multiply fractional part by 2 and note the product
 If the product is greater than or equal to 1, then output a
1, else output a zero
 Multiply the fractional part of the product by 2 again
 Continue until the fractional part of the product is 0 or starts
repeating the value of the original fractional part of the
number being converted
 Extract the binary equivalent of the fractional part by reading
from the first value output
 Combine the two parts to form the binary equivalent
Examples
 7.375 = 111.011
 10.25 = 1010.01
 Convert the following
 5.1625 decimal to binary
 0.625 decimal to binary
 0.0010 binary to decimal
 101.11110 binary to decimal
Decimal Fractions to Octal
 7.325 = 7.246214
 The whole part is converted in the usual way
 For the fractional part .325 multiply by 8, if the answer is
greater than 1, output the whole part, else output a zero.
 Continue until the answer is zero or the original number
repeats, or you have an adequate number of values after the
decimal
Octal Fractions to Decimal
 7.246214=?
 The whole part is converted in the usual way
 Using the above example the fractional part is converted
as
 28-1 + 48-2 + 68-3 + 28-4 + 18-5 + 48-6=7.3248
 Note that because of having stopped after 6 digits in the
original conversion to octal (previous slide) some accuracy is
lost.
 The more digits after the decimal in the conversion, the more
accurate the result
Binary Arithmetic
ADDITION RULES
1+0=1
0+1=1
0+0=0
1 + 1 = 10
Addition Example
11101
+ 01011
= 101 00 0
SUBSTRACTION RULES
0-0=0
1-1=0
1-0=1
0 - 1 = 1 or
10 – 1 =1
Examples – subtraction
1101
1010
0011
11010
01001
10001
1010.00
1000.11
00001.01
11000
00001
Binary Multiplication
Rules
0x0=0
0x1=0
1x0=0
1x1=1
Examples
1101
X 1100
10011100
0100
X 0011
0001100
Binary Division




A /B = C
A – dividend
B – Divisor
C – Quotient
Uses a series of shift and subtract operations
Binary Division
Example
Binary Division
Exercise
 1111/1100=?
 1111/10=?
 1111.01/10=
Signed Numbers
 Computers only deal with 0s and 1s and so we need to
represent negative numbers differently from positive
numbers
 We use sign bit to denote this (0) for positive and (1) for –ve
 To write -5 and -1 using binary
 we add a sign bit to each one. Notice that we have padded '1'
with zeros so it will have four bits i.e. added an extra zero at
the beginning
 0101 (5)
 0001 (1)
 To make our binary numbers negative, we simply change our
sign bit from '0' to '1'.
 1101 (-5)
 1001 (-1)
Signed Numbers
4 bit Signed Number
 Be sure that you do not mistake the binary number 11012 for
the decimal number 1310.
 Since we are using 4-bit signed representation, we know the
leftmost bit is our sign and the remaining three bits are our
number.
Signed Numbers
 Consider the subtraction problem 3010 - 610.
 We can convert this problem to an equivalent addition
problem by changing 610 to -610.
 Now we can state the problem as; 3010 + (-610) = 2410.
 We can do something similar to this in binary by representing
negative numbers as complements.
 We will look at two ways of representing signed numbers
using complements:
1's complement
 Representing a signed number with 1's complement is done
by changing all the bits that are 1 to 0 and all the bits that are
0 to 1.
 Reversing the digits in this way is also called complementing a
number.
 For example representing -5 using 4 bits
 First, we write the positive value of the number in binary.
 0101 (+5) Next, we reverse each bit of the number so 1's
become 0's and 0's become 1's
 1010 (-5)
Binary
Decimal
0111
+7
0110
+6
0101
+5
0100
+4
0011
+3
0010
+2
0001
+1
0000
+0
1111
-0
1110
-1
1101
-2
1100
-3
1011
-4
1010
-5
1001
-6
1000
-7
Summary
Note
 Whenever we use 1's complement notation, the most
significant bit always tells us the sign of the number.
 The only exception to this rule is -0. In 1's complement, we
have two ways of representing the number zero.
 Notice also that the values +0 to +7 are the same as the
normal binary representation.
 Only the negative values must be complemented.
2’s complement
 Representing a signed number with 2's complement is done by
adding 1 to the 1's complement representation of the number.
 How can we represent the number -510 in 2's complement using
4-bits?
 First, we write the positive value of the number in binary.
 0101 (+5) Next, we reverse each bit to get the 1's complement.
 1010 Last, we add 1 to the number.
 1011 (-5)
 We notice that we only have one way to represent 0 in 2's
complement.
 This is an advantage because it simplifies representation of
signed numbers.
Binary Subtraction using 1’s complement
Example:
 7–1=6
0111
-0001
Convert 0001 to 1’s complement
1110
Add this value to positive 7 Binary
0111
+1110
= 10101
Remove the overflow bit at the start and it to the result
0101
+ 1
= 0110 – this is the answer
(6 decimal
Binary Subtraction using 1’s complement
Exercise
 Use one’s complement to perform the following calculations in
binary
 13-9
 -5-5
 -20-5
Binary Subtraction using 2’s complement
Example: 7-1
0111
-0001
Convert 0001 to 2’s complement 1111
Add the 7 binary to -1 (2’s complement)
0111
1111
10110
Discard the overflow bit
0110(this is the answer)
 In two’s complement, the overflow bit is discarded while in one’s
complement, it is added back to the result
Binary Subtraction using 2’s complement
Exercise
 Use 2’s complement to perform the following calculations in binary
 13-9
 -5-5
 -20-5
Data representation
 We have so far seen how to represent numbers
 They are represented by a sequence of binary digits
 How do we represent characters e.g. ‘a’, ‘b’, ‘1’, ‘2’, …
 Note that 1 and ‘1’ are different. One is a number the other is
a character
 The computer will represent them differently
 E.g. the character ‘1’ can be found in the string
‘ she was number 1’
 ’22’ is a sequence of two characters i.e. the character ‘2’ and
another character ‘2’.
 A sequence of characters is called a string
Data representation
 The set of characters recognized by computers are
 Set of alphabetic characters
 Set of digits 0-9
 Other symbols appearing on the keyboard e.g. space, ‘}’, ‘\’. ‘,’, …
Some other characters are recognized by the computer. There
effect can be seen but they may not visible.
 E.g. end of line character ‘\n’– generated when return is
pressed, ‘\a’ – bell , form feed character – used to move to
next page, escape character, delete character (back space).
 Some of these characters are used for control purposes and
are known as control characters
 Some cannot be generated by pressing a specific character
on the keyboard but have to be generated by pressing a
sequence of keyboard characters e.g. by pressing the ctrl
key and another character
Data representation
 The set of characters recognizable by a given computer is finite.
 E.g. some computers recognize 128 characters, others 256 and so
forth
 Example:
 If we are using 3 bits to represent each character, then we can
only have 8 unique characters i.e.
000, 001, 010, 011, 100, 101, 110, 111
For instance, this could be ‘a’ - 000, ‘b’ - 001, ‘c’- 010,
‘d’ - 011, ‘e’ - 100, ‘f’ – 101 , ‘g’- 110, ‘h’ – 111
Thus using only three bits, we cannot be able to represent
many characters
 A character code is a mapping between binary numbers of a fixed
length and the characters they represent
 When a character in the keyboard is pressed, a binary code
corresponding to the pressed character is generated by the
keyboard
Data representation
 When a character in the keyboard is pressed, a binary code
corresponding to the pressed character is generated by the keyboard
 For instance using the code we defined in the previous slide, when ‘a’
is pressed, 000 is generated.
 The character code is transmitted from the keyboard to the computer
a series of electrical signals that are either high or low.
 For ‘a’ above, 3 low pulses are transmitted
 Actually 5 pulses are transmitted
One high pulse called start bit,
The 3 low pulses for a
Then one low pulse called the stop bit
The start bit and the stop bit are always of opposite polarity
An equal amount of time is spent is transmitting each pulse.
Data representation
 An additional pulse called the parity bit may also be transmitted
Even Parity
 Where even parity is being used, the idea is to make the
number of 1 bits even
 So if the number of 1 bits in the code is odd, the parity bit
will have a value of 1 hence making then number of 1 bits
even
 If the number of bits with the value 1 in the code is even,
then the parity bit is set to zero, hence the number of bits
remains even
Odd Parity
 If the number of 1 bits in the code is even, the parity bit is
set to 1, else it is set to zero
 Using even parity ‘a’ 000 – in our code will have a zero in the parity
bit.
 Using odd parity ‘a’ 000 -- in our code will have a 1 in the parity bit
Data representation
 Using even parity, the following is the pulse generated by the
keyboard for the character ‘b’ 001, in our code
010011
0
1
0
0
1
 Note that the rightmost bit is transmitted first
1
Data representation
The ASCII Character Code
 The character code we created with three digits is insufficient
 Character codes have been defined already to represent the keyboard
characters
 Once such code is the ASCII (American standard code for Information
Interchange)
 ASCI is used to represent a set of 128 characters using 7 bits
 The first 32 (0-31) characters are known as control characters which
are used to send control information to the output device
 E.g.
 0000000 -0 is the null character
 0000111 – 7 is the bell character
 0001000 – 8 is the backspace character
 0001101 – 13 Carriage return – brings back the printing mechanism
to the left margin
 0001010 -10 – Line Feed – goes to the next line
Data representation
The ASCII Character Code
 For the printable characters
 0100001– 33 is !
 0110000 – 48 is zero
 0110000 – 49 is ‘1’
 0111001- 59 is 9
 65 is A
 90 is Z
 97 is a
 122 is z
 Note that most computers represent characters using 8 bits
 When 8 bits are used to represent an ASCII characters, the leftmost bit
is zero
Data representation
Extended ASCII Character Code
 Uses 8 bits
 Defined to include such characters as , etc. this extension enables
support of 256 characters
 Other Representation formats
 UTF- 8 (universal character set transformation format)
 Unicode (unique, unified, universal encoding )