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

Similar documents
Data Representation 1

Number Systems. Both numbers are positive

COMP2611: Computer Organization. Data Representation

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Signed umbers. Sign/Magnitude otation

COMP Overview of Tutorial #2

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

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

Introduction to Computers and Programming. Numeric Values

Floating Point Arithmetic

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

CS & IT Conversions. Magnitude 10,000 1,

CMPSCI 145 MIDTERM #1 Solution Key. SPRING 2017 March 3, 2017 Professor William T. Verts

MACHINE LEVEL REPRESENTATION OF DATA

15213 Recitation 2: Floating Point

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

Bits, Words, and Integers

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

Final Labs and Tutors

Integers and Floating Point

COMP2121: Microprocessors and Interfacing. Number Systems

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

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

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

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

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Inf2C - Computer Systems Lecture 2 Data Representation

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

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

ECE232: Hardware Organization and Design

Numeric Encodings Prof. James L. Frankel Harvard University

CHAPTER 3 Expressions, Functions, Output

Computer System and programming in C

Floating Point Arithmetic

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Chapter 7. Basic Types

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

Integer Representation Floating point Representation Other data types

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Chapter 3: Arithmetic for Computers

8/30/2016. In Binary, We Have A Binary Point. ECE 120: Introduction to Computing. Fixed-Point Representations Support Fractions

Foundations of Computer Systems

CS 101: Computer Programming and Utilization

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

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

Floating Point Numbers

Floating Point Numbers

Chapter 2. Data Representation in Computer Systems

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

CHW 261: Logic Design

Chapter 3. Fundamental Data Types

World Inside a Computer is Binary

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

FLOATING POINT NUMBERS

Computer Systems Programming. Practice Midterm. Name:

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

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

Chapter 2 Data Representations

Basic data types. Building blocks of computation

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Binary Representations and Arithmetic

Set Theory in Computer Science. Binary Numbers. Base 10 Number. What is a Number? = Binary Number Example

Data Representation. DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Number Systems CHAPTER Positional Number Systems

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

Representation of Non Negative Integers

Data Representations & Arithmetic Operations

Chapter Three. Arithmetic

Floating Point January 24, 2008

IEEE Standard for Floating-Point Arithmetic: 754

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Integer Data Types. Data Type. Data Types. int, short int, long int

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

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

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

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

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

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

ISA 563 : Fundamentals of Systems Programming

Variables and Constants

Introduction to Scientific Computing Lecture 1

Module 2: Computer Arithmetic

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

CHAPTER 1 Numerical Representation

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Finite arithmetic and error analysis

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

M1 Computers and Data

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Transcription:

Appendix F Number Systems Binary Numbers Decimal notation represents numbers as powers of 10, for example 1729 1 103 7 102 2 101 9 100 decimal = + + + There is no particular reason for the choice of 10, except that several historical number systems were derived from people s counting with their fingers. Other number systems, using a base of 12, 20, or 60, have been used by various cultures throughout human history. However, computers use a number system with base 2 because it is far easier to build electronic components that work with two values, which can be represented by a current being either off or on, than it would be to represent 10 different values of electrical signals. A number written in base 2 is also called a binary number. For example, 1101 1 2 1 2 0 2 1 2 8 4 1 13 3 2 1 0 binary = + + + = + + =

2 APPENDIX F Number Systems For digits after the decimal point, use negative powers of 2. 1. 101 1 20 1 2 1 0 2 2 1 2 3 binary = + + + 1 1 = 1 + + 2 8 = 1 + 0. 5 + 0. 125 = 1. 625 In general, to convert a binary number into its decimal equivalent, simply evaluate the powers of 2 corresponding to digits with value 1, and add them up. Table 1 shows the first powers of 2. Table 1 Powers of Two Power Decimal Value 2 0 1 2 1 2 2 2 4 2 3 8 2 4 16 2 5 32 2 6 64 2 7 128 2 8 256 2 9 512 2 10 1,024 2 11 2,048 2 12 4,096 2 13 8,192 2 14 16,384 2 15 32,768 2 16 65,536

APPENDIX F Number Systems 3 To convert a decimal integer into its binary equivalent, keep dividing the integer by 2, keeping track of the remainders. Stop when the number is 0. Then write the remainders as a binary number, starting with the last one. For example, 100 2 = 50 remainder 0 50 2 = 25 remainder 0 25 2 = 12 remainder 1 12 2 = 6 remainder 0 6 2 = 3 remainder 0 3 2 = 1remainder 1 1 2 = 0 remainder 1 Therefore, 100decimal = 1100100binary. Conversely, to convert a fractional number less than 1 to its binary format, keep multiplying by 2. If the result is greater than 1, subtract 1. Stop when the number is 0. Then use the digits before the decimal points as the binary digits of the fractional part, starting with the first one. For example, 035. i 2 = 0. 7 07. i 2 = 1. 4 04. i 2 = 0. 8 08. i 2 = 1. 6 06. i 2 = 1. 2 02. i 2 = 0. 4 Here the pattern repeats. That is, the binary representation of 0.35 is 0.01 0110 0110 0110... To convert any floating-point number into binary, convert the whole part and the fractional part separately. Long, Short, Signed, and Unsigned Integers There are two important properties that characterize integer values in computers. These are the number of bits used in the representation, and whether the integers are considered to be signed or unsigned. Most computers you are likely to encounter use a 32-bit integer. However, the C++ language does not require this, and there have been machines that used 16-, 20-, 36-, or even 64-bit integers. There are times when it is useful to have integers of different sizes. The C++ language provides two modifiers that are used to declare such integers. A short int (or simply a short) is an integer that, on most

4 APPENDIX F Number Systems implementations, has fewer bits than an int. (The phrase on most implementations is necessary because the language definition only requires that a short integer have no more bits than a standard integer.) On most platforms that use a 32-bit integer, a short is 16 bits. At the other extreme are long integers. As you might expect, a long int (or simply a long) contains no fewer bits than a standard integer. At the present time most personal computers still use a 32-bit long, but processors that provide 64-bit longs have started to appear and will likely be more common in the future. A character (or char) is sometimes used as a very short (8-bit) integer. The C++ programmer therefore has the following hierarchy of integer sizes: Type char short int long Typical Size 8-bit 16-bit 32-bit 32- or 64-bit The sizeof operator can be used to tell how many bits your compiler assigns to each type. This operator takes a type as argument and returns the number of bytes each type requires. Multiplying the number of bytes by 8 will tell you the number of bits: cout << "Number of bytes for char " << sizeof(char) << " number of bits " << 8 * sizeof(char) << "\n"; cout << "Number of bytes for short " << sizeof(short) << " number of bits " << 8 * sizeof(short) << "\n"; cout << "Number of bytes for int " << sizeof(int) << " number of bits " << 8 * sizeof(int) << "\n"; cout << "Number of bytes for long " << sizeof(long) << " number of bits " << 8 * sizeof(long) << "\n"; If the only numbers you needed were positive, then the preceding discussion would be everything you needed to know. However, in most applications it is more useful to allow both positive and negative values, and so a more complicated encoding is necessary. This characteristic of an integer is declared using the modifiers signed and unsigned. An unsigned integer holds only positive values. An unsigned short int that is represented using 16 bits can maintain the values between 0 and 65,535 (that is, between zero and 2 16 1). A 32-bit unsigned int can represent values between 0 and 4,294,967,295. If no modifier is provided, an integer is assumed to be signed. Allowing both positive and negative values requires changing the representation of an integer value. The details of this representation are described in the next section. However, an important feature is that allowing both positive and negative numbers requires setting aside one bit (the so-called sign bit) to indicate whether the number is positive or negative. This reduces the largest value that can be

APPENDIX F Number Systems 5 represented. The following table shows the range of values that can be represented using signed and unsigned integers of 8, 16, 32, and 64 bits. Integer Type Range of Values 8-bit signed 128 to 127 8-bit unsigned 0 to 255 16-bit signed 32,768 to 32,767 16-bit unsigned 0 to 65,535 32-bit signed 2,147,483,648 to 2,147,483,647 32-bit unsigned 0 to 4,294,967,295 64-bit signed 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 64-bit unsigned 0 to 18,446,744,073,709,551,615 Two s Complement Integers To represent negative integers, there are two common representations, called signed magnitude and two s complement. Signed magnitude notation is simple: use the leftmost bit for the sign (0 = positive, 1 = negative). For example, when using 8-bit numbers, 13 = 10001101 signed magnitude However, building circuitry for adding numbers gets a bit more complicated when one has to take a sign bit into account. The two s complement representation solves this problem. To form the two s complement of a number, Flip all bits. Then add 1. For example, to compute 13 as an 8-bit value, first flip all bits of 00001101 to get 11110010. Then add 1: 13 = 11110011 two s complement Now no special circuitry is required for adding two numbers. Just follow the normal rule for addition, with a carry to the next position if the sum of the digits and the prior carry is 2 or 3.

6 APPENDIX F Number Systems For example, 1 1111 111 +13 0000 1101-13 1111 0011 1 0000 0000 But only the last 8 bits count, so +13 and 13 add up to 0, as they should. In particular, 1 has two s complement representation 1111... 1111, with all bits set. The leftmost bit of a two s complement number is 0 if the number is positive or zero, 1 if it is negative. Two s complement notation with a given number of bits can represent one more negative number than positive numbers. For example, the 8-bit two s complement numbers range from 128 to +127. This phenomenon is an occasional cause for a programming error. For example, consider the following code: short b =...; if (b < 0) b = -b; This code does not guarantee that b is nonnegative afterwards. If short values are 16 bits and b happens to be 32,768, then computing its negative again yields 32,768. (Try it out take 100... 00 (15 zeros), flip all bits, and add 1.) IEEE Floating-Point Numbers The Institute for Electrical and Electronics Engineering (IEEE) defines standards for floating-point representations in the IEEE-754 standard. Figure 1 shows how single-precision (float) and double-precision (double) values are decomposed into A sign bit An exponent A mantissa 1 bit 8 bit 23 bit sign biased exponent e + 127 mantissa (without leading 1) Single Precision 1 bit 11 bit 52 bit sign biased exponent e + 1023 mantissa (without leading 1) Double Precision Figure 1 IEEE Floating-Point Representation

APPENDIX F Number Systems 7 Floating-point numbers use scientific notation, in which a number is represented as b b b b e 0. 1 2 3 2 In this representation, e is the exponent, and the digits b0. b1b2b3 form the mantissa. The normalized representation is the one where b 0 0. For example, 100 = 1100100 = 1. 100100 2 6 decimal binary binary Because in the binary number system the first bit of a normalized representation must be 1, it is not actually stored in the mantissa. Therefore, you always need to add it on to represent the actual value. For example, the mantissa 1.100100 is stored as 100100. The exponent part of the IEEE representation uses neither signed magnitude nor two s complement representation. Instead, a bias is added to the actual exponent. The bias is 127 for single-precision numbers and 1023 for double-precision numbers. For example, the exponent e = 6 would be stored as 133 in a single-precision number. Thus, 100 decimal = 0 10000101 10010000000000000000000 single-precision IEEE In addition, there are several special values. Among them are: Zero: biased exponent = 0, mantissa = 0. Infinity: biased exponent = 11...1, mantissa = ±0. NaN (not a number): biased exponent = 11...1, mantissa ±0.

8 APPENDIX F Number Systems Hexadecimal Numbers Because binary numbers can be hard to read for humans, programmers often use the hexadecimal number system, with base 16. The digits are denoted as 0, 1,, 9, A, B, C, D, E, F. (See Table 2.) Four binary digits correspond to one hexadecimal digit. That makes it easy to convert between binary and hexadecimal values. For example, 11 1011 0001 binary = 3B1 hexadecimal In C++, hexadecimal integers are denoted with a 0x prefix, such as 0x3B1. Table 2 Hexadecimal Digits Hexadecimal Decimal Binary 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111