Download presentation
Presentation is loading. Please wait.
1
23/04/2019 Data Representation Conversion
2
Learning Objectives: Express numbers in binary, octal and hexadecimal.
23/04/2019 Learning Objectives: Express numbers in binary, octal and hexadecimal.
3
The Binary System 23/04/2019 Computers store information (data of all types – numbers, characters, sound, pictures, …) in Binary format i.e. base 2. i.e. 0 or 1 Used because computers can only store and understand 2 states: i.e. whether a circuit has current flowing or not / circuit is closed or open / voltage is high or low. 1
4
23/04/2019 Bits and Bytes A binary digit (1 or 0) is known as a ‘bit’, short for BInary digiT. In modern computers bits are grouped in 8 bit bytes. 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. Processors can have 8-, 16-, 32-(standard) or 64-(fast) bit word sizes (or more).
5
23/04/2019 Character set The symbols that a computer (software) can recognise which are represented by binary codes that the computer understands.
6
Character Representation
23/04/2019 Character Representation Over the years different computer designers have used different sets of binary codes for representing characters in a character set. This has led to great difficulty in transferring information from one computer to another. i.e. which binary code represents each character
7
ASCII (American Standard Code for Information Interchange)
23/04/2019 ASCII (American Standard Code for Information Interchange) Represents each character in a standard character set as a single byte binary code. The standard code form that most PCs use to allow for communication between systems. Usually uses a 7 bit binary code so can store 128 different characters and simple communications protocols. Sufficient for all characters on a standard keyboard plus control codes. Can be extended (extended ASCII) to use 8 bits (so can store 256 characters) to encode Latin language characters.
8
23/04/2019 ASCII code The first 32 ASCII codes are used for simple communications protocols, not characters. e.g. ACK – acknowledge and would be sent by a device to acknowledge receipt of data. – 2 – 1 ….. – A – B
9
Representing Characters and Numbers
23/04/2019 Representing Characters and Numbers e.g. If the ‘A’ key is pressed ‘ ’ is sent to the CPU. If the 1 key is pressed then ‘ ’ is sent to the CPU. If the user wants to print ‘123’ the codes for 1, 2 & 3 are sent to the printer.
10
Binary Arithmetic Rules
23/04/2019 Binary Arithmetic Rules 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 (carry 1) 1+1+1 = 1 (carry 1)
11
23/04/2019 Arithmetic ASCII coding is fine for input and output but useless for arithmetic: i.e. not 1 There is no easy way to perform calculations on the numbers stored in this way. Numbers which are to be used in calculations are therefore held in binary format.
12
Decimal or Denary system
23/04/2019 Decimal or Denary system 134 = Each column is worth 10X as much as the last i.e. base 10 (10 fingers!). 100 10 1 3 4
13
Binary system 23/04/2019 134 = Each column is worth 2X as much as the last i.e. base 2. 128 64 32 16 8 4 2 1 Most Significant Bit (MSB) Least Significant Bit (LSB) Increasing Bit Status
14
Binary – Decimal Spreadsheet Converter 1
23/04/2019 Binary – Decimal Spreadsheet Converter 1 Try using it to ‘play’ with binary numbers. On the website below the link to this presentation.
15
23/04/2019 Denary -> Binary e.g. 117 Always use the column headings for a byte (8 bits). 117 < 128 so put a 0 and repeat. 128 64 32 16 8 4 2 1 117 > 64 so put a 1. 128 64 32 16 8 4 2 1 = 53 , 53 > 32, so put a 1. 128 64 32 16 8 4 2 1 = 21 , 21 > 16, so put a 1. Continue this until: 128 64 32 16 8 4 2 1
16
Binary -> Denary e.g. 10110110 So 10110110 = 128 + 32 + 16 + 4 + 2
23/04/2019 Binary -> Denary e.g Put the column headings above the binary number and add up all the columns with a 1 in them. 128 64 32 16 8 4 2 1 So = = 182 (denary)
17
Questions 1. Convert the following binary numbers to decimal. 0011
23/04/2019 Questions 1. Convert the following binary numbers to decimal. 0011 3 0110 6 1010 10 65 69
18
23/04/2019 8 bit patterns Because in modern computers bits are grouped in 8 bit bytes numbers in binary format are usually written in 8 bit patterns even if there are unnecessary left leading 0’s. e.g. 11(binary) = 3 (decimal) But you will usually find it written as
19
Decimal -> Binary Questions
23/04/2019 Decimal -> Binary Questions 2. Convert the following decimal numbers to binary: 5 7 1 26 68 137
20
23/04/2019 Size of number Using only one byte to hold a number of places a restriction on the size of number the computer can hold. Therefore four or more consecutive bytes are commonly used to store numbers
21
Binary -> Decimal Questions
23/04/2019 Binary -> Decimal Questions 3. What is the largest decimal number that can be held in (hint: 2^no. of bits): 1 byte 255 (2^8 - 1) 2 bytes 65535 (2^16 - 1) 3 bytes (2^24 - 1) 4 bytes (2^32-1)
22
Limits of the AP CS Syllabus
23/04/2019 Limits of the AP CS Syllabus The information on slides 23 – 46 on Two’s Complement, Fixed Point & Floating Point Binary are not actually on your syllabus and can be skipped. All you really need to know is the statement on slide 35 that floating point numbers have accuracy/precision problems, so are not always accurate in calculations. I am including them for students who wish to have a fuller understanding as to why and how computers actually handle very large, negative and real fractional (decimal) numbers. Skip to slide 47 - Hexadecimal
23
Two’s Complement A way to represent negative numbers.
23/04/2019 Two’s Complement A way to represent negative numbers. Imagine a km clock in a car set at kilometres. If the car goes forward one km the reading becomes If the meter was turned back one km the reading would be km. This could be interpreted as ‘-1’ km.
24
Sign Bit Two’s Complement So: 0 0000011 = 3 0 0000010 = 2
23/04/2019 Two’s Complement So: = 3 = 2 = 1 = 0 = -1 = -2 = -3 Sign Bit
25
Binary – Decimal Spreadsheet Converter 2
23/04/2019 Binary – Decimal Spreadsheet Converter 2 Try using it to ‘play’ with two’s complement binary numbers.
26
Using Two’s Complement
23/04/2019 Using Two’s Complement Negative denary to binary Work out the binary number as if it were positive. From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. Flip means change 0 to 1 or 1 to 0. Negative binary to denary Reverse of above
27
-5 = 11111011 -5 Work out the binary number as if it were positive.
23/04/2019 -5 Work out the binary number as if it were positive. 5 = From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. -5 = Don’t flip the last 1. 1 1 1 1 1 1 1
28
23/04/2019 From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. Work out the decimal number as if it were positive. = 5 Add the minus sign. = -5
29
Alternative way of using Two’s Complement
23/04/2019 Alternative way of using Two’s Complement The MSB stays as a number, but is made negative. This means that the column headings are +117 does not need to use the MSB, so it stays as -117 = = ( ) Fitting this in the columns gives
30
23/04/2019 Two’s Complement Now addition and subtraction calculations give the correct results: = 3 = -3 = 0 <- carries Notes: The last ‘carry’ of 1 (carry in and out of the Most Significant Bit – MSB) has to be ignored unless an overflow has occurred (see next slide). The arithmetic works here, as all the bits, including the sign bit, in this method have a place value. There is only one representation for zero. = 0 = -128 (not 0 as in sign & magnitude)
31
Problem with Two’s Complement
23/04/2019 Problem with Two’s Complement +102 = +117 = = 219 but = -37 <- carries The original numbers are positive but the answer is negative! There has been an overflow from the positive part of the byte to the negative. To solve this error: If an "overflow" occurs add an extra bit and use this as the new sign bit. An overflow in a two's complement sum has occurred if: The sum of two positive numbers gives a negative result. The sum of two negative numbers gives a positive result. e.g. For the example above: = 219 (which is correct). New Sign bit (-256) old sign bit = +128
32
23/04/2019 Fixed Point Binary We can extend the binary system to represent real numbers by reserving some bits for the real or fractional part. Note that actually java uses 8 bytes (64 bits) to represent a double so the range is actually more than shown (however, the basic principles remain the same). 8 4 2 1 1/8 1/16 . 6.75 =
33
Fixed Point Binary - Decimal Converter
23/04/2019 Fixed Point Binary - Decimal Converter Try using it to ‘play’ with fixed point binary.
34
Fixed Point Binary Precision
23/04/2019 Fixed Point Binary Precision Also the fractional part can only hold 4 places, any places after the first 4 will be either rounded or truncated, so precision will be lost. This might first appear to be accurate enough for most purposes. However, each binary digit after the point is worth half of the last not 1/10 like in decimal values. Example shown on next slide.
35
Fixed Point Binary Precision
23/04/2019 Fixed Point Binary Precision 110.1 = 6.5 = 6.75 We have missed out 6.51 to 6.74! This means accuracy is poor. Skip to slide 46 - Hexadecimal
36
Floating Point (Fractional Real Numbers)
23/04/2019 Floating Point (Fractional Real Numbers) This increases the possible range of stored real numbers but not accuracy (this only achieved by using more bytes/bits): e.g. Standard form (also referred to as "Scientific notation"): 1,400,000,000,000 (Decimal) = 1.4*1012 1.4 = mantissa, 12 = exponent A number is therefore held in two parts: Mantissa Some websites also state that this is known more correctly as the Significand and that the Mantissa is the number before the decimal point. However, we will use the term Mantissa for the “whole digit string” e.g. 14 in the example above (at least up to now). Exponent Could be represented: if it was understood that the first part is the mantissa and second part is the exponent
37
Mantissa & Exponent 23/04/2019 For simplicity we will use 1 byte for each number, with 8 bits for the mantissa and 8 bits for the exponent. Actually java double uses 8 bytes, with 11 bits for exponent and 52 bits for mantissa. However, the same basic principles apply. Floating Point Video (from youtube) Try ‘playing’ with the Floating Point Binary - Decimal Converter.
38
23/04/2019 Mantissa Represents the magnitude of the number and is the fractional part of the representation. Place value of MSB is –1 and the other bits are ½, ¼.
39
23/04/2019 Exponent Represents the power of 2 by which the mantissa must be multiplied to give the original value.
40
Decimal Normalisation
23/04/2019 Decimal Normalisation 34,568,000 = x 104 = x 108 = x 107 The last way is more efficient and is the typical “correct” way to use scientific notation. This form is called the normalised form.
41
Floating Point Binary Normalisation
23/04/2019 Floating Point Binary Normalisation In binary the normalised form is used to maximise efficiency and to have only one way to represent a number. The mantissa is said to be normalised if the first two bits are different. For positive numbers, the first bit is always 0 and the second is always 1. For negative numbers the first bit is always 1 and the second is always 0.
42
Improving Accuracy of Binary Floating Point Numbers
23/04/2019 Improving Accuracy of Binary Floating Point Numbers If we want to improve accuracy we must use more bits for the mantissa by reducing the number of bits for the exponent. As more digits could be represented after the binary point. However the range would be decreased as the exponent could not be as large as before. So the power of two which the mantissa is multiplied by is decreased.
43
Representing Zero Using the Floating Point Binary - Decimal converter:
23/04/2019 Representing Zero Using the Floating Point Binary - Decimal converter: Try representing 0 as a non-normalised binary floating point number. Now try representing 0 as a normalised floating point number? Can you? Why?
44
23/04/2019 Representing Zero A normalised value must have the first two bits of the mantissa different. Therefore one must be a 1 which must represent either -1 or + ½ , but not zero. This is normally dealt with by making zero a special case: if every bit is zero (the sign bit being irrelevant), then the number is considered zero.
45
23/04/2019 Floating Point Binary You may now be thinking ‘If the range is so large why don’t we use floating point binary representation for all numbers (including integers)?’ However, it is more complicated to perform arithmetic on floating point numbers than integers and so they are slower to work with. Because of this floating point representation should only used with real fractional numbers or very large integers outside the range of ~ +2 billion to -2 billion (actually 2,147,483,648 to 2,147,483,647 which is the limit for typical 4 byte integer binary representation).
46
Limits of the AP CS Syllabus
23/04/2019 Limits of the AP CS Syllabus The information on the previous slides (23 – 45) on Two’s Complement, Fixed Point & Floating Point Binary are not actually on your syllabus and can be skipped. Slides 23 – 45. All you really need to know is the statement on slide 35 that floating point numbers have accuracy/precision problems, so are not always accurate in calculations. I have included them for students who wish to have a fuller understanding as to why and how computers actually handle very large, negative and real fractional (decimal) numbers.
47
Hexadecimal Counts in 16’s. Uses the digits 0 – 9 and letters A – F.
23/04/2019 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 symbols and we need 16!). We could invent 6 more symbols but we would have to learn them, so we use 6 that we already know, the 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) = 1111
48
Hexadecimal Denary / Decimal Binary Hexadecimal 1 00000001 2 00000010
23/04/2019 Hexadecimal Denary / Decimal Binary Hexadecimal 1 2 etc …. 9 10 A 11 B 15 F 16
49
Decimal -> Hexadecimal e.g. 75
23/04/2019 Decimal -> Hexadecimal e.g. 75 Use the column headings 1, 16, 256, … (16^n) 75 < 4096 & 75 < 256 so put a 0. 4096 256 16 1 75 > 16 & 75 / 16 = 4 r 11 11= B (hexadecimal) 4096 256 16 1 4 B So 75 = 4B (Hexadecimal)
50
Hexadecimal-Denary Converter
23/04/2019 Hexadecimal-Denary Converter Try using it to ‘play’ with hexadecimal numbers. host/0BxvAvCIUrln7bjJWaDAwZ0lBVFU/a/4%20Data%20Representation, %20Data%20Structures%20and%20Data%20Manipulation/4.1%20Data %20Representation/4.11%20Conversion/Hexadecimal- Denary_Converter.xls 23/04/2019 50
51
Denary -> Hexadecimal Question:
23/04/2019 Denary -> Hexadecimal Question: Translate 101 (denary) into hexadecimal. 65 Translate 64 (denary) into hexadecimal. 40
52
Hexadecimal -> Denary e.g. BD
23/04/2019 Hexadecimal -> Denary e.g. BD 4096 256 16 1 B D 11 13 B = 11 , D = 13 BD = (11 * 16) + 13 = = 189 ( in denary)
53
Hexadecimal -> Denary Question:
23/04/2019 Hexadecimal -> Denary Question: Translate 96 (hexadecimal) into denary. 150 Translate the 75 (hexadecimal) into denary. 117 Translate the 30 (hexadecimal) into denary. 48
54
Binary -> Hexadecimal
23/04/2019 Binary -> Hexadecimal Translate each group of 4 bits into denary and then into hexadecimal. e.g (binary) = (1001)(0010) = (9)(2) = 92 (hexadecimal) An alternative way to convert from denary to hexadecimal is to convert to binary and then do the above. Subsequently, vice versa for hexadecimal to denary.
55
23/04/2019 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.
56
23/04/2019 Questions: The ASCII code for the letter N is Show how to represent this in a) denary 78 b) hexadecimal = (0100) (1110) = (4 + 14) = 4E
57
23/04/2019 Octal Counts in 8’s.
58
Decimal -> Octal e.g. 117 So 117 = 165 (Octal)
23/04/2019 Decimal -> Octal e.g. 117 Use the column headings 1, 8, 64, 512 … (8^n) 117 < 512 so put a 0. 512 64 8 1 117 > 64 & 117 / 64 = 1 r 53 512 64 8 1 53 > 8 & 53 / 8 = 6 r 5 512 64 8 1 6 5 So 117 = 165 (Octal)
59
Decimal -> Octal Question:
23/04/2019 Decimal -> Octal Question: Translate 101 (denary) into octal. 145
60
Octal -> Denary e.g. 76 76 (octal) = (7 * 8) + 6 = 56 + 6
23/04/2019 Octal -> Denary e.g. 76 512 64 8 1 7 6 76 (octal) = (7 * 8) + 6 = = 62 ( in denary)
61
Octal -> Decimal Question:
23/04/2019 Octal -> Decimal Question: Translate 101 (octal) into denary. 65
62
Binary -> Octal 165 (octal) 01110101 (binary)
23/04/2019 Binary -> Octal (binary) Arrange in 3’s from the right. Add leading 0 Each group of three bits converted denary but the final result is octal. 165 (octal)
63
23/04/2019 Unicode 16 bit code so can store characters and codes and simple communications protocols. Used to allow coding of languages that do not use Western characters. Currently supports 24 language scripts.
64
Plenary Express the denary value 109 as
23/04/2019 Plenary Express the denary value 109 as A binary number using an 8-bit byte. An octal number. A hexadecimal number. A number in binary coded decimal (BCD).
65
23/04/2019 Plenary 155 6D
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.