20/11/20151 data representation conversion. 220/11/2015 learning objectives: express numbers in...
TRANSCRIPT
20/04/2320/04/23 11
Data Representation
Conversion
2220/04/2320/04/23
Learning Objectives:Learning Objectives:
Express numbers in binary, binary coded decimal (BCD), octal and hexadecimal.
3320/04/2320/04/23
The Binary System
Computers store information (data of all types – numbers, characters, sound, pictures, …) in Binary format i.e. base 2. i.e. 0 or 1i.e. 0 or 1
Used because computers can only store Used because computers can only store and understand 2 states:and understand 2 states: i.e. whether a circuit has current flowing or not i.e. whether a circuit has current flowing or not
/ circuit is closed or open / voltage is high or / circuit is closed or open / voltage is high or low.low.
1 0
4420/04/2320/04/23
Bits and BytesBits and Bytes
A binary digit (1 or 0) is known as a ‘A binary digit (1 or 0) is known as a ‘bitbit’, short ’, short for for BIBInary diginary digiTT..In modern computers bits are grouped in 8 bit In modern computers bits are grouped in 8 bit bytesbytes..A A Nibble is 4 bits (half a byte).A Word is the number of bits that the CPU can process simultaneously.
Determines the speed of the computer.Determines the speed of the computer. Processors can have 8-, 16-, 32-(standard) or 64-Processors can have 8-, 16-, 32-(standard) or 64-
(fast) bit word sizes (or more).(fast) bit word sizes (or more).
5520/04/2320/04/23
Character set Character set
The symbols that a computer (software) The symbols that a computer (software) can recognise which are represented by can recognise which are represented by binary codes that the computer binary codes that the computer understands.understands.
6620/04/2320/04/23
Character RepresentationCharacter Representation
Over the years different computer Over the years different computer designers have used different designers have used different sets of sets of binary codesbinary codes for representing characters for representing characters in a character set.in a character set.This has led to great difficulty in This has led to great difficulty in transferring information from one computer transferring information from one computer to another.to another. i.e. which binary code represents each i.e. which binary code represents each
charactercharacter
7720/04/2320/04/23
ASCII (AASCII (American merican SStandard tandard CCode ode for for IInformation nformation IInterchange)nterchange)
Represents each character in a standard Represents each character in a standard character set as a single byte binary code.character set as a single byte binary code.The standard code form that most PCs use to The standard code form that most PCs use to allow for communication between systems. allow for communication between systems. Usually uses a 7 bit binary code so can store Usually uses a 7 bit binary code so can store 128 different characters and simple 128 different characters and simple communications protocols.communications protocols.Sufficient for all characters on a standard Sufficient for all characters on a standard keyboard plus control codes.keyboard plus control codes.
Can be extended (extended ASCII) to use 8 bits (so Can be extended (extended ASCII) to use 8 bits (so can store 256 characters) to encode Latin language can store 256 characters) to encode Latin language characters.characters.
8820/04/2320/04/23
ASCIIASCII code code
The first 32 ASCII codes are used for simple The first 32 ASCII codes are used for simple communications protocols, not characters.communications protocols, not characters. e.g. ACK – acknowledge and would be sent by a e.g. ACK – acknowledge and would be sent by a
device to acknowledge receipt of data.device to acknowledge receipt of data. 0110010 – 2 0110010 – 2 0110001 – 10110001 – 1 …….... 1000001 – A1000001 – A 1000010 – B1000010 – B
9920/04/2320/04/23
Representing Characters and Representing Characters and NumbersNumbers
e.g. If the ‘A’ key is pressed ‘1000001’ is e.g. If the ‘A’ key is pressed ‘1000001’ is sent to the CPU.sent to the CPU.
If the 1 key is pressed then ‘0110001’ is If the 1 key is pressed then ‘0110001’ is sent to the CPU.sent to the CPU.
If the user wants to print ‘123’ the codes If the user wants to print ‘123’ the codes for 1, 2 & 3 are sent to the printer.for 1, 2 & 3 are sent to the printer.
101020/04/2320/04/23
Binary Arithmetic RulesBinary Arithmetic Rules
0 + 0 = 00 + 0 = 0
0 + 1 = 10 + 1 = 1
1 + 0 = 11 + 0 = 1
1 + 1 = 0 (carry 1)1 + 1 = 0 (carry 1)
1+1+1 = 1 (carry 1)1+1+1 = 1 (carry 1)
111120/04/2320/04/23
ArithmeticArithmetic
ASCII coding is fine for input and output but ASCII coding is fine for input and output but useless for arithmetic:useless for arithmetic: 2 2 0110010 0110010 -1-1 -- 0110001 0110001 11 0000000 i.e. not 1 0000000 i.e. not 1
There is no easy way to perform calculations on There is no easy way to perform calculations on the numbers stored in this way.the numbers stored in this way.
Numbers which are to be used in calculations Numbers which are to be used in calculations are therefore held in binary format.are therefore held in binary format.
121220/04/2320/04/23
Decimal or Denary systemDecimal or Denary system
134 = 100 + 30 + 4134 = 100 + 30 + 4
Each column is worth 10X as much Each column is worth 10X as much as the last i.e. base 10 (10 fingers!).as the last i.e. base 10 (10 fingers!).
100100 1010 11
11 33 44
131320/04/2320/04/23
Binary systemBinary system134 = 128 + 4 + 2134 = 128 + 4 + 2
Each column is worth 2X as much as the last Each column is worth 2X as much as the last i.e. base 2.i.e. base 2.
128128 6464 3232 1616 88 44 22 11
11 00 00 00 00 11 11 00
Most Significant Bit(MSB)
Most Significant Bit(MSB)
Least Significant Bit (LSB)
Least Significant Bit (LSB)
Increasing Bit StatusIncreasing Bit Status
141420/04/2320/04/23
Binary – Decimal Spreadsheet Binary – Decimal Spreadsheet Converter 1Converter 1
Try using it to ‘play’ with binary numbers.Try using it to ‘play’ with binary numbers. https://https://
4565e4f1bb6fcb191b6a80b2e8cd1502a3f5fc3b.googledrive.com4565e4f1bb6fcb191b6a80b2e8cd1502a3f5fc3b.googledrive.com/host/0BxvAvCIUrln7bjJWaDAwZ0lBVFU/a/4%20Data/host/0BxvAvCIUrln7bjJWaDAwZ0lBVFU/a/4%20Data%20Representation,%20Data%20Structures%20and%20Data%20Representation,%20Data%20Structures%20and%20Data%20Manipulation/4.1%20Data%20Representation/%20Manipulation/4.1%20Data%20Representation/4.11%20Conversion/Binary-Denary_Converter1.xls4.11%20Conversion/Binary-Denary_Converter1.xls
Denary -> Binary e.g. 117Denary -> Binary e.g. 1171.1. Always use the column headings for a byte (8 bits).Always use the column headings for a byte (8 bits).2.2. 117 < 128 so put a 0 and repeat.117 < 128 so put a 0 and repeat.
128128 6464 3232 1616 88 44 22 11
00
3.3. 117 > 64 so put a 1.117 > 64 so put a 1.128128 6464 3232 1616 88 44 22 11
00 11
4.4. 117 - 64 = 53 , 53 > 32, so put a 1.117 - 64 = 53 , 53 > 32, so put a 1.128128 6464 3232 1616 88 44 22 11
00 11 11 11 00 11 00 11
5.5. 53 - 32 = 21 , 21 > 16, so put a 1.53 - 32 = 21 , 21 > 16, so put a 1.6.6. Continue this until:Continue this until:
128128 6464 3232 1616 88 44 22 11
00 11 11 11 00 11 00 11
BinaryBinary -> Denary e.g. -> Denary e.g. 1011011010110110
128128 6464 3232 1616 88 44 22 11
11 00 11 11 00 11 11 00
So 10110110 = 128 + 32 + 16 + 4 + 2 So 10110110 = 128 + 32 + 16 + 4 + 2
= 182 = 182 (denary)(denary)
Put the column headings above the binary Put the column headings above the binary number and add up all the columns with a 1 in number and add up all the columns with a 1 in them. them.
171720/04/2320/04/23
QuestionsQuestions
1. Convert the following binary numbers to 1. Convert the following binary numbers to decimal.decimal. 00110011
33 01100110
66 10101010
1010 0100000101000001
6565 0100010101000101
6969
181820/04/2320/04/23
8 bit patterns8 bit patterns
Because in modern computers bits are Because in modern computers bits are grouped in 8 bit grouped in 8 bit bytes bytes numbers in binary numbers in binary format are usually written in 8 bit patterns format are usually written in 8 bit patterns even if there are unnecessary left leading even if there are unnecessary left leading 0’s.0’s. e.g. 11(binary) = 3 (decimal)e.g. 11(binary) = 3 (decimal) But you will usually find it written as But you will usually find it written as 0000001100000011
191920/04/2320/04/23
Decimal -> Binary QuestionsDecimal -> Binary Questions
2. Convert the following decimal numbers to binary:2. Convert the following decimal numbers to binary: 55
0000010100000101 77
0000011100000111 11
0000000100000001 2626
0001101000011010 6868
0100010001000100 137137
1000100110001001
202020/04/2320/04/23
Size of numberSize of number
Using only one byte to hold a number of Using only one byte to hold a number of places a restriction on the size of number places a restriction on the size of number the computer can hold. the computer can hold.
Therefore four or more consecutive bytes Therefore four or more consecutive bytes are commonly used to store numbersare commonly used to store numbers
212120/04/2320/04/23
Binary -> Decimal QuestionsBinary -> Decimal Questions
3. What is the largest decimal number that 3. What is the largest decimal number that can be held in (hint: 2^no. of bits):can be held in (hint: 2^no. of bits): 1 byte1 byte
255 (2^8 - 1)255 (2^8 - 1) 2 bytes2 bytes
65535 (2^16 - 1)65535 (2^16 - 1) 3 bytes3 bytes
16777215 (2^24 - 1)16777215 (2^24 - 1) 4 bytes4 bytes
4294967295 (2^32-1)4294967295 (2^32-1)
222220/04/2320/04/23
Hexadecimal
Counts in 16’s.Uses the digits 0 – 9 and letters A – F.
We need symbols going further than 0 to 9 (only 10 We need symbols going further than 0 to 9 (only 10 symbols and we need 16!).symbols and we need 16!).
We could invent 6 more symbols but we would have We could invent 6 more symbols but we would have to learn them, so we use 6 that we already know, the to learn them, so we use 6 that we already know, the letters A to F. letters A to F.
Each digit is worth 16X as much as the one to the right.Each hex bit = 4 binary bits.
e.g. F (decimal 15) = 1111e.g. F (decimal 15) = 1111
232320/04/2320/04/23
HexadecimalDenary / DecimalDenary / Decimal BinaryBinary HexadecimalHexadecimal
11 0000000100000001 11
22 0000001000000010 22
etc ….etc ….
99 0000100100001001 99
1010 0000101000001010 AA
1111 0000101100001011 BB
etc ….etc ….
1515 0000111100001111 FF
1616 0001000000010000 1010
Decimal -> Hexadecimal e.g. 75Decimal -> Hexadecimal e.g. 75
1.1. Use the column headings 1, 16, 256, … (16^n)Use the column headings 1, 16, 256, … (16^n)2.2. 75 < 4096 & 75 < 256 so put a 0.75 < 4096 & 75 < 256 so put a 0.
40964096 256256 1616 11
00 00
3.3. 75 > 16 & 75 / 16 = 4 r 1175 > 16 & 75 / 16 = 4 r 114.4. 11= B 11= B (hexadecimal)(hexadecimal)
40964096 256256 1616 11
00 00 44 BB
So 75 =So 75 = 4B (Hexadecimal) 4B (Hexadecimal)
252520/04/2320/04/23
Hexadecimal-Denary ConverterHexadecimal-Denary Converter
Try using it to ‘play’ with hexadecimal Try using it to ‘play’ with hexadecimal numbers.numbers.
https://https://4565e4f1bb6fcb191b6a80b2e8cd1502a3f5fc3b.googledrive.com4565e4f1bb6fcb191b6a80b2e8cd1502a3f5fc3b.googledrive.com/host/0BxvAvCIUrln7bjJWaDAwZ0lBVFU/a/4%20Data/host/0BxvAvCIUrln7bjJWaDAwZ0lBVFU/a/4%20Data%20Representation,%20Data%20Structures%20and%20Data%20Representation,%20Data%20Structures%20and%20Data%20Manipulation/4.1%20Data%20Representation/%20Manipulation/4.1%20Data%20Representation/4.11%20Conversion/Hexadecimal-Denary_Converter.xls4.11%20Conversion/Hexadecimal-Denary_Converter.xls
262620/04/2320/04/23
Denary -> Hexadecimal Question:Denary -> Hexadecimal Question:
Translate 101 Translate 101 (denary)(denary) into hexadecimal into hexadecimal.. 65 65
Translate 64 Translate 64 (denary)(denary) into hexadecimal into hexadecimal.. 4040
272720/04/2320/04/23
Hexadecimal -> Denary e.g. BDHexadecimal -> Denary e.g. BD
B = 11 , D = 13B = 11 , D = 13
BD = (11 * 16) + 13BD = (11 * 16) + 13
= 176 + 13= 176 + 13
= 189 ( in denary)= 189 ( in denary)
40964096 256256 1616 11
00 00 BB DD
00 00 1111 1313
282820/04/2320/04/23
Hexadecimal -> Denary Question:Hexadecimal -> Denary Question:
Translate 96 Translate 96 (hexadecimal)(hexadecimal) into denary. into denary. 150150
Translate the 75 Translate the 75 (hexadecimal)(hexadecimal) into into denary.denary. 117117
Translate the 30 Translate the 30 (hexadecimal)(hexadecimal) into into denary.denary. 4848
292920/04/2320/04/23
Binary -> HexadecimalBinary -> Hexadecimal
Translate each group of 4 bits into denary and Translate each group of 4 bits into denary and then into hexadecimal.then into hexadecimal.
e.g. 10010010 (binary) = (1001)(0010) = (9)(2) = e.g. 10010010 (binary) = (1001)(0010) = (9)(2) = 92 (hexadecimal)92 (hexadecimal)
An alternative way to convert from denary to An alternative way to convert from denary to hexadecimal is to convert to binary and then do hexadecimal is to convert to binary and then do the above.the above. Subsequently, vice versa for hexadecimal to denary.Subsequently, vice versa for hexadecimal to denary.
303020/04/2320/04/23
Hexadecimal
In order to ease the task of examining the contents of memory or a computer file, binary numbers are commonly put into groups of 4 bits and displayed in the form of hexadecimal numbers, base 16.
Used as a shorthand for binary.
313120/04/2320/04/23
Questions:Questions:
The ASCII code for the letter N is 0100 The ASCII code for the letter N is 0100 1110. Show how to represent this in1110. Show how to represent this in
a) denarya) denary
7878
b) hexadecimalb) hexadecimal
= (0100) (1110) = (4 + 14) = 4E= (0100) (1110) = (4 + 14) = 4E
323220/04/2320/04/23
OctalOctal
Counts in 8’s.Counts in 8’s.
Decimal -> Octal e.g. 117Decimal -> Octal e.g. 117
1.1. Use the column headings 1, 8, 64, 512 … (8^n)Use the column headings 1, 8, 64, 512 … (8^n)2.2. 117 < 512 so put a 0.117 < 512 so put a 0.
512512 6464 88 11
00
3.3. 117 > 64 & 117 / 64 = 1 r 53117 > 64 & 117 / 64 = 1 r 53
4.4. 53 > 8 & 53 / 8 = 6 r 553 > 8 & 53 / 8 = 6 r 5
So 117 =So 117 = 165 (Octal) 165 (Octal)
512512 6464 88 11
00 11 00 00
512512 6464 88 11
00 11 66 55
343420/04/2320/04/23
Decimal -> Octal Question:Decimal -> Octal Question:
Translate 101 Translate 101 (denary)(denary) into into octal.octal. 145145
353520/04/2320/04/23
Octal -> Denary e.g. 76Octal -> Denary e.g. 76
76 76 (octal)(octal) = (7 * 8) + 6 = (7 * 8) + 6
= 56 + 6= 56 + 6
= 62 ( in denary)= 62 ( in denary)
512512 6464 88 11
00 00 77 66
363620/04/2320/04/23
Octal -> Decimal Question:Octal -> Decimal Question:
Translate 101 Translate 101 (octal)(octal) into into denary.denary. 6565
373720/04/2320/04/23
Binary -> OctalBinary -> Octal
01110101 01110101 (binary)(binary)
1.1. Arrange in 3’s from the right.Arrange in 3’s from the right. 01 110 10101 110 101
2.2. Add leading 0Add leading 0 001 110 101001 110 101
3.3. Each group of three bits converted Each group of three bits converted denary but the final result is octal.denary but the final result is octal.
1 6 51 6 5 165 165 (octal)(octal)
383820/04/2320/04/23
BCD (BCD (BBinary inary CCoded oded DDecimal)ecimal)
Represents numbers only by representing Represents numbers only by representing each decimal digit by a 4 bit binary code.each decimal digit by a 4 bit binary code.
DecimalDecimal BinaryBinary
00 00000000
11 00010001
22 00100010
etc....etc....
99 10011001
e.g. 19 is shown by:e.g. 19 is shown by: 1 91 9 0001 10010001 1001
393920/04/2320/04/23
Denary -> BCD QuestionDenary -> BCD Question
Convert Convert 398602 398602 (denary) into BCD.(denary) into BCD. 001110011000011000000010001110011000011000000010
404020/04/2320/04/23
BCD -> Denary e.g. 001001110110BCD -> Denary e.g. 001001110110
Split into groups of 4.Split into groups of 4. (0010) (0111) (0110)(0010) (0111) (0110)
Translate each group of 4 into decimal in Translate each group of 4 into decimal in the same fashion as binary -> decimal.the same fashion as binary -> decimal. 2 7 62 7 6
So 001001110110 So 001001110110 = 276 (denary)= 276 (denary)
414120/04/2320/04/23
BCD -> Denary QuestionBCD -> Denary Question
Convert Convert 100000110101 (BCD) into 100000110101 (BCD) into denarydenary.. 835 835
424220/04/2320/04/23
BCD (BCD (BBinary inary CCoded oded DDecimal)ecimal)
Advantages:Advantages: Easy to convert from BCD to decimal and vice Easy to convert from BCD to decimal and vice
versa:versa:as a BCD number is split into groups of four bits as a BCD number is split into groups of four bits and each group converted directly to the and each group converted directly to the corresponding decimal digitcorresponding decimal digit
Used in some pocket calculatorsUsed in some pocket calculators
434320/04/2320/04/23
Fixed Point BinaryFixed Point Binary
A number with a decimal point is known (strangely!) A number with a decimal point is known (strangely!) as a as a realreal number as opposed to an integer which is a number as opposed to an integer which is a whole number.whole number.
We can extend the binary system to represent real We can extend the binary system to represent real numbers by reserving some bits for the real or numbers by reserving some bits for the real or fractional part.fractional part.
88 44 22 11 ½½ ¼¼ 1/81/8 1/161/16
00 11 11 00 11 11 00 00
6.75 = 0110.11006.75 = 0110.1100..
444420/04/2320/04/23
Fixed Point Binary PrecisionFixed Point Binary Precision
110.1 = 6.5110.1 = 6.5
110.11 = 6.75110.11 = 6.75
We have missed out 6.51 to 6.74!We have missed out 6.51 to 6.74!
This means accuracy is poor.This means accuracy is poor.
454520/04/2320/04/23
BCD (BCD (BBinary inary CCoded oded DDecimal)ecimal)
Advantages:Advantages: When storing fractional numbers in BCD no When storing fractional numbers in BCD no
‘rounding’ occurs:‘rounding’ occurs:As each digit is encoded separately so as many As each digit is encoded separately so as many bits as necessary are used to represent the bits as necessary are used to represent the complete numbercomplete number
Used in business applications where every Used in business applications where every significant digit has to be retained in a result.significant digit has to be retained in a result.
464620/04/2320/04/23
BCD (BCD (BBinary inary CCoded oded DDecimal)ecimal)
DisadvantageDisadvantage More bits are required to store each number More bits are required to store each number
than pure binary e.g.than pure binary e.g.19 in BCD = 0001 1001 (8 bits)19 in BCD = 0001 1001 (8 bits)19 in normal binary format = 10011 (5 bits)19 in normal binary format = 10011 (5 bits)
Difficult to calculate with:Difficult to calculate with:e.g. 1 0000 0001e.g. 1 0000 0001
+ 19+ 19 0001 10010001 1001 20 0001 101020 0001 1010
‘10’ is wrong‘10’ is wrong invalidinvalid
474720/04/2320/04/23
BCD (BCD (BBinary inary CCoded oded DDecimal)ecimal)
Only the first ten out of 16 combinations of 4 Only the first ten out of 16 combinations of 4 digits are used to encode the decimal digits ‘0’ to digits are used to encode the decimal digits ‘0’ to ‘9’‘9’Therefore whenever the sum of two binary digits Therefore whenever the sum of two binary digits is >9, 6 has to be added to skip over the 6 is >9, 6 has to be added to skip over the 6 unused codes.unused codes.e.g. 1 0000 0001e.g. 1 0000 0001
+ 19+ 19 + +0001 10010001 1001 20 0001 101020 0001 1010
+ 0110+ 0110 0010 0000 i.e. 20 which is correct0010 0000 i.e. 20 which is correct
484820/04/2320/04/23
UnicodeUnicode
16 bit code so can store 65536 characters 16 bit code so can store 65536 characters and codes and simple communications and codes and simple communications protocols.protocols.
Used to allow coding of languages that do Used to allow coding of languages that do not use Western characters.not use Western characters.
Currently supports 24 language scripts.Currently supports 24 language scripts.
494920/04/2320/04/23
PlenaryPlenary
Express the denary value 109 asExpress the denary value 109 as A binary number using an 8-bit byte.A binary number using an 8-bit byte. An octal number.An octal number. A hexadecimal number. A hexadecimal number. A number in binary coded decimal (BCD).A number in binary coded decimal (BCD).
505020/04/2320/04/23
PlenaryPlenary
0110110101101101
155155
6D6D
0001 0000 10010001 0000 1001