Chapter 2 Data and Number Representations

Slides:



Advertisements
Similar presentations
1 Chapter 2 The Digital World. 2 Digital Data Representation.
Advertisements

3.1 3 Data Storage Foundations of Computer Science  Cengage Learning.
Data Representation COE 202 Digital Logic Design Dr. Aiman El-Maleh
The Binary Numbering Systems
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
Digital Fundamentals Floyd Chapter 2 Tenth Edition
Data Representation Computer Organization &
Connecting with Computer Science, 2e
Assembly Language and Computer Architecture Using C++ and Java
Chapter 4 Operations on Bits
Assembly Language and Computer Architecture Using C++ and Java
Binary Expression Numbers & Text CS 105 Binary Representation At the fundamental hardware level, a modern computer can only distinguish between two values,
Number Representation Rizwan Rehman, CCS, DU. Convert a number from decimal to binary notation and vice versa. Understand the different representations.
IT-101 Section 001 Lecture #4 Introduction to Information Technology.
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
Chapter 5 Data representation.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
Chapter 2 Data Representation. Define data types. Visualize how data are stored inside a computer. Understand the differences between text, numbers, images,
© 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
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
Numbering systems.
Chapter 3 Number Representation. Convert a number from decimal 、 hexadecimal,octal to binary notation and vice versa. Understand the different representations.
Computers Organization & Assembly Language
Logic and Digital System Design - CS 303
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Data Representation.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Number Systems Spring Semester 2013Programming and Data Structure1.
Foundations of Computer Science Chapter 4 Operations on Bits.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 3 Number Representation. Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer.
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Data Representation, Number Systems and Base Conversions
Computer Math CPS120 Introduction to Computer Science Lecture 4.
DATA REPRESENTATION CHAPTER DATA TYPES Different types of data (Fig. 2.1) The computer industry uses the term “MULTIMEDIA” to define information.
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
Data Representation. What is data? Data is information that has been translated into a form that is more convenient to process As information take different.
Data Representation.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
BINARY SYSTEMS ENGR. KASHIF SHAHZAD 1. BINARY NUMBERS 1/2 Internally, information in digital systems is of binary form groups of bits (i.e. binary numbers)
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.
Lec 3: Data Representation
Chapter 4 Operations on Bits.
3.1 Denary, Binary and Hexadecimal Number Systems
Chapter 3 Data Storage.
Data Structures Mohammed Thajeel To the second year students
Chapter 2 Bits, Data Types & Operations Integer Representation
Ch2: Data Representation
Information Representation
Chapter 2 Data Representation.
Number Representation
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Data Representation Chapter 2 Computer HW (Von Neumann Model) Program
Chapter Four Data Representation in Computers By Bezawit E.
OBJECTIVES After reading this chapter, the reader should be able to :
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Presentation transcript:

Chapter 2 Data and Number Representations

Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent Different Types of Data Number Representation (chapter3) Number Systems Conversion Between Number Systems Integer Representation Floating-point Representation Operations on Bits (chapter4) Arithmetic Operations Logical Operations

Objectives Differentiate between data and information. Explain how text, images, audio and video are represented in computers. Explain decimal notation, binary notation, hexadecimal notation, and octal notation. Apply conversions from one number system to another.

Objectives Explain how integers are stored in computers (sign-and-magnitude, one’s complement, two’s complement). Explain how the Excess system works. Explain how to represent a floating-point number in computers. Apply bit operations such as arithmetic operations, logical operations, and shift operations.

Part 1 Data Representation

Overview What’s data? Data refers to the symbols that a computer uses to represent facts (such as people, events and things) and ideas. What’s information? The words, numbers, and graphics used as the basis for human actions and decisions. The difference between data and information: Data becomes information when it is presented in a format that people can understand and use.

Graphics Data Information Table

Overview (con.) Data entered into a computer is called input. The processed information are called output. The cycle of input, process, output, and storage is called the information processing cycle. Data information

Data Text Number Image Audio Video Overview (con.) There are several types of data. Data Text Number Image Audio Video The computer industry uses the term “multimedia” to define information that contains numbers, text, images, audio, and video. Note:

Question : How do you handle all these data types (text, number, image, audio and video)? 1234…890….

Solution The most efficient solution is to use a uniform representation of data. All data types from outside a computer are transformed into this uniform representation when stored in a computer and then transformed back when leaving the computer. This universal format is called a bit pattern.

Digital Data Representation Digital device works with discrete, distinct data or digits, such as 1 and 0. Analog device works with continuous data. Digital Signals Analog Most computers are digital computers because the digital is a relatively simple, dependable, and adaptable technology.

Some important concepts Bit– short for binary digit, is the smallest unit of data that can be stored in a computer. In the binary system, each 0 and 1 is called a bit. In a two-state on/off arrangement (such as a switch), one state can represent a 1 digit (on), the other represents a 0 digit (off). Computers use sequences of bits (bit pattern) to represent all kinds of data. Bit: binary digit 二进制位

Some important concepts (con.) Bit pattern is a sequence or a string of bits. It is the responsibility of I/O devices or programs to interpret a bit pattern as a number, text, or some type of data. 1 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1 Byte is a kind of bit pattern. Its length is 8 bits. 1 byte = 8 bits

Data Representation (con.) How to represent different types of data: Number Representation Text Representation Image Representation Audio Representation In a computer, numbers are represented using the binary system. Note:

A piece of text in any language is a sequence of symbols. Text Representation A piece of text in any language is a sequence of symbols. Symbol examples: 26 uppercase letters (A, B, C … Z) 26 lowercase letters (a, b, c …z) 10 numeric characters (0, 1, 2 … 9) Others (? ; blank, newline, and tab….) In a bit pattern, the number of bits to represent a symbol depends on how many symbols are in the set. --- More symbols mean a longer bit pattern.

Text Representation (con.)

Text Representation (con.) The relationship between the length of the bit pattern and the number of symbols is logarithmic. For example: If you need 4 symbols, the length is 2 bits. log24 =2 The forms are: 00, 01, 10 and 11 Number of symbols Bit pattern length 2 1 16 4 128 7 ……. 65,536

Text Representation (con.) Different sets of bit patterns have been designed to represent text symbols. Each set is called a code, or code scheme. There are some common codes: ASCII Extended ASCII EBCDIC Unicode ISO 美国信息交换标准码: American Standerd code for Information Interchange 扩充的二进制编码的十进制交换码 Extended Binary Coded Decimal Interchange Code

ASCII Code Popular Code Schemes Stands for American Standard Code for Information Interchange. It was developed by ANSI (American National Standards Institute). It is used on most microcomputers, many minicomputers, and some mainframe computers. This code uses 7 bits for each symbol, which means 128 (27) different symbols can be defined. ASCII is pronounced “ASK ee”.

Features of ASCII code : Popular Code Schemes Features of ASCII code : Range of 7-bit pattern is from 0000000 to 1111111. The first pattern (000 0000) represents the null character. The last pattern (111 1111) represents the delete character. There are 31 control characters. …

A-65 a-97

It uses 8-bits pattern to represent 256 characters. Popular Code Schemes Extended ASCII Code To make the size of each pattern 1 byte (8 bits) , the ASCII bit patterns are augmented with an extra 0 at the left. The code is from 00000000 to 01111111. The left bit (0) is extended bit. EBCDIC Code Stands for Extended Binary Coded Decimal Interchange Code. It was commonly used in IBM mainframes. It uses 8-bits pattern to represent 256 characters. 扩充的二进制编码的十进制交换码 Extended Binary Coded Decimal Interchange Code

Popular Code Schemes Unicode Although previous codes can handle English and European languages well, it cannot handle all the characters of some other languages, such as Chinese and Japanese. Unicode, which was developed to deal with such languages, uses 2 bytes (16 bits) to handle 65 536 characters. ISO Code It is developed by ISO (International Organization for Standardization), which uses 4 bytes (32 bits) to handle 4 294 967 296 (232) characters. 我国文字从古代的象形文字发展到今天的汉字,中间是经过多次变化和改革的;但是历代的改革几乎都是兼收并蓄的,以致使汉字越积越多。当前我国文字之多是非常惊人的,比较常用的《新华字典》就有10000多个汉字;1990年出版的《辞海》有14872个汉字;1716年编撰的《康熙字典》有47035个汉字;郭沫若生前曾根据日本朋友的估计,我国大约有60000多个汉字;1994年出版的《中华字海》有87019个汉字(其中重复字320个);近来,根据北京国安资讯设备公司汉字库公布,共收入有出处的汉字91251个汉字。其中一级适用汉字21303个,包括国家标准提出的汉字、我国古今近1200个姓氏6699个汉字和当前乡村一级的地名专用字;二级适用汉字36581个,包括古今权威字典中音、义具全的汉字和十三经、二十四史等的古籍用字;三级备用汉字33367个,包括罕用字、异形字、音义未详字等。尚不包括杂书野史和各地地方志、名人笔记等自造汉字;还有正、草、隶、篆不同写法的异体字、同义字等。单据《说文解字》中就有别体字147个。有人估计实际汉字约在10万个以上;也有人估计在120000个以上;我国究竟有多少汉字至今尚没有定论。

Image Image Representation Bitmap Vector Video Bitmap: --- an image is divided into a matrix of pixels. --- each pixel is assigned a bit pattern. --- to represent a black and white image, 1 represents white pixel and 0 represents black one. --- to represent a color image, a pixel is decomposed into three colors (RGB).

Image Representation (con.)

Image Representation (con.)

Image Representation (con.) For a color image, each pixel has three bit patterns: one to represent the intensity of the red color, one to represent the intensity of the green color, and one to represent the intensity of the blue color.

Image Representation (con.) Vector: ---an image is decomposed into a combination of curves and lines. ---each curve or line is represented by a mathematical formula. ---For example, a line may be described by the coordinates of its endpoints, and a circle may be described by the coordinates of its center and the length of its radius. ---they use much less storage space than bitmap images, but do not look as realistic as bitmap images.

Image Representation (con.)

Image Representation (con.) Video: ---is composed of a series of frames. A movie is a series of frames shown one after another to create the illusion of motion. ---each frame could be stored as a bitmap. ---a digital video requires tremendous storage capacity. ---Refer to chapter 15 for details about video compression.

---Audio is a representation of sound or music. Audio Representation ---Audio is a representation of sound or music. ---music, voice and sound effects can all be recorded as waveform, which is by nature analog data. ---samples of the sound are collected as periodic intervals and stored as numeric data.

Audio Representation (con.) The steps to change audio data to bit patterns: The analog data is sampled. Sampling means measuring the value of the signal at equal intervals. The samples are quantized. Quantization means assigning a value to a sample. The quantized values are changed to binary patterns. The binary patterns are stored.

Audio Representation (con.)

Audio Representation (con.)

Number Representation Part 2 Number Representation

Some essential concepts Number system: any system of naming or representing numbers, also called number representation system or numeration system. Carry: happens when the sum or product of two or more digits equals or exceeds the base of the number system.

Base: the number of digits in a number system. Some essential concepts Base: the number of digits in a number system. Position value: the value associated with each digit place, also called radix, weight or positional value. --For example: for the decimal number 125, the position values associated with the character 1,2,5 are respectively 102,101,100, therefore, (125)10=1×102+2×101+5×100.

Question: how do we compare these two numbers, 25. 6 and 52 Question: how do we compare these two numbers, 25.6 and 52.6, in the decimal system? (25.6)10 = 2 × 101 + 5 × 100 + 6 × 10-1 (52.6)10 = 5 × 101 + 2 × 100 + 6 × 10-1 (N)R =  Ki  Ri Ki{0,1,……,R-1} n i=-m n = number of digits of the integer - 1 m = number of digits of the decimal fraction R = base Ri = Position values

Different Number Systems Binary System (Base 2): consists of 2 digits (symbols). Octal System (Base 8): consists of 8 digits. Decimal System (Base 10): consists of 10 digits. Hexadecimal System (Base 16): consists of 16 digits. 0 1 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 A B C D E F

Question: how do we tell which number system we are using? Subscript method: (101)2 (101) 8 (101) 10 (101) 16 Binary Octal Decimal Hexadecimal 101B 101O 101D 101H Prefix method: Postfix method:

243 Binary System Its base is 2. It has only two digits,1 and 0. There will be a carry when the result equals 2. Position Value Table 243

Binary Position Value Table Position Power Decimal Value Binary Value 216 65536 10000 0000 0000 0000 … 210 1024 100 0000 0000 29 512 10 0000 0000 28 256 1 0000 0000 27 128 1000 0000 26 64 100 0000 25 32 10 0000 24 16 1 0000 23 8 1000 22 4 100 21 2 10 20 1

How to represent (16)10 in the 16-bit system?? Decimal Binary Binary (16-bit system) 0000 0000 0000 0000 1 0000 0000 0000 0001 2 10 0000 0000 0000 0010 3 11 0000 0000 0000 0011 4 100 0000 0000 0000 0100 5 101 0000 0000 0000 0101 6 110 0000 0000 0000 0110 7 111 0000 0000 0000 0111 8 1000 0000 0000 0000 1000 9 1001 0000 0000 0000 1001 1010 0000 0000 0000 1010 1011 0000 0000 0000 1011 12 1100 0000 0000 0000 1100 13 1101 0000 0000 0000 1101 14 1110 0000 0000 0000 1110 15 1111 0000 0000 0000 1111 How to represent (16)10 in the 16-bit system??

How computer capacity is expressed: bit by bit The following terms are used to denote capacity: Bit: In the binary system, the binary digit (bit) –0 or 1—is the smallest unit of measurement. Byte: A group of 8 bits is called a byte, and a byte represents one character, digit, or other value. Kilobyte: A kilobyte(K, KB) is about 1000 bytes. (Actually, it's precisely 1024, that is 210bytes, but the figure is commonly rounded.)

How computer capacity is expressed: bit by bit Megabyte: A megabyte (M, MB) is about 1 million bytes (220). Gigabyte: A gigabyte (G, GB) is about 1 billion bytes (230). Terabyte: A terabyte (T, TB) represents about 1 trillion bytes (240). Petabyte(250): A new measurement accommodates the huge storage capacities of modern databases—a petabyte represents about 1 million gigabytes! 实际设备的M和理论上的M有什么区别?

Octal System Bit Pattern Oct Digit 000 100 4 001 1 101 5 010 2 110 6 Its base is 8. It has eight digits: 0, 1, 2, 3, 4, 5, 6 and 7. There will be a carry when the result equals 8. A 3-bit pattern can be represented by an octal digit, and vice versa. Bit Pattern Oct Digit 000 100 4 001 1 101 5 010 2 110 6 011 3 111 7

Decimal System Nowadays, there are two dominant number systems in the world: decimal system and binary system. It has ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

Hexadecimal System Its base is 16. It has sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. A 4-bit pattern can be represented by a hexadecimal digit, and vice versa. Bit Pattern Hex Digit 0000 1000 8 0001 1 1001 9 0010 2 1010 10 0011 3 1011 11 0100 4 1100 12 0101 5 1101 13 0110 6 1110 14 0111 7 1111 15

Decimal Binary Octal Hexadecimal 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 1010 12 A 1011 13 B 1100 14 C 1101 15 D 1110 16 E 1111 17 F

Conversion between number systems Binary to Decimal Decimal to Binary Binary to Octal Octal to Binary Binary to Hexadecimal Hexadecimal to Binary

Binary to Decimal Conversion Solution: Step 1: multiply each binary digit by its corresponding position value. Step 2: add all multiplication results together to get the decimal number. Start from 0 0 1 0 1 1 0 1 Binary Number Position 6 5 4 3 2 1 0 Position Value 26 25 24 23 22 21 20 0×26 + 1×25 +0×24 + 1×23 + 1×22 + 0×21 + 1×20 0 + 32 + 0 + 8 + 4 + 0 + 1 = ( 45 )10 Result

Decimal to Binary Conversion Solution: Step 1: divide the number by 2 and write the quotient and remainder. Step 2: use the remainder (from step 1) as the corresponding binary digit (from right to left), 1 or 0 . Step 3: check whether the quotient is 0 or not. If it is zero, skip to the Step 4; otherwise, use the quotient as the number and go back to Step 1. Step 4: stop and put all remainders together to get the binary number.

Example: Convert the decimal number 35 to binary. Stop when the quotient is 0 Quotient Decimal Number 1 2 4 8 17 35 1 1 1 Remainder Binary Number : 100011

Questions: What if the number has a fraction part? How do we convert octal to decimal, and decimal to octal? How do we convert hexadecimal to decimal, and decimal to hexadecimal?

Binary to Octal Conversion Solution: Step 1: Organize the pattern into groups of 3 (from right to left). Step 2: Transform each group into an octal digit. 1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0 1 7 6 3 4 4 If the leftmost bit pattern does not contain 3 digits, add extra 0s to the left.

Octal to Binary Conversion Solution: Transform each octal digit into a 3-bit binary pattern. Octal Number Octal Number 1 4 2 5 6 2 001 100 010 101 110 010 Binary Number Binary Number

Step 2: Transform each group into a hexadecimal digit. Binary to Hexadecimal Solution: Step 1: Organize the bit pattern into groups of 4 (from right to left). Step 2: Transform each group into a hexadecimal digit. 1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0 F C E 4 If the leftmost bit pattern does not contain 3 digits, add extra 0s to the left.

Transform each hexadecimal digit into a 4-bit binary pattern. Hexadecimal to Binary Solution: Transform each hexadecimal digit into a 4-bit binary pattern. Hexadecimal Number 2 4 C 0010 0100 1100 Binary Number

Exercises (10 111 011.110 1)2 = (?)8 = (273.64)8 (6754.32)8 = (?)2 = (110 111 101 100. 011 010)2 (1011 1110 0110. 1101 1)2 = (?)16 = (BE6.D8)16 (A7B8.C9)16 = (?)2 = (1010 0111 1011 1000. 1100 1001)2

Integer Representation Integers are the whole numbers, which include positive integers and negative integers. The figure below shows different integers. Integers Unsigned Signed Sign-and- Magnitude One’s Complement Two’s Complement

Unsigned Integers Range of unsigned integers: 0 ~ ( 2N-1 ) ---N is the number of bits the computer allocates to store an unsigned integer. ---For example: 8 -bit computer : 0 ~ 255 16-bit computer : 0 ~ 65,535 How to store an unsigned integer: Step 1: Convert the number to binary. Step 2: If the number of bits is less than N, 0s are added to the left of the binary number so that there is a total of N bits.

Unsigned integers in two different computers Add zeros to the left when needed Decimal 8-bit Allocation 16-bit Allocation 7 234 258 24,760 1,245,678 0000 0111 0000 0000 0000 0111 1110 1010 0000 0000 1110 1010 overflow 0000 0001 0000 0010 overflow 0110 0000 1011 1000 overflow overflow Unsigned integers in two different computers Exceeds the capacity of the storage

Signed Integers Sign-and-Magnitude Representation The leftmost bit defines the sign of the number : 0 for positive, and 1 for negative. ---For example: in an 8-bit allocation, the leftmost bit shows the sign and the other seven bits represent the absolute value. Range of signed integers: -(2N-1-1) ~ +( 2N-1-1) ---N is the number of bits allocated to represent one sign-and-magnitude integer.

Note: There are two 0s in sign-and-magnitude representation: positive zero (+0) and negative zero (-0). ---For example: In an 8-bit allocation, +0  00000000 -0  10000000 How to store a sign-and-magnitude integer: Step 1: Convert the number to binary, the sign is ignored. Step 2: If the number of bits is less than N-1, 0s are added to the left of the number so that there is a total of N-1 bits. Step 3: Add sign-bit to the left ( to make it N bits ) : if it’s positive, add 0; if negative, add 1.

Example: Store +7 in an 8-bit memory location, and store –258 in a 16-bit memory location using sign-and-magnitude representation . +7 Question: how about +7 in a 16-bit allocation, and -258 in an 8-bit allocation? 1 1 1 1 1 1 1 1 1 -258 1 1 1 1 1 1 1

Signed Integers One’s Complement Representation To represent a positive number, use the convention adopted for an unsigned integer. To represent a negative number, complement the positive number. In one’s complement, the complement of a number is obtained by changing all 0s to 1s and all 1s to 0s. The leftmost bit defines the sign of the number : 0 for positive, and 1 denotes negative. Range of signed integers: -(2N-1-1) ~ +( 2N-1-1) ---N is the number of bits allocated to represent a one’s complement integer.

Note: There are two 0s in one’s complement representation, too: positive zero (+0) and negative zero (-0). ---For example: In an 8-bit allocation, +0  00000000 -0  11111111 How to store a one’s complement integer: Step 1: Convert the number to binary, the sign is ignored. Step 2: 0s are added to the left of the number to make a total of N bits. Step 3: If the sign is positive, no action is needed. If negative, every bit is complemented.

Example: Store +7 and -7 in an 8-bit memory location, and store –258 in a 16-bit memory location using one’s complement representation . +7 -7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -258 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Note: One’s complementing means reversing all bits. If you one’s complement a positive number, you get the corresponding negative number. If you one’s complement a negative number, you get the corresponding positive number. If you one’s complement a number twice, you get the original number.

Signed Integers Two’s Complement : Range of signed integers: -(2N-1) ~ +( 2N-1-1) --- N is the number of bits allocated to represent a two’s complement integer. How to store a two’s complement integer: Step 1: Convert the number to binary, ignore the sign. Step 2: 0s are added to the left of the number to make a total of N bits. Step 3: If the sign is positive, no action is needed. If negative, leave all the rightmost 0s and the first 1 unchanged. Complement the rest of the bits. Two’s complement is the most common, the most important, and the most widely used representation of integers today.

Two’s complement =one’s complement + 1 Example: Store +7 in an 8-bit memory location, and store –40 in a 16-bit memory location using two’s complement representation . +7 1 1 1 1 1 1 -40 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Note: For negative numbers: Two’s complement =one’s complement + 1

Note: In two’s complement representation, the leftmost bit defines the sign of the number. If it is 0, the number is positive. If it is 1, the number is negative. There is only one 0 in two’s complement: For example: in an 8-bit allocation, 0  00000000 If you two’s complement a positive number, you get the corresponding negative number. If you two’s complement a negative number, you get the corresponding positive number. If you two’s complement a number twice, you get the original number.

Summary of integer representation Contents of Memory ------------ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Unsigned ------------ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Sign-and-Magnitude --------- +0 +1 +2 +3 +4 +5 +6 +7 -0 -1 -2 -3 -4 -5 -6 -7 One’s Complement --------- +0 +1 +2 +3 +4 +5 +6 +7 -7 -6 -5 -4 -3 -2 -1 -0 Two’s Complement -------- +0 +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1

Excess system In an excess conversion, a positive number, called the magic number, is used in the conversion process. The magic number is normally (2N-1) or ( 2N-1-1), where N is the bit allocation. For example, if N is 8, the magic number is either 128 (Excess_128) or 127(Excess_127). To represent a number in Excess, use the following procedure: Step 1: Add the magic number to the integer. Step 2: Change the result to binary and add 0s to make a total of N bits.

Example: Represent –25 in Excess 127 using an 8-bit allocation. Step 1: Add 127 to –25 and get 102. Step 2: Change the result to binary, which is 1100110. Step 3: Add one 0 to make a total of 8 bits. The representation is 01100110

See Decimal to binary Conversion Repetitive Multiplication Method Floating-point Representation Fraction Part Integer Part 1 4 . 2 3 4 1 4 2 3 4 See Decimal to binary Conversion To convert a floating-point number to binary: Step 1: Convert the integer part to binary. Step 2: Convert the fraction part to binary. Step 3: Put decimal point between the two parts. Repetitive Multiplication Method

Changing fractions to binary Multiply by 2 to get integer part Multiply 2 to gain integer part Stop when the result is 0 0.125 0.250 0.500 1.000 0.000 0 . 1 Transform the fraction 0.4 to a binary of 6 bits? 0.4 0.8 1.6 1.2 0.4 0.8 1.6 0 . 1 1 1

Example: transform the number 49 Example: transform the number 49.58 to a binary ( 3 bits after the decimal point)? ( 49 . 58 )10 = ( . )2 110001 100 Integer Part 0.58  2 1 .16 0 .32 0 .64 2 49 2 24 ---- 1 2 12 ---- 0 2 6 ---- 0 2 3 ---- 0 2 1 ---- 1 0 ---- 1 Fraction Part

(Excess representation) Floating-point Representation Normalization: the moving of the decimal point so that there is only one ‘1’ to the left of the decimal point (1. XXXXXXXXX) . 6 digits + 26×1.01000111001 + 1010001 . 11001 3 digits - 2-3×1.110011 - 0 . 001110011 ± 2e × 1.XXXXXXX Sign (1 bit) Exponent (Excess representation) Mantissa (unsigned integer)

Single-precision format Double-precision format Floating-point Representation IEEE Standard: Single-precision format 4 bytes = 32 bits sign 1 8 23 exponent mantissa 1 11 52 Double-precision format 8 bytes = 64 bits

Floating-point Representation IEEE Standard:

Example: Show the representation of the normalized number: + 26 × 1 The sign is positive. The Excess_127 representation of the exponent is 133. You add extra 0s on the right to make it 23 bits. The number in memory is stored as: 0 10000101 01000111001000000000000

Example: Interpret the following 32-bit floating-point number 1 01111100 11001100000000000000000 The sign is negative. The exponent is –3 (124 – 127). The number after normalization is -2-3 × 1.110011

Quiz The decimal equivalent of the binary number 11.11 is . Show the following number in 32-bit IEEE format: . -2-5×1.01101001 Suppose the following bit pattern represents a value stored in two’s complement notation. Find the two’s complement representation of the negative of the value. 01010101

Part 3 Operations on Bits

Bit Operations Arithmetic Logical Shift

Arithmetic operations Arithmetic operations involve adding, subtracting, multiplying, dividing, and so on. You can apply these operations to integers and floating-point numbers. The multiplication operation can be implemented in software using repeated addition or in hardware using other techniques. The division operation can also be implemented in software using repeated subtraction or in hardware using other techniques.

Arithmetic Operations Table: adding bits Number of 1s Result Carry 1 2 3 Rule of Adding Integers in Two’s Complement Add 2 bits and propagate the carry to the next column. If there is a final carry after the leftmost column addition, discard it.

? + + ? (+24) + (-17) = +7 (+127) + (+3) = +130 1 1 1 1 1 Example: add the following numbers in two’s complement representation: (+24) + (-17) ; (+127) + (+3) . ? (+24) + (-17) = +7 (+127) + (+3) = +130 1 1 1 1 1 Carry 1 1 1 1 1 1 1 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 + + 1 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 Result 1 0 0 0 0 0 1 0 discard ? What’s wrong??? Data overflow !!! (-126)10

(-1) + 0 = -1 - (2N-1) --- 0 --- +(2N-1 –1) 127+3= -126 127+1= -128 1 1 1 1 , 1 1 1 1 Overflow (-1) + 0 = -1 Overflow will happen when the value exceeds the range of the allocation. Range of numbers in two’s complement representation: - (2N-1) --- 0 --- +(2N-1 –1) 127+3= -126 127+1= -128 1 0 0 0 , 0 0 1 0 1 0 0 0 , 0 0 0 0

Note: When you do arithmetic operations on numbers in a computer, remember that each number and the result should be in the range defined by the bit allocation. To subtract, negate (two’s complement) the second number and add. For example: (+101) - (+62)  (+101) + (-62) Carry 1 1 0 1 1 0 0 1 0 1 + 1 1 0 0 0 0 1 0 ---------------------------------- Result 0 0 1 0 0 1 1 1  39 The leftmost carry is discarded.

Addition and subtraction for floating-point numbers Check the signs. If different, use the sign of the number whose absolute value is larger. Move the decimal point to make the exponents the same. Add or subtract the mantissas (including the integer part and fraction part). Normalize the result before storing in memory. Check for overflow.

Example Add two floats: 0 10000100 10110000000000000000000 0 10000010 01100000000000000000000 Solution The exponents are 5 and 3. The numbers are: +25 × 1.1011 and +23 × 1.011 Make the exponents the same. (+25 × 1.1011)+ (+25× 0.01011)  +25 × 10.00001 After normalization +26 × 1.000001, which is stored as: 0 10000101 000001000000000000000000

True False It’s a cat , not a tiger ! It’s a red tulip ! Logical Operations Some essential concepts It’s a cat , not a tiger ! True It’s a red tulip ! False Each logical variable can only be True or False. In the computer system, “1” means “True”, “0” means “False”.

Some essential concepts According to the number of operands they take, operators can be categorized as: unary, binary and Ternary. Unary Input Output Binary Input Output

Some essential concepts Logical Operations Unary NOT Binary AND OR XOR

Some essential concepts A truth table shows all the possible input combinations of input with the corresponding output. x y x AND y 1 x NOT x 1 x y x OR y 1 x y x XOR y 1

NOT operator The NOT operator has one input. It inverts bits; that is, it changes 0 to 1 and 1 to 0.

Example Use the NOT operator on the bit pattern 10011000 Solution Target 1 0 0 1 1 0 0 0 NOT ------------------ Result 0 1 1 0 0 1 1 1

AND operator

Example Use the AND operator on bit patterns 10011000 and 00110101. Solution Target 1 0 0 1 1 0 0 0 AND 0 0 1 1 0 1 0 1 ------------------ Result 0 0 0 1 0 0 0 0

Inherent rule of the AND operator If a bit in one input is 0, you can quickly conclude that the result is 0.

OR operator

Example Use the OR operator on bit patterns 10011000 and 00110101 Solution Target 1 0 0 1 1 0 0 0 OR 0 0 1 1 0 1 0 1 ------------------ Result 1 0 1 1 1 1 0 1

Inherent rule of the OR operator If a bit in one input is 1, you can quickly conclude that the result is 1.

XOR operator

Example Use the XOR operator on bit patterns 10011000 and 00110101. Solution Target 1 0 0 1 1 0 0 0 XOR 0 0 1 1 0 1 0 1 ------------------ Result 1 0 1 0 1 1 0 1

Inherent rule of the XOR operator If a bit in one input is 1, the result is the inverse of the corresponding bit in the other input.

Mask A mask is a special bit pattern used to modify another bit pattern. We can use masks to unset, set, or reverse specific bits.

Example of unsetting specific bits

Example Use a mask to unset (clear) the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution The mask is 00000111. Target 1 0 1 0 0 1 1 0 AND Mask 0 0 0 0 0 1 1 1 ------------------ Result 0 0 0 0 0 1 1 0

Example Imagine a power plant that pumps water to a city using eight pumps. The state of the pumps (on or off) can be represented by an 8-bit pattern. For example, the pattern 11000111 shows that pumps 1 to 3 (from the right), 7 and 8 are on while pumps 4, 5, and 6 are off. Now assume pump 7 shuts down. How can a mask show this situation?

Solution Use the mask 10111111 to AND with the target pattern. The only 0 bit (bit 7) in the mask turns off the seventh bit in the target. Target 1 1 0 0 0 1 1 1 AND Mask 1 0 1 1 1 1 1 1 ------------------ Result 1 0 0 0 0 1 1 1

Example of setting specific bits

Example Use a mask to set the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution The mask is 11111000. Target 1 0 1 0 0 1 1 0 OR Mask 1 1 1 1 1 0 0 0 ------------------ Result 1 1 1 1 1 1 1 0

Example Using the power plant example, how can you use a mask to to show that pump 6 is now turned on? Solution Use the mask 00100000. Target 1 0 0 0 0 1 1 1 OR Mask 0 0 1 0 0 0 0 0 ------------------ Result 1 0 1 0 0 1 1 1

Example of flipping specific bits

Example Use a mask to flip the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution Target 1 0 1 0 0 1 1 0 XOR Mask 1 1 1 1 1 0 0 0 ------------------ Result 0 1 0 1 1 1 1 0

Shift operations If a bit pattern can be shifted to the right or to the left.

Example Show how you can divide or multiply a number by 2 using shift operations. Solution If a bit pattern represents an unsigned number, a right-shift operation divides the number by two. The pattern 00111011 represents 59. When you shift the number to the right, you get 00011101, which is 29. If you shift the original number to the left, you get 01110110, which is 118.

Example Use a combination of logical and shift operations to find the value (0 or 1) of the fourth bit (from the right). Solution Use the mask 00001000 to AND with the target to keep the fourth bit and clear the rest of the bits.

Solution (continued) Target a b c d e f g h AND Mask 0 0 0 0 1 0 0 0 ------------------ Result 0 0 0 0 e 0 0 0 Shift the new pattern three times to the right 0000e000  00000e00  000000e0  0000000e Now it is easy to test the value of the new pattern as an unsigned integer. If the value is 1, the original bit was 1; otherwise the original bit was 0.

Objectives Differentiate between data and information. Explain how text, images, audio and video are represented in computers. Explain decimal notation, binary notation, hexadecimal notation, and octal notation. Apply conversions from one number system to another.

Objectives Explain how integers are stored in computers (sign-and-magnitude, one’s complement, two’s complement). Explain how the Excess system works. Explain how to represent a floating-point number in computers. Apply bit operations such as arithmetic operations, logical operations, and shift operations.

That’s all for this chapter!