23/04/2019 Data Representation Conversion.

Slides:



Advertisements
Similar presentations
Digital Fundamentals Floyd Chapter 2 Tenth Edition
Advertisements

Assembly Language and Computer Architecture Using C++ and Java
Assembly Language and Computer Architecture Using C++ and Java
Binary Number Systems.
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Fundamentals Tenth Edition Floyd.
IT253: Computer Organization
EX_01.1/46 Numeric Systems. EX_01.2/46 Overview Numeric systems – general, Binary numbers, Octal numbers, Hexadecimal system, Data units, ASCII code,
Computing Theory – F453 Number Systems. Data in a computer needs to be represented in a format the computer understands. This does not necessarily mean.
1 Data Representation Characters, Integers and Real Numbers Binary Number System Octal Number System Hexadecimal Number System Powered by DeSiaMore.
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Data Representation Conversion 24/04/2017.
Data Representation, Number Systems and Base Conversions
Fractions in Binary.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
Number Systems Denary Base 10 Binary Base 2 Hexadecimal Base 16
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
MECH1500 Chapter 3.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
09/06/ Data Representation ASCII, Binary Denary Conversion, Integer & Boolean data types.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Chapter 9 Computer Arithmetic
William Stallings Computer Organization and Architecture 8th Edition
Lesson Objectives Aims You should know about: Binary numbers ‘n’ that.
Floating Point Numbers
CS2100 Computer Organisation
Data Representation COE 308 Computer Architecture
Programming and Data Structure
NUMBER SYSTEMS.
Data Representation ICS 233
Lec 3: Data Representation
CS2100 Computer Organisation
Data Representation.
Lecture No. 4 Number Systems
Number Representation
3.1 Denary, Binary and Hexadecimal Number Systems
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
Data Representation Binary Numbers Binary Addition
Lesson Objectives Aims
CHAPTER 1 : INTRODUCTION
Chapter 3 Data Storage.
Binary Arithmetic Binary arithmetic is essential in all digital computers and in many other types of digital systems. Addition, Subtraction, Multiplication,
Introduction The term digital is derived from the way computers perform operation, by counting digits. Application of digital technology: television, communication.
A Level Computing Component 2
William Stallings Computer Organization and Architecture 7th Edition
IT 0213: INTRODUCTION TO COMPUTER ARCHITECTURE
CS1010 Programming Methodology
Data Structures Mohammed Thajeel To the second year students
BEE1244 Digital System and Electronics BEE1244 Digital System and Electronic Chapter 2 Number Systems.
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSC 370 (Blum)
Chapter 2 Bits, Data Types & Operations Integer Representation
Data Representation Data Types Complements Fixed Point Representation
Digital Logic & Design Lecture 03.
Fundamentals of Data Representation
Digital Logic & Design Lecture 02.
Data Representation Conversion 05/12/2018.
ECEG-3202 Computer Architecture and Organization
4.1 Strings ASCII & Processing Strings with the Functions
C1 Number systems.
Storing Negative Integers
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter 8 Computer Arithmetic
A-level Computer Science
Data Binary Arithmetic.
Chapter3 Fixed Point Representation
Data Representation COE 308 Computer Architecture
Two’s Complement & Binary Arithmetic
Presentation transcript:

23/04/2019 Data Representation Conversion

Learning Objectives: Express numbers in binary, octal and hexadecimal. 23/04/2019 Learning Objectives: Express numbers in binary, octal and hexadecimal.

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

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).

23/04/2019 Character set The symbols that a computer (software) can recognise which are represented by binary codes that the computer understands.

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

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.

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. 0110010 – 2 0110001 – 1 ….. 1000001 – A 1000010 – B

Representing Characters and Numbers 23/04/2019 Representing Characters and Numbers e.g. If the ‘A’ key is pressed ‘1000001’ is sent to the CPU. If the 1 key is pressed then ‘0110001’ is sent to the CPU. If the user wants to print ‘123’ the codes for 1, 2 & 3 are sent to the printer.

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)

23/04/2019 Arithmetic ASCII coding is fine for input and output but useless for arithmetic: 2 0110010 -1 - 0110001 1 0000000 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.

Decimal or Denary system 23/04/2019 Decimal or Denary system 134 = 100 + 30 + 4 Each column is worth 10X as much as the last i.e. base 10 (10 fingers!). 100 10 1 3 4

Binary system 23/04/2019 134 = 128 + 4 + 2 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

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.

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 117 - 64 = 53 , 53 > 32, so put a 1. 128 64 32 16 8 4 2 1 53 - 32 = 21 , 21 > 16, so put a 1. Continue this until: 128 64 32 16 8 4 2 1

Binary -> Denary e.g. 10110110 So 10110110 = 128 + 32 + 16 + 4 + 2 23/04/2019 Binary -> Denary e.g. 10110110 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 10110110 = 128 + 32 + 16 + 4 + 2 = 182 (denary)

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 01000001 65 01000101 69

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 00000011

Decimal -> Binary Questions 23/04/2019 Decimal -> Binary Questions 2. Convert the following decimal numbers to binary: 5 00000101 7 00000111 1 00000001 26 00011010 68 01000100 137 10001001

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

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 16777215 (2^24 - 1) 4 bytes 4294967295 (2^32-1)

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

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 00000000 kilometres. If the car goes forward one km the reading becomes 00000001. If the meter was turned back one km the reading would be 99999999 km. This could be interpreted as ‘-1’ km.

Sign Bit Two’s Complement So: 0 0000011 = 3 0 0000010 = 2 23/04/2019 Two’s Complement So: 0 0000011 = 3 0 0000010 = 2 0 0000001 = 1 0 0000000 = 0 1 1111111 = -1 1 1111110 = -2 1 1111101 = -3 Sign Bit

Binary – Decimal Spreadsheet Converter 2 23/04/2019 Binary – Decimal Spreadsheet Converter 2 Try using it to ‘play’ with two’s complement binary numbers.

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

-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 = 0 0000101 From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. -5 = 11111011 Don’t flip the last 1. 1 1 1 1 1 1 1

23/04/2019 11111011 From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. 00000101 Work out the decimal number as if it were positive. 0 0000101 = 5 Add the minus sign. 11111011 = -5

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 -128 64 32 16 8 4 2 1 +117 does not need to use the MSB, so it stays as 01110101. -117 = -128 + 11 = -128 + (8 + 2 + 1) Fitting this in the columns gives 10001011

23/04/2019 Two’s Complement Now addition and subtraction calculations give the correct results: 0 0000011 = 3 + 1 1111101 = -3 10 0000000 = 0 11 111111 <- 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. 00000000 = 0 10000000 = -128 (not 0 as in sign & magnitude)

Problem with Two’s Complement 23/04/2019 Problem with Two’s Complement +102 = 01100110 +117 = 01110101 102+117 = 219 but 01100110 + 01110101 11011011 = -37 11 1 <- 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: 011011011 = 219 (which is correct). New Sign bit (-256) old sign bit = +128

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 = 0110.1100

Fixed Point Binary - Decimal Converter 23/04/2019 Fixed Point Binary - Decimal Converter Try using it to ‘play’ with fixed point binary.

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.

Fixed Point Binary Precision 23/04/2019 Fixed Point Binary Precision 110.1 = 6.5 110.11 = 6.75 We have missed out 6.51 to 6.74! This means accuracy is poor. Skip to slide 46 - Hexadecimal

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: 14 12 if it was understood that the first part is the mantissa and second part is the exponent

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.

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 ½, ¼.

23/04/2019 Exponent Represents the power of 2 by which the mantissa must be multiplied to give the original value.

Decimal Normalisation 23/04/2019 Decimal Normalisation 34,568,000 = 3456.8 x 104 = 0.34568 x 108 = 3.4568 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.

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.

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.

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?

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.

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).

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.

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

Hexadecimal Denary / Decimal Binary Hexadecimal 1 00000001 2 00000010 23/04/2019 Hexadecimal Denary / Decimal Binary Hexadecimal 1 00000001 2 00000010 etc …. 9 00001001 10 00001010 A 11 00001011 B 15 00001111 F 16 00010000

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)

Hexadecimal-Denary Converter 23/04/2019 Hexadecimal-Denary Converter Try using it to ‘play’ with hexadecimal numbers. https://4565e4f1bb6fcb191b6a80b2e8cd1502a3f5fc3b.googledrive.com/ 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

Denary -> Hexadecimal Question: 23/04/2019 Denary -> Hexadecimal Question: Translate 101 (denary) into hexadecimal. 65 Translate 64 (denary) into hexadecimal. 40

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 = 176 + 13 = 189 ( in denary)

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

Binary -> Hexadecimal 23/04/2019 Binary -> Hexadecimal Translate each group of 4 bits into denary and then into hexadecimal. e.g. 10010010 (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.

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.

23/04/2019 Questions: The ASCII code for the letter N is 0100 1110. Show how to represent this in a) denary 78 b) hexadecimal = (0100) (1110) = (4 + 14) = 4E

23/04/2019 Octal Counts in 8’s.

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)

Decimal -> Octal Question: 23/04/2019 Decimal -> Octal Question: Translate 101 (denary) into octal. 145

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 = 56 + 6 = 62 ( in denary)

Octal -> Decimal Question: 23/04/2019 Octal -> Decimal Question: Translate 101 (octal) into denary. 65

Binary -> Octal 165 (octal) 01110101 (binary) 23/04/2019 Binary -> Octal 01110101 (binary) Arrange in 3’s from the right. 01 110 101 Add leading 0 001 110 101 Each group of three bits converted denary but the final result is octal. 1 6 5 165 (octal)

23/04/2019 Unicode 16 bit code so can store 65536 characters and codes and simple communications protocols. Used to allow coding of languages that do not use Western characters. Currently supports 24 language scripts.

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).

23/04/2019 Plenary 01101101 155 6D 0001 0000 1001