Title: DATA REPRESENTATION
1DATA REPRESENTATION
- Digital computers store information in binary
- Binary allows two states
- On yes true 1
- Off no false 0
- Each digit in a binary number is called a bit
- 0 1 1 0
- bit
- 0ff 0n 0n 0ff
- Binary number system how numbers can be
processed in binary
2Binary Numbers
- Digits are 1 and 0
- 1 true
- 0 false
- MSB most significant bit
- LSB least significant bit
- Bit numbering
3Number system
- A number system of base (radix) r is a system
that uses distinct symbol for r digits. - Numbers are represented by a string of digit
symbols.
4Number system
- The base of a number is usually specified as a
subscript, e.g. - (01000011)2,
- (71203)8,
- (FF078ABC)16, ...etc.
- Or a letter indicating the base (d for decimal, b
for binary, o for octal and h for hexadecimal) is
appended to the number, e.g. - 01000011b,
- 71203o,
- FF078ABCh, ...etc.
5Number system
Attribute Binary Octal Decimal Hexadecimal
Base 2 8 10 16
Lowest Digit 0 0 0 0
Highest Digit 1 7 9 F
6Number system
- The value of a digit depends not only on its
value but also on its position within the number
? gives the power of the radix by which it is
multiplied.
7Positional values in the decimal number system
Decimal Digit 9 3 7
Position Name Hundreds Tens Ones
Positional Value 100 10 1
Positional Value as a power of the base (10) 102 101 100
8Positional values in the binary number system
Binary Digit 1 0 1
Position Name Fours Twos Ones
Positional Value 4 2 1
Positional Value as a power of the base (2) 22 21 20
9Example
- 2
1 0 -1
-2 - 379.25 310 710 910210510
- n
n-1 - (dndn-1...d0.f1f2fm)r dn ( r ) dn-1 ( r
) . -
- 0
-1 -2
-m - d0 ( r ) f1 ( r ) f2 ( r ) fm ( r
) - Converting to decimal
10Examples on converting from different bases to
Decimal
- Convert the following to Decimal
- (1001001)2
- (203)8
- (FA07)16
- Solution
- (1001001)2 1 021 022 123 024 025
126 73d - (203)8 3 081 282 131d
- (FA07)16 7 0161 10162 15163 64007d
11Conversion from decimal
- The conversion of a decimal integer into a base r
is done by - Whole numbers divisions by r
- Fractions repeated multiplication by r.
12Conversion from decimal
- To convert from decimal to any numbering system
with base r -
- The decimal number is divided by r,
- Keeping the remainder aside, the result is
further divided by r, and the new remainder is
kept aside, - The new result is divided again by r, and so on
till the result is less than r and this would be
the last remainder, - The remainders make up the equivalent base-r
number, with the last remainder being the
most-significant digit and the first remainder
being the least-significant digit.
13 Conversion to binaryWhole number
- Ex. (67)10 (?)2
- 2 67
- 2 33 rem 1 LSB
- 2 16 rem 1
- 2 8 rem 0
- 2 4 rem 0
67d 10000112 - 2 2 rem 0
- 2 1 rem 0
- 0 rem 1 MSB
14Fraction
- Repeat multiplication by 2 until the fractional
product is 0 - Ex. (0.3125)10 (?)2
-
Carry - 0.3125 2 0.625 0 MSB
- 0.625 2 1.25 1
(0.3125)10 (0.0101)2 - 0.25 2 0.5 0
- 0.5 2 1.00 1
LSB
15Conversion to base r
- (50) 10 (?)8
-
- 8 50
- 8 6 2 8
0 6 (50)10 (62)8 -
16Octal and Hexadecimal number systems
- Binary numbers are long.
- On average, it takes about 3.3 times as many
digits to represent a value in binary as it does
to represent the same value in decimal.
17Octal and Hexadecimal number systems
- If a base R1 is an integral power of
-
d - another base R2 i.e. R1 R2 ?
- Group of d digits in base R2 maps directly
into one digit in base R1 - And each digit in base R1 maps directly into d
digits in base R2.
18Octal number systems
- 3
- Base 8 2 ? d 3
- Digits 0 ? 7
- Binary to Octal conversion
- Group every 3 bits (starting from the right)
- Replace them by their corresponding octal digit
- Ex. ( 0 1 0 1 1 0 1 0 1 )2 ( 265 )8
- ( 1 0 0 0 1 )2 ( 21 )8
19Octal to binary conversion
- Replace each digit by its 3-bit binary
equivalent. - Ex. (476)8 (100111110)2
20Octal to binary conversion
21Binary Octal conversion table
- Binary Octal Binary Octal
- 000 0 001000
10 - 001 1 010000
20 - 010 2
- 011 3
- 100 4
- 101 5
- 110 6
- 111 7
22Hexadecimal number systems (Hex)
-
4 - Base 16 2 ? d 4
- Digits 0,1,2,..,9,A,B,C,D,E,F
- Where A 10, B 11, C 12, D 13, E 14, F
15
23Binary to Hex conversion
- Group each 4 bits and replace them by their
corresponding hex digit. - Ex.
- ( 1 0 1 1 0 1 0 1 )2 ( B5 )16
-
24Hex to binary conversion
- Replace each digit by their 4 bit binary
equivalent - Ex.
- ( 1D9C )16 (0001110110011100 )2
25Hex to binary conversion
26Binary Hexadecimal conversion table
- Binary Hex Binary
Hex - 0000 0 1000
8 - 0001 1 1001
9 - 0010 2 1010
A - 0011 3 1011
B - 0100 4 1100
C - 0101 5 1101
D - 0110 6 1110
E - 0111 7 1111
F - (00010100)2 ? (14)16 ? ( 20 )10
- (12.8)16 ? ( 00010010.1000)2 ? (18.5)10
27Binary Arithmetic operations
- Addition
- 00 0
- 01 1
- 10 1
- 11 10 (carry)
28Binary Arithmetic operations
- Subtraction
- 0 - 0 0
- 0 - 1 1 (after borrowing)
- 1 - 0 1
- 1 - 1 0
29Binary Arithmetic operations
- Multiplication
- 00 0
- 01 0
- 10 0
- 11 1
30ADDITION
- Like decimal numbers, two numbers can be added by
adding each pair of digits together with carry
propagation.
31Example
Carries
32 SUBTRACTION
- Two numbers can be subtracted by subtracting each
pair of digits together with borrowing, where
needed.
33SUBTRACTION
34Representation of signed binary numbers
- Given a fixed number of bit positions (n), we
-
n - can represent 2 patterns From (00..0)2 to
(11..1)2. - Using unsigned numbers we can represent
- n
- 0 ? 2 -1
- n
- Using signed numbers ? 2 positive negative
patterns
35Complementary notations
- The most commonly used way of representing signed
numbers. It greatly simplifies all arithmetic
operations. - Positive number is represented as it is (like an
unsigned positive number). - Negative numbers are represented by the
complement of unsigned number.
36Complementary notations
- The value used for complementation is a power of
the base or less one than a power of the base. - For binary numbers we have 2s complement and
1s complement. - For decimals, we have 10s complement and 9s
complement. - For hex. Numbers we have 16s complement and Fs
(15s) complement.
37Ones complement
- Is obtained by subtracting the number from
(11..1)2 (n 1s) ? reversing each bit changing
every 1 to 0 and every 0 to 1. - Ex. represent (14d), -(14d) in 8-bit binary
number - (14)10 ( 00001110)2 (00001110)1s
- (-14)10 -( 00001110)2 (11110001)1s
38For 8-bits number system
- Largest positive number 0 1111111 (127)10
- smallest negative number 1 0000000 - (127)10
- Zeroes 0 0000000
- 1
1111111 - Range
- m-1
m-1 - - (2 -1) to 2 -1 (m the
number of bits) - -(127)10 to (127)10
- The most significant bit represents the sign
- 0 ve 1 -ve.
392s complement
- Take the 1s complement and add 1 ? invert all
the bits and add 1. - Ex. 2s complement of (01110100)2
- 1s complement (10001011)2 add 1 ?
- 2s complement (10001100)2
402s complement
- 2s complement of (00000000)2
- 1s complement (11111111)2 add 1 ?
(100000000)2 - 2s complement of n-bit number include only the
rightmost n bits - 2s complement of (00000000)2
(00000000)2
412s complement
- - For 8-bit binary numbers 2s complement of (5)d
(00000101)2 - 1s complement (11111010)2 add 1?
(11111011)2 - 510 (-5)10 ? 000001012 111110112
1000000002 - Carry
- 2s complement of any N integer represents N
- N (-N) 0
422s complement
- To find the 2s complement of a binary number,
proceeding from right to left, leave all bits
unchanged up to and including the first 1.
Reverse all the remaining bits. - Range of signed value that can be represented in
2s complemented notation is - m-1 m-1
- - 2 to 2 -1
43For 8-bits number system
- Largest positive number
- 0 1111111 (127)10
- smallest negative number
- 1 0000000 - (128)10
- Zero 0 0000000
- Range -(128)10 to (127)10
- The most significant bit represents the sign
- 0 ve 1 -ve.
442s complement in hexadecimal
- Each group of four bits corresponds to a single
hexadecimal digit. - Subtract each hexadecimal digit from F and then
add 1 (equivalent of taking the 16s complement).
45Ex. 2s complement of (3A6E)16
- 1s complement FFFF
- - 3A6E
- C591
-
- 1
- 2s complement (C592)16
46Comparing 2s complement numbers
- Check the signs
- If they differ, they determine the order
- If they are the same, the order of the numbers is
the same as that of their representations.
47Ex. (01001100)2 and (10100101)2
- Different signs ? the 1st is ve
- the 2nd is ve
- ? 1st gt 2nd
48(10110010)2 and (10111001)2
- Both numbers are negative 1st lt 2nd
49Signed binary arithmetic
- Overflow
- Signed binary numbers are of a fixed range.
- If the result of addition/subtraction goes beyond
this range, overflow occurs. - Two conditions under which overflow can occur
are - (i) positive add positive gives
negative - (ii) negative add negative gives
positive
502s complement addition
- Algorithm
- Perform binary addition on the two numbers.
- Ignore the carry out of the MSB.
- Check for overflow Overflow occurs if the
carriers into and out of the MSB are different.
512s complement subtraction
- Algorithm for performing A - B
- A-B A (-B)
- Take 2s complement of B by inverting all the bits
and adding 1 - Add the 2s complement of B to A
522s complement addition/Subtraction
- Examples (4-bits system )
3 0011 4 0100
---- ------- 7 0111
---- -------
-2 1110 -6 1010
---- ------- -8
11000 ---- -------
532s complement addition/Subtraction
4 0100 -7 1001
---- ------- -3
1101 ---- -------
6 0110 -3 1101
---- ------- 3
10011 ---- -------
54Examples Overflow in 2s Addition/Subtraction4-bi
ts system
-3 1101 -6 1010
---- ------- -9 10111
---- -------
5 0101 6 0110
---- ------- 11
1011 ---- -------
7
-5
55Examples
- 1)Add 45 and 18 (use 8-bit 2s complement
representation - (45)d B00101101
- (18)d B00010010 ? (-18)d B11101110
- B00101101
- B11101110
- B100011011 ? 27
- Discard carry
56Examples
- 2)Add 38 and 57
- 38 B00100110
- 57B00111001 ? -57 B11000111
- B 00100110 B 11000111 B11101101 ?
-19
57Examples
- 3)Add 110 and 75
- 110 75 185
-
010011100 carries - 110 B01101110 B01101110
- 75B01001011 B01001011
-
B10111001 negative result ? overflow the result
is too big. 8 bits ? -128lt I lt 127
58Examples
- 4)Subtraction
- B01101011 -B00101001
- 2s complement of B00101001 is
- 11010111
- B01101011 B11010111 B101000010
59Examples
- Note In addition subtraction, if the operands
have different length, the shortest must be sign
extended. - Ex.
- 1) Add B1011 with B01101101
- B11111011 B01101101
- B101101000
60Examples
- 2) subtract XA3BC from X34A10BEE
- XA3BC sign extended ? FFFFA3BC
- 2s complement ? FFFFFFFF-FFFFA3BC 1
00005C44 - X34A10BEE X00005C44
- X34A16832
61CODING
- The most fundamental problem of coding is to get
as much information as possible in a fixed-length
representation. - In binary How many different binary numbers we
can write with n bits for a fixed - n
- n? ? 2 numbers.
62Binary Coded Decimal (BCD)
- Decimal numbers are more natural to humans.
Binary numbers are natural to computers. Quite
expensive to convert between the two. - If little calculation is involved, we can use
some coding schemes for decimal numbers.
63Binary Coded Decimal (BCD)
- One such scheme is BCD, also known as the 8421
code. - Represent each decimal digit as a 4-bit binary
code.
64Binary Coded Decimal (BCD)
- Some codes are unused, eg (1010)BCD, (1011)BCD,
, (1111)BCD. These codes are considered as
errors. - Easy to convert, but arithmetic operations are
more complicated.
65Binary Coded Decimal (BCD)
- Examples
- (234)10 (0010 0011 0100)BCD
- (7093)10 (0111 0000 1001 0011)BCD
- (1000 0110)BCD (86)10
- (1001 0100 0111 0010)BCD (9472)10
- Note
- BCD is not equivalent to binary.
- Example (234)10 (11101010)2
66Character data type
- Apart from numbers, computers also handle textual
data. - Internally all computers represent characters by
storing them as binary data - Character set frequently used includes
- alphabets A .. Z, and a .. z
- digits 0 .. 9
- special symbols , ., ,, _at_, ,
- non-printable line feed, bell,
67Character data type
- Usually, these characters can be represented
using 7 or 8 bits. - Two important issues concerning the character
codes - - The size i.e. the of bits used to
represent each character. - - The collating sequence.
68Character data type
- Standard encoding of character data is 8-bit.
- Each character was to be represented as a string
of 8-binary digits and there are a total - 8
- of 2 (256) unique characters available.
- widely used standardASCII (American Standard
Code for Information Interchange) 7-bit
the code fits into a byte, with the most
significant bit set to 0
69ASCII
- ASCII code is really 7-bit code ? 128 ASCII codes
- 2 calsses of characters
- - Printable characters ( code 2016 (408)?7E16
(1768) ) displayed on screen, or printed on
printer. They consist of - . Special characters e.g. , , , lt, gt,
- . Numbers 0 - 9
- . Uppercase characters A - Z
- . Lowercase characters a - z
70ASCII
- Control characters (0 - 1F16 (378) , 7F16 (1778)
). These do not cause a character to be printed
but instead initiate some action, control the
operation of devices (e.g. tab, LF, CR, ring
bell) or convey some status information.
71ASCII
- Example
- What is the internal ASCII code for the character
string 123? - 1 2 3
- 0618 0628 0638
- 3116 3216 3316
- 00110001 00110010 00110011
- ? 001100010011001000110011
72ASCII
- This example points up that the representation of
the character string 123 is quit different from
the representation of the signed integer 123. - Note The character representation takes 24 bits
(38). - The signed integer representation in 16-bits is
- (0000000001111011)2
73ASCII
74ASCII code table book page 32
CHAR HEX DEC CHAR HEX DEC
_at_ 40 64 ltCCgt 00 0
A 41 65 ltCCgt 01 1
B 42 66 ltCCgt 02 2