Number Systems. The Computer Works in Binary, or how I learned to think like a computer. The computer s natural number system is binary not decimal.

Similar documents
Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

CHW 261: Logic Design

Digital Fundamentals

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

CS & IT Conversions. Magnitude 10,000 1,

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

The x86 Microprocessors. Introduction. The 80x86 Microprocessors. 1.1 Assembly Language

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.

Fundamentals of Programming Session 2

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

Objectives. Connecting with Computer Science 2

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

Memory Addressing, Binary, and Hexadecimal Review

More about Binary 9/6/2016

Level ISA3: Information Representation

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

Number System. Introduction. Decimal Numbers

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

Final Labs and Tutors

Moodle WILLINGDON COLLEGE SANGLI. ELECTRONICS (B. Sc.-I) Introduction to Number System

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

MACHINE LEVEL REPRESENTATION OF DATA

Data Types Literals, Variables & Constants

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

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

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

Appendix. Numbering Systems. In This Appendix...

CS 101: Computer Programming and Utilization

Variables and Data Representation

Beyond Base 10: Non-decimal Based Number Systems

COMP Overview of Tutorial #2

Introduction to Computer Science-103. Midterm

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

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

Beginning C Programming for Engineers

Inf2C - Computer Systems Lecture 2 Data Representation

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

NUMERIC SYSTEMS USED IN NETWORKING

Number Systems. Both numbers are positive

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

Appendix. Numbering Systems. In this Appendix

Binary, Hexadecimal and Octal number system

Logical Bases: Representation of Numbers Decimal number Positional notation Binary numbers

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

Number Systems and Number Representation

MATH 104B OCTAL, BINARY, AND HEXADECIMALS NUMBERS

Digital Fundamentals

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

Number representations

Beyond Base 10: Non-decimal Based Number Systems

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

Topic Notes: Bits and Bytes and Numbers

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

TOPICS. Other Number Systems. Other Number Systems 9/9/2017. Octal Hexadecimal Number conversion

Data Types Literals, Variables & Constants

Numerical Precision. Or, why my numbers aren t numbering right. 1 of 15

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

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

Binary Addition & Subtraction. Unsigned and Sign & Magnitude numbers

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

unused unused unused unused unused unused

15213 Recitation 2: Floating Point

Computer Number Systems Supplement

COMP2611: Computer Organization. Data Representation

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

ECE 372 Microcontroller Design Assembly Programming Arrays. ECE 372 Microcontroller Design Assembly Programming Arrays

Computer Organization and Assembly Language. Lab Session 4

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

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

CMPS 10 Introduction to Computer Science Lecture Notes

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

ROUNDING ERRORS LAB 1. OBJECTIVE 2. INTRODUCTION

Chapter 3 Data Representation

Chapter 02: Computer Organization. Lesson 02: Functional units and components in a computer organization- Part 1: Processor

Chapter 2 Bits, Data Types, and Operations

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Introduction to Computers and Programming. Numeric Values

THE LOGIC OF COMPOUND STATEMENTS

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

Lecture C1 Data Representation. Computing and Art : Nature, Power, and Limits CC 3.12: Fall 2007

DIGITAL SYSTEM FUNDAMENTALS (ECE 421) DIGITAL ELECTRONICS FUNDAMENTAL (ECE 422) COURSE / CODE NUMBER SYSTEM

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

But I know what most of you will do: use a calculator to convert; that's fine, IF you understand the theory.

CS61c Midterm Review (fa06) Number representation and Floating points From your friendly reader

Chapter 3: Arithmetic for Computers

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

Representing Information. Bit Juggling. - Representing information using bits - Number representations. - Some other bits - Chapters 1 and 2.3,2.

A Level Computing. Contents. For the Exam:

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

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

Decimal Binary Conversion Decimal Binary Place Value = 13 (Base 10) becomes = 1101 (Base 2).

EXPERIMENT 1. FAMILIARITY WITH DEBUG, x86 REGISTERS and MACHINE INSTRUCTIONS

Fundamentals of Programming (C)

The Design of C: A Rational Reconstruction


Transcription:

PROGRAMMING CONCEPTS Number Systems The Computer Works in Binary, or how I learned to think like a computer Copyright 2013 Dan McElroy The computer s natural number system is binary not decimal. For example, if you input 27 + 46, each of the numbers are converted to binary, the answer is processed in binary and then the result is converted back into a decimal 73 to be output on the screen. 27 00011011 +46 +00101110 =01001001 73 INPUT PROCESS OUTPUT 1

Suppose your brain worked in binary What is 27 + 46? 27 00011011 +46 00101110 01001001 73 27 + 46 73 Suppose that I was learning Spanish and the teacher asks Qué es lo que ves? 2

and I hear the words in Spanish and input them into my brain Qué es lo que ves? Qué es lo que ves? I then convert the Spanish words into English Qué es lo que ves? What do you see? 3

Process the answer using English What do you see I see a green house Convert the answer back into Spanish I see a green house Veo una casa verde 4

and output the answer back in Spanish Qué es lo que ves? Veo una casa verde Now does it make sense how the computer works? What is 27 + 46? 27 00011011 +46 00101110 01001001 73 27 + 46 73 5

For the Computer, it is Decimal as a Second Language For Assembly example, Language if you input 27 + High 46, Level each Language of the numbers are converted to binary, the answer is processed in binary and then the result is converted back into and a output decimal routines. 73 to be output on the screen. You may need to provide the code to convert from decimal to binary, and back from binary to decimal. Conversion to and from binary is already built into the input 27 00011011 +46 +00101110 =01001001 73 INPUT PROCESS OUTPUT Integers Integers are whole numbers. The values of integers can be positive, negative or zero. Examples: 79-85 0 6

Binary Representation of an Integer s 73 = 64 + 8 + 1 = 73 An unsigned decimal value from 0 to 255 can be stored in 8 bits for 256 combinations, including 0. For signed numbers, the most significant bit is used as a sign bit to indicate a positive or negative number leaving one less bit for the value. Then, 8 bits can be used to store a value from -128 to +127, still 256 combinations. More Bits for Larger Numbers 1 byte 2 bytes C C++ VisualBasic Bytes Range byte Byte 1 0 to 255 short Short 2-32,768 to +32,767 unsigned short 2 0 to 65,536 int long Integer 4-2,147,483.648 to +2,147,483,647 unsigned int or long Long 8 4 0 to 4,294,967,295-9,223,372,036 854,775,808 to +9,223,372,036 854,775,807 7

Converting to Binary The easiest way to convert from decimal to binary and back again is to use a calculator designed for programmers. Most graphical operating systems already include this type of calculator. Here is the calculator from Windows 7. Signed vs. Unsigned Internally, computers only use binary, 0 s and 1 s, also known as base 2. As a human, I learned to count in decimal, 0 through 9, also known as base 10. Before starting a discussion on how computers implement signed numbers (+ and ), first let s see how it could be implemented in decimal without using a + or a symbol. 8

Ten s Complement Think about an odometer in a car. As the mileage reaches 99999, one more mile causes the odometer to roll over to 00000. See the chart under the picture. I don t know if you ever saw the movie, Ferris Bueller s Day Off, but Ferris and his friend Cameron borrowed Cameron s Dad s red Ferrari and some dude took it and ran up a lot of extra miles on the car before the kids got it back. They jacked up the car, ran it in reverse hoping to take off the miles. Unfortunately, this didn t work. Adding Miles 99997 + 1 = 99998 + 1 = 99999 + 1 = 00000 + 1 = 00001 + 1 = 00002 + 1 = 00003 Ten s Complement We could use half of the values from 00000 up to 49999 to represent positive numbers, and 99999 down to 50000 to represent negative numbers. All of the math would work for addition and subtraction. For example, +3 5 would give 99998 which is 2. We would just need to convert the negative values into a human readable format using the symbol before displaying the numbers on the display. Subtracting Miles 00003 is +3 1 = 00002 is +2 1 = 00001 is +1 1 = 00000 is 0 1 = 99999 is 1 1 = 99998 is 2 1 = 99997 is 3 9

Two s Complement By default, integers are signed numbers. C and C++ can also declare an integer to be unsigned unsigned int x; The charts below use only eight bits, but show the decimal equivalent value as the binary value rolls over past 00000000. Unsigned integers 11111101 is 253 11111110 is 254 11111111 is 255 00000000 is 0 00000001 is 1 00000010 is 2 00000011 is 3 Signed integers 11111101 is 3 11111110 is 2 11111111 is 1 00000000 is 0 00000001 is +1 00000010 is +2 00000011 is +3 Two s Complement For both signed and unsigned, when the least significant bit = 0 the number is even, and when it = 1, the number is odd. For signed numbers when the most significant bit = 0 the number is positive, and when it = 1, the number is negative. Unsigned integers 11111101 is 253 11111110 is 254 11111111 is 255 00000000 is 0 00000001 is 1 00000010 is 2 00000011 is 3 Sign bit Signed integers 11111101 is 3 11111110 is 2 11111111 is 1 00000000 is +0 00000001 is +1 00000010 is +2 00000011 is +3 10

Why Is Zero a Positive Number? Actually, zero is not a positive number. But in binary, all zeros represents the number zero. There was confusion in the early days of computers of what to do with 10000000 which had a 1 in the sign bit, but all zeros for the number. Could this be considered negative 0? One time, a customer received a bill for -0.00 but since the balance was not all zero, they kept receiving bills and threatened with collections. Now, the number zero uses up one of the possible combinations of positive numbers (sign bit = 1), so a Short Integer has the range of -32,768 to +32,767 Groups of Bits Although computers only work in binary, it can be somewhat confusing to write down a large number of 1 s and 0 s. Octal and hexadecimal are a convenient shorthand way of representing binary numbers. Octal numbers are used to represent groups of three bits and hexadecimal numbers are used to represent groups of four. Hexadecimal usually goes by its nickname hex. 11

Binary Octal and Hex There are eight combinations when using three bits, and sixteen combinations when using four bits. The choice of whether to use octal or hex depends on how the bits are logically grouped on the computer or in the program. Octal 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7 Memory and character codes are organized in groups of eight bits (a byte). Two hex characters easily represent a byte. Binary Hex Decimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 Hexadecimal Numbers For example, 101110100101001 is separated into groups of four bits 101 1101 0010 1001 starting from the least significant bit (LSB). Assume leading zeros if there are less than four bits on the left side. Binary is then converted into the hex digits 5D29 using the table. As a programmer, you should memorize the hex table. The letters A F are used so that only one character is required for all sixteen combinations. Binary Hex Decimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 12

Converting Below are samples converting between binary, octal and hex. Bin 010 110 101 001 011 Oct 2 6 5 1 3 Binary Octal 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7 Bin 0100 0110 1101 0010 1111 1110 Hex 4 6 D 2 F E Try converting: Bin 011 111 000 110 101 100 010 Oct Bin 1110 0011 1011 1110 0000 0101 Hex Binary Hex Decimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 ASCII Characters Convert the following bit patterns into ASCII characters. HINT: First convert the binary into hex, and look up the character in the table. Hex Table 0000=0 0100=4 1000=8 1100=C 0001=1 0101=5 1001=9 1101=D 0010=2 0110=6 1010=A 1110=E 0011=3 0111=7 1011=B 1111=F 01001000 01100101 01101100 01101100 01101111 13

ASCII Characters Convert the following bit patterns into ASCII characters. HINT: First convert the binary into hex, and look up the character in the table. Hex Table 0000=0 0100=4 1000=8 1100=C 0001=1 0101=5 1001=9 1101=D 0010=2 0110=6 1010=A 1110=E 0011=3 0111=7 1011=B 1111=F 01001000 01100101 01101100 01101100 01101111 Separate the bits into groups of eight (bytes) ASCII Characters Convert the following bit patterns into ASCII characters. HINT: First convert the binary into hex, and look up the character in the table. Hex Table 0000=0 0100=4 1000=8 1100=C 0001=1 0101=5 1001=9 1101=D 0010=2 0110=6 1010=A 1110=E 0011=3 0111=7 1011=B 1111=F 01001000 01100101 01101100 01101100 01101111 4 8 6 5 6 C 6 C 6 F Separate each byte into nybbles (4 bits) and then decode each hex character. 14

ASCII Characters Convert the following bit patterns into ASCII characters. HINT: First convert the binary into hex, and look up the character in the table. Hex Table 0000=0 0100=4 1000=8 1100=C 0001=1 0101=5 1001=9 1101=D 0010=2 0110=6 1010=A 1110=E 0011=3 0111=7 1011=B 1111=F 01001000 01100101 01101100 01101100 01101111 4 8 6 5 6 C 6 C 6 F H e l l o Look up the hex codes in the ASCII table to find out which character they represent. Memory Dumps and Characters Memory dumps and character codes are typically displayed in hex The first part of this memory dump of command.com from Microsoft DOS shows executable instructions. The second part of the memory dump shows a list of keyboard commands and where to process them. Compare the ASCII chart to the decoded characters on the right. 1CC0:0100 83 7E A4 01 72 64 C7 46-F8 01 00 8B 76 F8 80 7A.~$.rdGFx...vx.z 1CC0:0110 A5 20 73 49 80 7A A5 0E-75 06 C6 42 A5 0A EB 3D % si.z%.u.fb%.k= 1CC0:0120 8B 76 F8 80 7A A5 08 74-0C 80 7A A5 07 74 06 80.vx.z%.t..z%.t.. 1CC0:0130 7A A5 0F 75 28 FF 46 FA-8B 76 FA 8B 84 06 F6 8B z%.u(.fz.vz...v. 1CD6:3B00 05 43 4F 50 59 01 CC 1A-06 50 41 55 53 45 00 1F.COPY.L..PAUSE.. 1CD6:3B10 13 05 44 41 54 45 00 38-18 05 54 49 4D 45 00 CE..DATE.8..TIME.N 1CD6:3B20 18 04 56 45 52 00 57 0E-04 56 4F 4C 01 C8 0D 03..VER.W..VOL.H.. 1CD6:3B30 43 44 01 A6 12 06 43 48-44 49 52 01 A6 12 03 4D CD.&..CHDIR.&..M Sample display of internal processor registers AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 DS=2246 ES=2246 SS=2246 CS=2246 IP=1443 15

FLOATING POINT NUMBERS Floating point numbers can contain digits past the decimal point. Floating point numbers are called REAL NUMBERS in the math department and in some computer languages Examples: 7.0 0.0 3.1418579 0.00000000578 positive -85.3357-0.00000000578 negative Scientific Notation If you ever took a science class, one of the first things the teacher introduced was scientific notation. Only one digit is placed before the decimal point and then an exponent of base 10 to indicate where the decimal point should be. The number has two parts, the mantissa (numbers), and the exponent (power of 10). Value Scientific Notation Computer Display 398445 3.98445 x 10 5 3.98445e5-175.6-1.756 x 10 2-1.756e2 3.0 3.0 X 10 0 3.0e0 0.0000000582 5.82 x 10-8 5.82e-8 16

Floating Point in Binary The IEEE (Institute of Electrical and Electronics Engineers) has defined a standard format for representing floating point numbers in binary. Just like the scientific notation, floating point numbers are stored in two parts when they are converted to binary. Double Precision Floating Point Numbers Are Extremely Accurate - but not always perfect. In some cases the computer approximates the number as close as is possible within the limits of the number of bits available to store the number. Floating point numbers are GREAT for scientific work, but occasionally the extremely small errors cause grief in the financial community. 17

Problems with Floating Point Using 64 bits, the results of computing with floating point math are very accurate, but sometimes very small inaccuracies occur. For example, using a plain four-function calculator, if you take 10, divide by 3, and then multiply by 3 again, you would expect to get 10. But this is what happens on some four-function calculators is: 10 / 3 = 3.3333333 3.3333333 x 3 = 9.9999999 Some of the calculators will round the display to show a 10, but still keep 9.9999999 internally. Problems with Floating Point I tried the 10 / 3 x 3 and ended up with 10 on several compilers. But when I tried 29 / pi x pi, even though the display showed 29, the answer was not 29 internally. I needed to display 20 digits past the decimal to see what number the internal memory actually held. The internal value was actually 29.00000000000000400000 When I compared the original value to the result, they were not equal. You can sometimes have a bad result if you test to see if a floating point number is equal to another number. 18

// sample C++ program to demonstrate errors // in floating-point computations // see if the computed result is the same as the original value // if they are equal, display OK on the screen // otherwise display BAD on the screen // show the result without a bunch of digits past the decimal ACKNOWLEDGEMENTS IMAGES Wikimedia Commons Microsoft ClipArt 19