PSU CS 106 Computing Fundamentals II Bits, Binary Numbers & Binary Arithmetic HM 1/3/2009.

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

HEXADECIMAL NUMBERS Code
2-1 Chapter 2 - Data Representation Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer Architecture.
Chapter 2 : Number System
Digital Fundamentals Floyd Chapter 2 Tenth Edition
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Standard positional representation of numbers:
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
PSU CS 106 Computing Fundamentals II Base Conversion of Integer Numbers HM 4/6/2008.
Assembly Language and Computer Architecture Using C++ and Java
Data Representation in Computers
Digital Fundamentals Floyd Chapter 2 Tenth Edition
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
The Binary Number System
Number Systems Lecture 02.
Binary Number Systems.
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
Binary Representation and Computer Arithmetic
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 5 Data representation.
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
Data Representation Number Systems.
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Fundamentals Tenth Edition Floyd.
Simple Data Type Representation and conversion of numbers
ES 244: Digital Logic Design Chapter 1 Chapter 1: Introduction Uchechukwu Ofoegbu Temple University.
Numeral Systems Subjects: Numeral System Positional systems Decimal
Data Representation – Binary Numbers
Computer Arithmetic Nizamettin AYDIN
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
IT253: Computer Organization
Data Representation.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Number Systems Spring Semester 2013Programming and Data Structure1.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013.
Data Representation Dr. Ahmed El-Bialy Dr. Sahar Fawzy.
The Teacher CP4 Binary and all that… CP4 Revision.
Fixed and Floating Point Numbers Lesson 3 Ioan Despi.
CISC1100: Binary Numbers Fall 2014, Dr. Zhang 1. Numeral System 2  A way for expressing numbers, using symbols in a consistent manner.  " 11 " can be.
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)
Computer Math CPS120: Lecture 3. Binary computers have storage units called binary digits or bits: Low Voltage = 0 High Voltage = 1 all bits have 0 or.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
Data Representation, Number Systems and Base Conversions
Computer Math CPS120 Introduction to Computer Science Lecture 4.
SAK Chapter 21 Chapter 2 : Number System 2.1 Decimal, Binary, Octal and Hexadecimal Numbers 2.2 Relation between binary number system with other.
Number Systems Denary Base 10 Binary Base 2 Hexadecimal Base 16
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 1 Representing Data in a Computer. 1.1 Binary and Hexadecimal Numbers.
Lecture No. 4 Computer Logic Design. Negative Number Representation 3 Options –Sign-magnitude –One’s Complement –Two’s Complement  used in computers.
Computer Math CPS120 Introduction to Computer Science Lecture 7.
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.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Dr. ClincyLecture 2 Slide 1 CS Chapter 2 (1 of 5) Dr. Clincy Professor of CS Note: Do not study chapter 2’s appendix (the topics will be covered.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Floating Point Representations
Programming and Data Structure
NUMBER SYSTEMS.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Number Representation
Chapter 3 Data Representation
CS1010 Programming Methodology
Data Representation Data Types Complements Fixed Point Representation
Storing Negative Integers
Presentation transcript:

PSU CS 106 Computing Fundamentals II Bits, Binary Numbers & Binary Arithmetic HM 1/3/2009

2 © Dr. Herbert G. Mayer Agenda Decimal Number System Binary Number System Signed Binary Integers Two’s Complement Representation tcr Decimal to Binary Conversion Sample 8-bit Binary Numbers Hexadecimal Numbers Adding Hex Other Bases, and Bit Strings Floating-Point Numbers Characters

3 © Dr. Herbert G. Mayer Decimal Number System The decimal number system of our daily life uses 10 as the base to construct arbitrarily large values. Important in this number system are the use of the value 0, and the use of a series of powers of 10 increasing from right to left. Each digit position moving toward the left increases the exponent (of the power of 10). Why we use 10 as the base is not totally obvious, yet our having 10 fingers and 10 toes may have something to do with it. This decimal system had been used in the Arab civilization since times way before Christ. Due to superb qualities, the decimal number system has replaced the Roman number system in the western world. The Roman system was vaguely based on 10, but lacked the notion of a zero and the power series. In other ancient civilizations (Babylonian) 60 was a common numeric base, and today’s system of tracking time with 60 seconds per minute and 60 minutes per hour still is reminiscent of that practice. Other ancient cultures used 64 as the base, yet computers today use 2 as their base. Earlier computers with base 10 were also fairly common.

4 © Dr. Herbert G. Mayer Decimal Number System, Samples Decimal number has the value one thousand, two hundred, and thirty. 00,001,230 similarly, except commas are added for better readability. Yet leading zeros have no value and can be ignored. Thus it is efficient to skip them. Trailing zeros after other decimal digits –before the implied decimal point— have enormous meaning, as they shift the complete range of all preceding digits to higher values. Decimal integers are generally written without decimal point. The actual, numeric value of decimal integers is constructed from right to left, with increasing exponent to base 10: = 0 * * * * * * * 10 6 For general decimal numbers the computation of the fractional value progresses left to right, with decreasing exponents for the base 10, starting with -1 (i.e ) in the first position after the decimal point: =(integral) 2 * * 10 1 (fraction) 3 * * * 10 -3

5 © Dr. Herbert G. Mayer Binary Number System Binary number 0101 has the value one hundred and one, binary or 5 decimal. Like decimal numbers, leading zeros in the binary system have no value and are ignored. However, since computers use a fixed number of bits for binary numbers, such unused bits are internally set to 0. Trailing zeros –before the implied binary point— have enormous meaning, as they shift the complete range of all preceding binary digits (bits). Binary integers are generally written without binary point. The actual, numeric integer value of binary integers is constructed from right to left, with increasing exponent to the base 2 from right to left: 0101 = 1 * * * * 2 3 = = = 5 10 = 5 -– 5 is unambiguous, as no matching binary digit exists!

6 © Dr. Herbert G. Mayer Binary Number System, Samples By convention, numbers (decimal or binary) are written without their base when the context is clear. But if the base is critical to know, a subscripted base is added for clarity. When the digit allows us to infer the base, then again it would be superfluous to spell out the base. For general binary numbers the computation of the fractional value progresses left to right, with decreasing exponents of base 2, starting with -1 for 2 -1 in the first position after the binary point: = (integral) 1 * * * (fractional) 1 * * = (integral) (fractional) ½ + ¼ = = – 5.75 unambiguous, as no binary digits 5 or 7 exist!

7 © Dr. Herbert G. Mayer Binary Number System, Samples

8 © Dr. Herbert G. Mayer Signed Binary Integers Possible representations of binary numbers: sign-magnitude, one’s complement, and two’s complement representation (tcr). Advantage of tcr: the machine needs no subtract unit, a single adder suffices. When subtraction is needed, just add a negative number. Also there is no need for signed- and unsigned arithmetic; unsigned suffices. C++ uses heavily unsigned integers. In fact tcr HW implementations can ignore the sign bit. Such architectures just need an adder and inverter. Binary numbers in tcr use a sign bit and a fixed number of bits for the magnitude. For example, on a PC you may have 32-bit integers, one for the sign, 31 for the magnitude. When processed in a tcr architecture, the most significant bit is the sign bit, the other 31 bits hold the actual value of interest. By convention, a sign bit value of 0 stands for positive and 1 for negative (numbers).

9 © Dr. Herbert G. Mayer Two’s Complement Representation tcr To create a negative number in tcr, start out with the binary representation for the positive one, invert all bits, and add 1. Note that overflow cannot happen by inversion alone: the inverse (negative value) of all representable positive numbers can always be created. Overflow can happen during addition. Overflow occurs, when the sign bit’s carry-in is different from the carry-out, i.e. when the carry into the sign bit is different from the carry out of the sign bit, i.e. the carry created by adding the two sign bits. The number of negative numbers is one more than the number of positive ones (not counting 0). Naturally, there is also a 0, but no negative 0 in tcr, as we find in one’s complement (ocr) and sign-magnitude (smr) representations. To generate the absolute value of a negative number, invert all bits of the original negative number and add a 1. It is important to add a 1 again, not to subtract it. However, there will be one negative value, whose positive inverse cannot be represented, causing overflow. That value is the smallest negative number. An 8-bit, signed tcr integer can hold integral values in the range from See the asymmetry? See the one negative value that cannot be inverted?

10 © Dr. Herbert G. Mayer Decimal to Binary Conversion Take the decimal number d to be converted to binary representation, and as long as d is not yet zero, do the following repeatedly: –divide d by 2; the new value is d/2 –/ is integer divide, different from floating-point divide! –write the remainder, which will be 0 or 1; call this the initial string s –do repeatedly, until d is 0, each time adding one more remainder digit to s When done, read the digits of s backwards: that is number b in binary

11 © Dr. Herbert G. Mayer Binary Conversion: You Exercise When d is finally 0, after repeated integer division by 2, reverse the order of the remainder digits, which are the bits in s. In the above example we used decimal = This works for any base. Now convert decimal 678 to binary:

12 © Dr. Herbert G. Mayer Sample 8-bit Binary Numbers

13 © Dr. Herbert G. Mayer 8-Bit Two’s Complement of Creating a negative number in Two’s Complement binary representation (TCR): Use the bit string for the positive integer, invert all bits, and add a 1, ignore overflow if it occurs:

14 © Dr. Herbert G. Mayer 8-Bit Two’s Complement of bit integer with 1 bit for sign, leaves 7 bits for magnitude Range from -128 to Note asymmetry of numeric range. Sample of will fit, i.e. does not cause overflow:

15 © Dr. Herbert G. Mayer 8-bit Two’s Complement of 9+-7 Left sample: = 16,fits into 8-bit signed TCR Right side: 9 – 7 = 9 + (-7),no need for binary subtract unit Just need an inverter, and use existing adder to add binary value 1

16 © Dr. Herbert G. Mayer 8-bit Two’s Complement of Left sample: +7-9 = -2, in TCR system is = Right side: -9–7 = -16, in TCR system is =

17 © Dr. Herbert G. Mayer 8-bit Two’s Complement of Left sample: = 164 will not fit in 8 bits: Overflow (OVFL) Since carry-in to S = 1, carry-out of S = 0: different; hence OVFL Right sample: -100 – 64 = -164, also won’t fit: OVFL Since carry-in to S = 0, carry-out of S = 1: different; hence OVFL

18 © Dr. Herbert G. Mayer Exercise TCR: 40+80,40-80 The above 2 samples will fit into 8-bit TCR! Will the subsequent one fit? Prove your answer!

19 © Dr. Herbert G. Mayer Exercise TCR: , One sample will barely fit into 8-bit TCR. Which one? The other will barely not fit, prove OVFL occurs!

20 © Dr. Herbert G. Mayer Hexadecimal Numbers Hexadecimal (AKA hex) numbers are simply numbers with base 16, not 10, or 2. They have 16 different digits, 0..9 and a..f. The symbol a stands for hex digit 10 10, while f stands for the hex digit Here a few hex numbers and their equivalent decimal values:

21 © Dr. Herbert G. Mayer Adding Hex: af , 10a be 16 Adding Hex numbers proceeds like addition in base 10: Starting at the lowest (rightmost) digit, add 1 digit for both rows at a time, thus the max value cannot reach or exceed 32. Store the digit and record the possible carry for the next digit; note that this can be 1 through f.

22 © Dr. Herbert G. Mayer Other Bases If you manipulate numbers with a different, larger base than 16, invent suitable new digits, analogous to the convention for hex numbers. For example, if you select a base of 20 10, extend the digit range to the digits 10 10, 11, , represented as a, b, c,.. i, j. Here i 20 stands for the digit and j 20 for For example, the decimal number is in base 20 notation.

23 © Dr. Herbert G. Mayer Other Bit Strings Other Bit String Interpretations: On many computers a sequence of 8 bits is called a Byte, if such a sequence is uniquely addressable as a whole, complete unit. Sequences of bits can be used (i.e. be interpreted) as integer numbers of varying precision, as floating point numbers, decimal numbers, printable ASCII characters of which about 93 are printable, EBCDIC characters, or Unicode with about 100,000 different symbols. When looking at any string of bits it is important to keep in mind: “What is the current interpretation of these bits? Are they numbers, printable characters, or combinations of various things?”

24 © Dr. Herbert G. Mayer Integer Numbers Integer Numbers, AKA ints, represented on a computer come in various precisions, based on the number of bits allotted to them. See past samples of 8-bit numbers using tcr. They are used as signed or unsigned ints, the latter heavily used in early days of computing, since each bit in a restrictedly short word was precious. Typical integer precisions include 8, 16, 32, or 64 bits. In unsigned integers, the bit otherwise used for the sign is used to double the representable range of distinct numbers.

25 © Dr. Herbert G. Mayer Floating-Point Numbers Floating-Point Numbers: AKA floats are commonly packaged into 32 bits, with 1 bit for the sign, 8 bits for the exponent of an assumed base 16, and 23 bits for the mantissa. Double-precision floating point numbers use 64 bits, 1 for the sign, 11 to represent the exponent to the implied base 16, and 52 bits for the much more accurate mantissa. The IEEE standard 754 defines a commonly used format for floats, available on many contemporary computers. In the past, different formats and definitions of floats caused dangerous incompatibilities during the porting of data from one computer from another.

26 © Dr. Herbert G. Mayer Characters Characters: The American Standard Code for Information Interchange (ASCII) defines certain strings of bits in an 8-bit sequence (AKA byte) to be printable characters. These include 26 printable, common lowercase letters, also uppercase letters, 10 digits, numerous “special characters”, a small range of letter used in some foreign alphabets such as ä and œ, plus some unprintable characters. Only 128 combinations of the 8-bit sequences were used in the original ASCII definition. The leftmost bit in the 8-bit sequence was left unused. The Extended Binary Coded, Decimal Interchange Code (EBCDIC) is a different standard for defining character values on a computer; this standard was developed in the early days of electronic computing by IBM and used heavily on the 360-series of computers manufactured by IBM, Honeywell, Siemens and a few other companies. EBCDIC does not define the lowercase letters ‘a’ through ‘z’ and the corresponding uppercase correspondents ‘A’ through ‘Z’ contiguously. This poses an interesting, though only minor, challenge to compiler writers, wishing to devise portable compilers across computers from various manufacturers, and thus across multiple character standards. However, EBCDIC does map all 256 combinations in an 8-bit byte to a character value.