Numbers and Representations

Similar documents
CS & IT Conversions. Magnitude 10,000 1,

Number System. Introduction. Decimal Numbers

Number representations

Chapter 1 Review of Number Systems

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Signed Binary Numbers

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

Introduction to Computer Science-103. Midterm

Computer Number Systems Supplement

Module 1: Information Representation I -- Number Systems

MACHINE LEVEL REPRESENTATION OF DATA

Computer Organization

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects:

Number Systems CHAPTER Positional Number Systems

CMPE223/CMSE222 Digital Logic Design. Positional representation

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

Introduction to Computers and Programming. Numeric Values

CHAPTER 2 Number Systems

Memory Addressing, Binary, and Hexadecimal Review

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

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

Final Labs and Tutors

2 Number Systems 2.1. Foundations of Computer Science Cengage Learning

Bits, Words, and Integers

Beginning C Programming for Engineers

Binary Values. CSE 410 Lecture 02

Level ISA3: Information Representation

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

Chapter 2: Number Systems

Lecture (02) Operations on numbering systems

Data Representation 1

What Is It? Instruction Register Address Register Data Register

Appendix. Numbering Systems. In This Appendix...

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

EE292: Fundamentals of ECE

Octal and Hexadecimal Integers

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

in this web service Cambridge University Press

The type of all data used in a C++ program must be specified

Representing Characters, Strings and Text

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

Chapter 2 Bits, Data Types, and Operations

Digital Systems and Binary Numbers

Appendix. Numbering Systems. In this Appendix

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

Chapter 1 Emad Felemban

Introduction to Numbering Systems

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Topic Notes: Bits and Bytes and Numbers

T02 Tutorial Slides for Week 2

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Arithmetic Processing

Lecture (01) Digital Systems and Binary Numbers By: Dr. Ahmed ElShafee

Signed umbers. Sign/Magnitude otation

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

Numbering systems. Dr Abu Arqoub

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Representation of Non Negative Integers

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

COMP2611: Computer Organization. Data Representation

Binary Representations and Arithmetic

Slide Set 1. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Digital Systems and Binary Numbers

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-278: Digital Logic Design Fall Notes - Unit 4. hundreds.

Chapter 4. Operations on Data

CS101 Lecture 04: Binary Arithmetic

Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates. Invitation to Computer Science, C++ Version, Third Edition

data within a computer system are stored in one of 2 physical states (hence the use of binary digits)

Inf2C - Computer Systems Lecture 2 Data Representation

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-2700: Digital Logic Design Winter Notes - Unit 4. hundreds.

Chapter 10 - Computer Arithmetic

World Inside a Computer is Binary

Module 2: Computer Arithmetic

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Topic Notes: Bits and Bytes and Numbers

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Computer Arithmetic. In this article we look at the way in which numbers are represented in binary form and manipulated in a computer.

DIGITAL SYSTEM DESIGN

CHAPTER TWO. Data Representation ( M.MORRIS MANO COMPUTER SYSTEM ARCHITECTURE THIRD EDITION ) IN THIS CHAPTER

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Number Systems. Both numbers are positive

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

Internal Data Representation

MC1601 Computer Organization

Review of Data Representation & Binary Operations Dhananjai M. Rao CSA Department Miami University

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

Excerpt from: Stephen H. Unger, The Essence of Logic Circuits, Second Ed., Wiley, 1997

COMP Overview of Tutorial #2

Transcription:

Çetin Kaya Koç http://koclab.cs.ucsb.edu/teaching/cs192 koc@cs.ucsb.edu Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 1 / 38

Outline Computational Thinking Representations of integers Binary and decimal numbers Storing bits: Magnetic core and semiconductor memories Conversion between binary and decimal Hexadecimal and octal representations Negative and positive integer representations Addition and subtraction Representing symbols and characters Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 2 / 38

Representation of Integers Natural (whole, counting) numbers: N = {0, 1, 2, 3,...} (Positive and negative) integers: Z = {..., 3, 2, 1, 0, 1, 2, 3,...} We represent integers in decimal for human convenience Decimal numbers have 10 unique digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Digit locations have weights which increase by a factor of 10, from right to left (from least significant to most significant):... 10 3 10 2 10 1 10 0... 1000 100 10 1 Examples:... 10 3 10 2 10 1 10 0... 2 0 1 5 2015... 6 7 2 3 6723 Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 3 / 38

Binary Representation Computational Thinking Two unique digits: 0 and 1 Weights of digits double from right to left, starting with 1 (from least significant to most significant): Examples:... 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0... 128 64 32 16 8 4 2 1... 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0... 0 0 0 0 1 0 1 0 10... 0 1 0 0 1 0 1 0 74... 1 0 0 0 1 0 1 0 138 A binary digit is called bit Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 4 / 38

Storing Bits Computational Thinking Decimal representation is for human convenience: we are used to it Computers represent and store all numbers in binary To store a single bit we need a 1-bit memory To store a longer number, say n bits, we need n copies of 1-bit memory, n-bit memory Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 5 / 38

Storing Bits Computational Thinking Conceptually a 1-bit memory is a box capable of holding the value of the bit (which is either 1 or 0) as long as we need A 4-bit memory holding the binary number 1010 (in decimal, ten): 1 0 1 0 We should be able to re-write another number on top of the existing one (thus erasing the old) For example, we can write 1001 (in decimal, nine) over 1001 1 0 0 1 We lose the previous number completely Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 6 / 38

Memory Computational Thinking There are different types of computer memory based on their speed, capacity, and the modes of use: registers, caches, main memory, hard disks, semiconductor drives, etc The speed and capacity is determined by the technology In today s technology the most common form of memory is based on semiconductor physics However, historically other memory technologies are used: electric relays, magnetic cores, etc Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 7 / 38

Magnetic Core Memory For nearly 20 years (1955-1975) magnetic core memory was the predominant form of memory It uses tiny magnetic toroids (rings), the cores, through which wires are threaded to write and read information Each core represents one bit of information The cores can be magnetized in two different ways (clockwise or counterclockwise) and the bit stored in a core is 0 or 1 depending on that core s magnetization direction The wires are arranged to allow an individual core to be set to either a 1 or a 0, and for its magnetization to be changed, by sending appropriate electric current pulses through selected wires Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 8 / 38

Computational Thinking Magnetic Core Memory C etin Kaya Koc http://koclab.cs.ucsb.edu Fall 2016 9 / 38

Magnetic Core Memory The distance between the rings is roughly 1 mm (0.04 in). The green and brown wires (X and Y) are for selection. The sense wires are diagonal, colored orange, and the inhibit wires are vertical twisted pairs. Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 10 / 38

Computational Thinking Magnetic Core Memory 4 16 16 bits = 1024 bits 1024 bits is 1K (Kilo) bits Various logical organizations: 1 32 32 2 16 32 4 2 8 16 1 1024 2 512 4 256 C etin Kaya Koc http://koclab.cs.ucsb.edu Fall 2016 11 / 38

Magnetic Core Memory Size A 1-bit magnetic core takes approximately 1 mm2 space The 1024-bit core would be about 1024 mm2 10 cm2 Your iphone may have 16G (Giga) bytes, which is 16 8 2 30 bits In core memory, this means an area of 128 2 20 10 cm2 This is equivalent to 134,217 m2 A football field is about 110m by 48m, which is 5,280 m2 16 Gigabytes of magnetic core memory takes the space of 25 fields! iphone with magnetic core memory would be unthinkable! :) Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 12 / 38

Magnetic Core Memory Size Current memory technology is based on semiconductors The geometry of 1-bit memory is measured by nanometers (10 9 m) The main concept of 1-bit (CMOS) memory: The size of 1Gbit memory is as small as 10 mm2 Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 13 / 38

Semiconductor Memory - Logical View Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 14 / 38

Semiconductor Memory - Chip Mask Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 15 / 38

Semiconductor Memory - Cross-Section View Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 16 / 38

Computational Thinking Semiconductor Memory - Physical Device C etin Kaya Koc http://koclab.cs.ucsb.edu Fall 2016 17 / 38

Semiconductor Memory - Future Devices Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 18 / 38

Semiconductor Memory - Future Devices Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 19 / 38

Binary and Decimal Representation Decimal representation is for human convenience It is hard to read binary numbers on paper: 11111011111? If represented in decimal, it looks familiar: 2015 Sometimes we need to convert binary numbers to decimal or vice versa Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 20 / 38

Binary2Decimal Conversion Sum the powers of 2 for which the bit is 1, ignore others (0000 1010) = 8 + 2 = 10 (0100 1010) = 64 + 8 + 2 = 74 (1000 1010) = 128 + 8 + 2 = 138 Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 21 / 38

Decimal2Binary Conversion Subtract the largest power of 2 (keeping the remainder positive) from the number, and set the corresponding bit to 1 The other bits are zero For example, if the number is 138, the largest power of 2 that is closest to 138 would be 128 = 2 7, and thus: 138 2 7 = 10 the bit 7 is set to 1 10 2 3 = 2 the bit 3 is set to 1 2 2 1 = 0 the bit 1 is set to 1 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 138 1 0 0 0 1 0 1 0 Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 22 / 38

Hexadecimal Representation A convenient on-paper representation of binary numbers The binary number is grouped into 4 bits, starting from right If the number of bits is not a multiple of 4, leftmost 0s are added 2015 11111011111 0111 1101 1111 Each 4-bit group is viewed as a digit, and represented using a separate symbol Since there are 2 4 = 16 distinct numbers, we need 16 symbols We could select any set of 16 distinct symbols: α, β, γ, δ,... However, there is a conventional (and more logical) method We use decimal digits for the first ten and the first six letters of the alphabet for the rest Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 23 / 38

Hexadecimal Representation 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F Therefore, 2015 would be represented as (2015) decimal = (0111 1101 1111) binary = (7DF ) hex Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 24 / 38

Octal Representation Computational Thinking Hexadecimal representation is just a convenient way to read and write binary numbers Another useful (but less frequently used) representation is: octal The binary number is grouped into 3 bits If the number of bits is not a multiple of 3, leftmost 0s are added 2015 11111011111 011 111 011 111 Each 3-bit group is viewed as a digit, and represented using the first eight decimal digits: 0, 1, 2, 3, 4, 5, 6, 7 Therefore, 2015 would be represented as (2015) decimal = (011 111 011 111) binary = (3737) octal Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 25 / 38

Negative Numbers Computational Thinking So far we considered only positive numbers In fact, we did not think about the sign at all All numbers we have seen so far are unsigned integers To represent signed numbers, we need to reserve a separate bit for the sign, and also treat the rest of the bits somewhat differently There are 3 methods: Sign-Magnitude, One s-complement, and Two s-complement These representations allow both the negative and positive numbers in a unified way Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 26 / 38

Sign-Magnitude Representation First we fix the number of bits to a particular value, such as 4 We use all 4 bits to represent a number even if it does not need it The leftmost bit represents the sign of the number, positive or negative 0 stands for positive and 1 stands for negative The rest of the bits represent the magnitude of the number For example, let us fix the number of bits to 4, thus we have 16 binary numbers What do they represent? Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 27 / 38

4-bit Sign-Magnitude Representation 0000 +0 0001 +1 0010 +2 0011 +3 0100 +4 0101 +5 0110 +6 0111 +7 1000-0 1001-1 1010-2 1011-3 1100-4 1101-5 1110-6 1111-7 Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 28 / 38

One s-complement Representation Similarly we fix the number of bits to a particular value, such as 4 We use all 4 bits to represent a number even if it does not need it The leftmost bit is the sign of the number, positive or negative 0 stands for positive and 1 stands for negative The negative of a number is the bitwise complement of the number For example, since +6 = (0110), we have 6 = (1001) Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 29 / 38

4-bit One s-complement Representation 0000 +0 0001 +1 0010 +2 0011 +3 0100 +4 0101 +5 0110 +6 0111 +7 1000-7 1001-6 1010-5 1011-4 1100-3 1101-2 1110-1 1111-0 Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 30 / 38

Two s-complement Representation Similarly we fix the number of bits to a particular value, such as 4 We use all 4 bits to represent a number even if it does not need it The leftmost bit is the sign of the number, positive or negative 0 stands for positive and 1 stands for negative The negative of a number is obtained by taking the bitwise complement of the number and then adding 1 to it For example, +6 = (0110) implies 6 = (1001) + (0001) = (1010) Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 31 / 38

4-bit Two s-complement Representation 0000 +0 0001 +1 0010 +2 0011 +3 0100 +4 0101 +5 0110 +6 0111 +7 1000-8 1001-7 1010-6 1011-5 1100-4 1101-3 1110-2 1111-1 Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 32 / 38

Negation in Two s-complement Representation Given a positive integer x, how do we find x? For example, given 3 = (0011), how do we obtain 3? Algorithm: Complement every bit and add 1 0011 1100 1100 + 1 1101 Similarly, given 4 = (1100), how do we obtain +4? 1100 0011 0011 + 1 0100 Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 33 / 38

Advantages/Disadvantages of Representations Sign-Magnitude and One s-complement representations have two zeros: negative and positive zero However all three representations are used, but usually just one in a given special or general purpose computer system Almost all general purpose computers use Two s-complement representation Arithmetic is easier with Two s-complement representation Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 34 / 38

Addition in Two s-complement Representation Add the numbers (including their signs) and ignore the carry out For example: (-2)+(+7)=(1110)+(0111) is performed as (1) (1) (1) (0) carry bits 1 1 1 0 ( 2) 0 1 1 1 (+7) 0 1 0 1 (+5) Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 35 / 38

Subtraction in Two s-complement Representation Subtraction is performed by negating the second operand and then performing addition: (+5)-(+7) = (+5)+(-7) We already learned the negation algorithm: Complement every bit of the number and then adding 1 to it Since x = +7 = (0111), then x = (1000) + 1 = (1001) = 7 Now, add the numbers (including their signs) and ignore the carry out (+5)+(-7)=(0101)+(1001) is performed as (1) (0) (0) (1) carry bits 0 1 0 1 (+5) 1 0 0 1 ( 7) 1 1 1 0 ( 2) Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 36 / 38

Overflow Computational Thinking If the result is beyond the range of numbers representable, there will be overflow (in all 3 representations) In 4-bit Two s-complement we can represent only { 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7} If we attempt to add 4 and 5 in 4-bit Two s-complement representation, the result (which is 9) is not representable, and thus we will not be able to obtain the correct sum Solution: Allow more bits (0) (1) (0) (0) carry bits 0 1 0 0 (+4) 0 1 0 1 (+5) 1 0 0 1 ( 7)? Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 37 / 38

Representing Symbols and Characters Representation method of integers should take arithmetic operations into account In general, any representation method of a particular class of objects should mind the use of objects in a computational setting or their relationship to one another In some cases (such as integers), the computational requirements are very important In some other cases (such as characters, letters, text), there are fewer or less stringent computational requirements We just need to assign a code to the symbol (character or letter) and remember it when we need it Çetin Kaya Koç http://koclab.cs.ucsb.edu Fall 2016 38 / 38