Data Representation and Binary Arithmetic. Lecture 2

Similar documents
Fundamentals of Programming (C)

Chapter 2 Number System

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Number System (Different Ways To Say How Many) Fall 2016

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Fundamentals of Programming

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Number Systems Base r

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

Chapter 2 Bits, Data Types, and Operations

Number Representations

Chapter 2 Bits, Data Types, and Operations

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Chapter 3. Information Representation

Positional Number System

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Oberon Data Types. Matteo Corti. December 5, 2001

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Chapter 2 Bits, Data Types, and Operations

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

BINARY SYSTEM. Binary system is used in digital systems because it is:

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

3.1. Unit 3. Binary Representation

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Under the Hood: Data Representation. Computer Science 104 Lecture 2

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

The Binary Number System

UNIT 2 NUMBER SYSTEM AND PROGRAMMING LANGUAGES

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

Do not start the test until instructed to do so!

EE 109 Unit 2. Binary Representation Systems

CPSC 301: Computing in the Life Sciences Lecture Notes 16: Data Representation

Lecture (09) x86 programming 8

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Digital Fundamentals

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Hardware. ( Not so hard really )

Source coding and compression

Do not start the test until instructed to do so!

Fundamental Data Types

ASSIGNMENT 5 TIPS AND TRICKS

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Chapter 3: Number Systems and Codes. Textbook: Petruzella, Frank D., Programmable Logic Controllers. McGraw Hill Companies Inc.

Do not start the test until instructed to do so!

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Variables and data types

UNIT 7A Data Representation: Numbers and Text. Digital Data

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

Serial I/O. 4: Serial I/O. CET360 Microprocessor Engineering. J. Sumey

CHW 261: Logic Design

Chapter 8. Characters and Strings

S-Series Sensor ASCII Protocol v8.1.0

Universal Asynchronous Receiver Transmitter Communication

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i

Coding Theory. Networks and Embedded Software. Digital Circuits. by Wolfgang Neff

FD-011WU. 2D Barcode Reader User Guide V1.6CC

Imperial College London Department of Computing

User s Manual. Xi3000 Scanner. Table of Contents

MACHINE LEVEL REPRESENTATION OF DATA

Binary Codes. Dr. Mudathir A. Fagiri

CS & IT Conversions. Magnitude 10,000 1,

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Connecting UniOP to Datalogic Barcode Readers

CSE 30 Spring 2007 Final Exam

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

A GUIDE TO RS-232 COMMUNICATION WITH FX PLCS

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

CSE 30 Spring 2006 Final Exam

CSE 30 Fall 2013 Final Exam

CSE 30 Winter 2009 Final Exam

FA269 - DIGITAL MEDIA AND CULTURE

Table of Contents Sleep Settings How to Configure the Scanner. 7 Chapter 2 System Setup

Xi2000-BT Series Configuration Guide

Configuration Manual PULSAR C CCD SCANNER. Table of Contents

CSE 30 Winter 2014 Final Exam

CSE 30 Fall 2007 Final Exam

MOBILE THERMAL PRINTER

CSE 30 Fall 2012 Final Exam

Digital Systems COE 202. Digital Logic Design. Dr. Muhamed Mudawar King Fahd University of Petroleum and Minerals

4/14/2015. Architecture of the World Wide Web. During this session we will discuss: Structure of the World Wide Web

Characters Lesson Outline

Transcription:

Data Representation and Binary Arithmetic Lecture 2 Computer Data Data is stored as binary; 0 s and 1 s Because two-state ( 0 & 1 ) logic elements can be manufactured easily Bit: binary digit (smallest quantity of info.) Organized in groups of bits stored in registers or memory The basic unit of data Aword: 8, 16, 32, 64 bits Usually all bits of a word are accessible simultaneously A byte is a group of 8 bits a word may be 1, 2, 4, or 8 bytes Lecture 2 2 1

Data Representation A computer word is whatever the programmer chooses it to represent Example: in our midterm, a student s score is recorded as 0000 0000 in binary. What s the meaning? It might be 0 or 100! Entities which can be represented by a binary word are: an instruction or an operation code E.g. For Freescale ColdFire Family microprocessor 0100 1110 0111 0010 represents STOP a numeric quantity Unsigned binary number, signed binary number Binary floating point number, complex number, etc. a character The letter A or b or TAB or a space a Graphic Character on the screen, e.g. Lecture 2 3 Binary Numbers 101.11 10 = 1x10 2 =100 + 0x10 1 = 0 + 1x10 0 = 1 + 1x10-1 = 0.1 + 1x10-2 = 0.01 101.11 2 = 1x2 2 = 4 + 0x2 1 = 0 + 1x2 0 = 1 + 1x2-1 = 0.5 + 1x2-2 = 0.25 = 5.75 10 101.91 2 Lecture 2 4 2

How to convert a number in base b to a decimal number? Lecture 2 5 Equivalence Table DECIMAL BINARY OCTAL HEXADECIMAL 0 0000 0 0 1 0001 1 1 2 0010 2 2 3 0011 3 3 4 0100 4 4 5 0101 5 5 6 0110 6 6 7 0111 7 7 8 1000 10 8 9 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F Lecture 2 6 3

Binary to Octal/Octal to Binary 1 Octal Digit corresponds to exactly 3 binary digits (8=2 3) EXAMPLES: 1) 011001101.1110 2 00 3 1 5. 7 0 315.70 8 Divide into groups of 3 2) 4720.37 8 100 111 010 000. 011 111 2 Write each digit in 3-bit binary format Lecture 2 7 Binary to Hexadecimal/Hex. to Binary 1 Hexadecimal Digit corresponds to exactly 4 binary digits (16=2 4) EXAMPLES: 1) 01101101.1110 2 6 D. E 16 2) 9D0.7C 16 Write each digit in 4-bit binary format 1001 1101 0000. 0111 1100 2 Lecture 2 8 4

Decimal to Hex. Conversion In order to convert a decimal number into a hexadecimal number, the decimal number can first be converted into a binary number, and then into a hexadecimal number EXAMPLE: Two methods: 1. Divide by 16 or 25 10 = N 16 2. convert to binary (divide by 2) & group 25/16 = 1 R=9 1/16 = 0 R=1 LSB MSB 25/2 = 12 R=1 12/2 = 6 R=0 LSB 6/2 = 3 R=0 N 16 = 19 16 3/2 = 1 R=1 N 2 = 1 1001 Note: division completes until 1/2 = 0 R=1 MSB the quotient is 0 Question: Octal to Hex.? Lecture 2 9 Character Representation AMERICAN STANDARD CODE FOR INFORMATION INTERCHANGE (ASCII) A code for representing alpha-numeric characters in a digital system 7 bits, 128 possible combinations 26 upper case, 26 lower case, 10 numerals, special? / } *! Control Characters Examples: CR Carriage Return LF Line Feed BS Back Space VT Vertical Tab HT Horizontal Tab ETX End of Text SOH Start of Header STX Start of Text ACK Acknowledge ENQ Inquiry Lecture 2 10 5

ASCII Character Set MSB LSB 0 000 1 001 2 010 3 011 4 100 5 101 6 110 0 0000 NUL DLE SP 0 @ P p 1 0001 SOH DC1! 1 A Q a q 2 0010 STX DC2 2 B R b r 3 0011 ETX DC3 # 3 C S c s 4 0100 EQT DC4 $ 4 D T d t 5 0101 ENQ NAK % 5 E U e u 6 0110 ACK SYN & 6 F V f v 7 0111 BEL ETB 7 G W g w 8 1000 BS CAN ( 8 H X h x 9 1001 HT EM ) 9 I Y i y A 1010 LF SUB * : J Z j z Lecture 2 11 7 111 B 1011 VT ESC + ; K [ k { C 1100 FF FS, < L \ l D 1101 CR GS - = M ] m } E 1110 SO RS. > N ^ n ~ F 1111 SI US /? O _ o DEL L = 4C 16 = 1001100 2 l = 6C 16 = 1101100 2 Example Upper and Lower Case Letters L 4C 16 1001100 16 l 6C 16 1101100 Masking Operations - to change to lower case: OR with 0100000 to SET bit #5 - to change to upper case: AND with 1011111 to CLEAR bit #5 B6 B5 B4 B3 B2 B1 B0 B6 B5 B4 B3 B2 B1 B0 OR 0 1 0 0 0 0 0 AND 1 0 1 1 1 1 1 (LSB is bit #0) B6 1 B4 B3 B2 B1 B0 B6 0 B4 B3 B2 B1 B0 Lecture 2 12 6

Parity Bit Often a 7-bit ASCII code character is encoded as an 8- bit binary code pattern. This extra bit is traditionally placed in the most significant bit position EXAMPLE: L 4C 16 1001100 Even Parity 11001100 Parity Bit Odd Parity 01001100 Even parity: the total # of 1 s in the encoded 8 bits is even Lecture 2 13 Binary Coded Decimal (BCD) A set of 4 bits is used to encode each decimal digit 0 to 9 (i.e., 0000 2 to 1001 2 ) EXAMPLE: 1) 4095 10 = N BCD Disadvantages of BCD encoding: BCD used 4xn bits to encode an n digit number Not as efficient to store information EXAMPLE: 1) 84 10 = 0100 0000 1001 0101 BCD Binary is 1010100 2 7 bits needed BCD is 1000 0100 8 bits needed, not as efficient Advantage: It is relatively easy to figure out what s the equivalent decimal number Lecture 2 14 7

Binary Arithmetic ADDITION SUBTRACTION 0 + 0 = 0 0-0 = 0 0 + 1 = 1 0-1 = 1 + borrow 1+0=1 1 1-0=1 1 + 1 = 0 + carry 1-1 = 0 EXAMPLES: Addition 172 10 + 153 10 Subtraction 172 10 153 = 10101100 2 = 10011001 2 325 10 1 01000101 2 = 69 10 Wrong = 10101100 2 (overflow) - 153 10-10011001 2 19 10 00010011 = 19 10 Correct For unsigned calculation: carry/borrow means overflow Signed calculation: see slides 22-23 Lecture 2 15 Signed Negative Number There are two commonly used methods to represent negative numbers: 1) Sign plus magnitude 2) 2's complement All systems use the most significant bit position (MSB) as the sign bit 1 - negative number 0 - positive number Lecture 2 16 8

Sign and Magnitude MSB LSB Given an n-bit, the range of values possible is -( 2 n-1-1 ) to ( 2 n-1-1 ) 111..1 011..1 EXAMPLES: 1) -7 4 bit +7 = 0111-7 = 1111 8 bit +7 = 0000 0111-7 = 1000 0111 2) -65 3) -128 +65 = 0100 0001-65 = 1100 0001 Not possible with 8 bits / Range -127 to 127-128 is fine in a 2 s complement system with 8 bits! Lecture 2 17 Ten s Complement Arithmetic The 10 s complement of a n-digit number, N, is defined as 10 n N It can be calculated by subtracting each of the digits from 9 and adding 1 to the result, e.g. Consider a 4 digit number 1234 10 10 4 1234 = 8766 9999-1234 8765 + 1 = 8766 Lecture 2 18 9

Ten s Complement Arithmetic The complement of the complement is the original number. 10 4 1234 = 8766 10 4 8766 = 1234 Lecture 2 19 2 s Complement 0010 1101 + 1 1110 0010 1110 Generation of 2's complement representation of an n-bit number - Subtract no. from 2 n i.e 2 n -A 0010 1 0000 - Flip each bit of the n bits, and add 1 0010 - Copy bits from right until 1st One, then flip rest 2 s complement n=8 sign & magnitude 011 111 2 n-1-1 127 011 111 000 000 0 0 000 000 100 000 111 111-1 -1 100 001 111 110-2 -2 100 010 100 001 -( 2 n-1-1) -127 111 111 100...000 -( 2 n-1 ) -128 not valid 1110 Range: -( 2 n-1 ) to ( 2 n-1-1 ) -( 2 n-1-1) to ( 2 n-1-1 ) In 2 s complement system, -A is represented by 2 s complement of A Lecture 2 20 10

2 s Complement -- Examples 44 + (-56) +44 = 00101100 +56 = 00111000-56 = 11001000 = 11110100 11110100 and 00001100 (i.e., 12) are complement to each other. So 11110100 represents -12 You try the addition of two negative numbers, e.g., (-44) + (-56) Lecture 2 21 Overflow Assume 8-bit binary numbers. Does C=0 mean V=0? 68 +73 141 Yes, Why???? real result out of range: the biggest positive number with 8-bits is 127 68 + (-09) Overflow?? Overflow?? 01000100 01001001 C=0 10001101 V=1 No, Why??? 1 0100 1 0100 1 111 0111 C=1 0 011 1011 =59 10 V=0 For summation of two numbers of different sign, the real result is always within range. You find the examples with C=0, V=0, and C=1, and V=1 Lecture 2 22 11

Overflow (Cont d) When the real result of an addition or subtraction operation gives too large a result, this represents an overflow condition. There is overflow in addition when positive negative + positive + negative negative positive There is no overflow in addition when positive negative positive negative + positive + negative + negative + positive positive negative There is overflow in subtraction when positive negative - negative - positive negative positive Lecture 2 23 Extension of 2 s complement numbers If we have a 4 bit 2's complement number, what is the corresponding 8-bit representation? Extend the MSB! +7 = 0111 +7= 00000111-7 = 1001-7 = 11111001 Weighted values of bit positions in a 2's complement representation: n=8, 8-bit a 7 a 6 a 5 a 4 a 3 a 2 a 1 a 0-2 n-1, -2 7 = -128 11111001 +2 6 = 64 +2 5 = 32 +2 4 = 16 Lecture 2 24 12