Octal and Hexadecimal Integers

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

Characters, Strings, and Floats

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

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

Number representations

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

Number System. Introduction. Decimal Numbers

COMP Overview of Tutorial #2

Octal & Hexadecimal Number Systems. Digital Electronics

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

Level ISA3: Information Representation

CMPE223/CMSE222 Digital Logic Design. Positional representation

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

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

Module 1: Information Representation I -- Number Systems

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

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

What Is It? Instruction Register Address Register Data Register

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

Chapter 2 Bits, Data Types, and Operations

CHW 261: Logic Design

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

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

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Fundamentals of Programming Session 2

Introduction to Computers and Programming. Numeric Values

Number Systems CHAPTER Positional Number Systems

COE 202- Digital Logic. Number Systems II. Dr. Abdulaziz Y. Barnawi COE Department KFUPM. January 23, Abdulaziz Barnawi. COE 202 Logic Design

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Exercises and Answers

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

MACHINE LEVEL REPRESENTATION OF DATA

Lecture 2: Number Systems

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

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.

Introduction to Numbering Systems

World Inside a Computer is Binary

T02 Tutorial Slides for Week 2

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Chapter 2 Bits, Data Types, and Operations

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

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

Chapter 2 Bits, Data Types, and Operations

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

Number Systems. Both numbers are positive

Internal Data Representation

The. Binary. Number System

Memory Addressing, Binary, and Hexadecimal Review

COMP 122/L Lecture 2. Kyle Dewey

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

Number Systems MA1S1. Tristan McLoughlin. November 27, 2013

Final Labs and Tutors

Chapter 2: Number Systems

Digital Systems and Binary Numbers

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

Introduction to Computer Science-103. Midterm

CS 101: Computer Programming and Utilization

CS 121 Digital Logic Design. Chapter 1. Teacher Assistant. Hadeel Al-Ateeq

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

Numbering systems. Dr Abu Arqoub

CHAPTER 2 (b) : AND CODES

Inf2C - Computer Systems Lecture 2 Data Representation

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

Signed umbers. Sign/Magnitude otation

Lecture 6: Signed Numbers & Arithmetic Circuits. BCD (Binary Coded Decimal) Points Addressed in this Lecture

CMPS 10 Introduction to Computer Science Lecture Notes

Digital Fundamentals

Computer Sc. & IT. Digital Logic. Computer Sciencee & Information Technology. 20 Rank under AIR 100. Postal Correspondence

Numbers and Representations

Number Systems and Their Representations

Signed Binary Numbers

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

Arithmetic Processing

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Chapter Overview. Welcome to Assembly Language

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

Number systems and binary

Chapter 1 Preliminaries

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

Chapter 3: Arithmetic for Computers

Digital Logic Lecture 2 Number Systems

Xuan Guo. Lecture XIV: Review of Chapter 3 & 4. CSC 3210 Computer Organization and Programming Georgia State University. March 5, 2015.

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

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Chapter 2 Bits, Data Types, and Operations

6. Binary and Hexadecimal

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Chapter 1 Emad Felemban

Part 2,Number Systems Questions

COMP2121: Microprocessors and Interfacing. Number Systems

Electronic Data and Instructions

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

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

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

LOGIC DESIGN. Dr. Mahmoud Abo_elfetouh

LECTURE 1-2. Introduction and Number Systems

EE292: Fundamentals of ECE

Number Systems. Readings: , Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

CS 64 Week 1 Lecture 1. Kyle Dewey

Transcription:

Octal and Hexadecimal Integers CS 350: Computer Organization & Assembler Language Programming A. Why? Octal and hexadecimal numbers are useful for abbreviating long bitstrings. Some operations on octal and hexadecimal numbers correspond to operations on bitstrings. B. Outcomes At the end of today, you should know how Octal and hexadecimal numbers work To translate to and from binary, octal, hexadecimal, and decimal. To read octal and hexadecimal numbers as standing for binary 2's complement integers and to take their negative. C. Converting Between Octal, Hexadecimal, and Binary Octal: base 8 (uses digits 0 7). Hexadecimal (a.k.a. hex ): base 16 uses A F as digits for 10 15. Octal and are hexadecimal useful for abbreviating bit strings, hex for bitstrings of lengths 4, 8, 12, 16,, and octal for bitstrings of lengths 3, 6, 9, 12,. Converting Hexadecimal Binary To convert a string of hex digits into binary, replace each hex digit by its 4-bit representation: 0₁₆ = 0000₂,, 9₁₆ = 1001₂, A₁₆ = 10₁₀ = 1010₂; B₁₆ = 1011₂, C₁₆ = 1100₂, D₁₆ = 1101₂, E₁₆ = 1110₂, F₁₆ = 1111₂. E.g., 03FC₁₆ = 0000 0011 1111 1100₂. If we have k hex digits, we end up with 4 k bits unless we're told explicitly that we want fewer than that. E.g., normally we would say that 5D₁₆ represents the 8 bits 0101 1101, but if we say the 7 bits represented by 5D₁₆, we want 101 1101. We can t ask for the 6 bits represented by 5D₁₆ because we need at least 7 bits to get 101 1101. If we want the decimal number represented by 5D₁₆, we have to know if it represents a signed or unsigned bitstring; if it represents a signed bitstring, we also need to know how many bits it represents (and what scheme we're using). Example 1: As an 8-bit number, 5D₁₆ = 0101 1101₂ = 93₁₀. (Since the sign bit is 0, this equality holds whether we read the number as unsigned, sign-magnitude, 1 s complement, or 2 s complement.) Example 2: Reading 5D₁₆ as a 7-bit string, we get Unsigned, 5D₁₆ = 101 1101₂ = 93₁₀. In 2 s complement, 5D₁₆ = 101 1101₂ = -(010 0011₂) = -35₁₀. (I.e., 7-bit (2's complement) 5D₁₆ = -35₁₀.) In 1's complement, 5D₁₆ = 101 1101₂ = -(010 0010₂) = -34₁₀. CS 350: Comp Org & Asm Lang Pgmg 1 James Sasaki, 2017

In sign-magnitude, 5D₁₆ = 101 1101₂ = -(01 1101₂) = -29₁₀. Example 3: Reading DD₁₆ as the 8-bit string 1101 1101₂ Unsigned, DD₁₆ represents decimal 13*16 + 13 = 221 In 2 s complement, DD₁₆ = 1101 1101₂ = -(0010 0011)₂ = -35₁₀ In 1 s complement, DD₁₆ = 1101 1101₂ = -(0010 0010)₂ = -34₁₀ In sign-magnitude, DD₁₆ = 1101 1101₂ = -(101 1101)₂ = -93₁₀ Note that 8-bit 1101 1101₂ and 7-bit 101 1101₂ both represent -35 in 2 s complement and -34 in 1 s complement. The operation that takes 7-bit 101 1101 to 8-bit 1101 1101 is sign extension; we copy the sign bit 1 leftward. Note if the sign bit is 0, we copy that instead during sign extension: 001 1101 becomes 0001 1101. Converting Octal Binary Reading an octal number as a bitstring is similar to the process for hexadecimal numbers except that each digit is replaced by 3 bits, since 8 = 2³. If we have k octal digits, we end up with 3 k bits unless told otherwise. Example 4: 6-bit 31₈ = 011 001 = 25₁₀. 5-bit 31₈ = 11 001₂ In 2 s complement, 31₈ = 11 001 = -(00 111) = -7 In 1's complement 31₈ = 11 001 = -(00 110) = -6 In sign-magnitude 31₈ = 11 001 = -(01001) = -9. Converting Binary Hexadecimal To convert a bitstring read as an unsigned binary number to hexadecimal First, if the bitstring length is not a multiple of 4, then pad the bitstring on the left with enough 0s to get length that s a multiple of 4. Then replace each 4-bit sequence by its equivalent hex digit. E.g., 011011₂ 0001 1011₂ 1B₁₆. Alternatively, group the bitstring s bits into clumps of 4 going right-to-left and convert each clump into a hexadecimal digit. E.g., 011011₂ = 01 1011 = 1B₁₆. Don t try 011011₂ = 0110 11₂ = 63₁₆. Converting Octal Binary Replace each octal digit (0 7) by its 3-bit representation. E.g., 106₈ 001 000 110₂. Converting Binary Octal Pad bitstring on left with 0s to get a string of length divisible by 3. Replace each 3-bit sequence by its equivalent octal digit. E.g., 11010₂ 011 010₂ 32₈. (Not 11010₂ 110 10₂ 62₈.) CS 350: Comp Org & Asm Lang Pgmg 2 James Sasaki, 2017

D. Converting To Decimal We know that converting from base b to decimal involves some multiplication by powers of b. You can avoid calculating the powers of b by using factoring to get an expression that only multiplies by b. E.g., 10110₂ = 1 2⁴ + 0 2³ + 1 2² + 1 2 + 0 = (1 2³ + 0 2² + 1 2 + 1) 2 + 0 = ((1 2² + 0 2 + 1) 2 + 1) 2 + 0 = (((1 2 + 0) 2 + 1) 2 + 1) 2 + 0 At each step, the running total is the decimal equivalent of the bitstring seen so far: 10₂ = 2₁₀; 101₂ = 5₁₀, 1011₂ = 11₁₀, and 10110₂ = 22₁₀. This multiply and add technique generalizes to other bases. To convert an octal string to decimal, we multiply by 8 and add the next digit. To convert a hex string to decimal, we multiply by 16 and add the next digit. E. Converting From Decimal When converting from decimal to base b, we repeatedly divide by b and capture the remainders to build the base b number from right to left. To convert a number n > 0 to base b: Start with result string S = the empty string. while n > 0 Divide n by b to get quotient q and remainder r. If r > ten, convert it to the appropriate letter (A for 10, B for 11, etc). Append r to the left end of the string S. Set n q F. Taking the (1 or 2's Complement) Negative of Bitstrings Represented Using Hexadecimal What is the relationship between the hex representations of a bitstring and its 1 or 2's complement negative? Example 5: 3A₁₆ represents 0011 1010₂, whose 2's complement negative is 1100 0110₂, which is represented by C6₁₆. (Its 1's complement negative is 1100 0101₂, which is C5₁₆.) Example 6: FAB₁₆ = 1111 1010 1011₂, whose 2's complement negative = 0000 0101 0101₂ = 055₁₆. More generally, let N be a hex number that represents a bitstring X, and let M be the hex number that represents -X. How can we get from N to M? The obvious way is to take N, convert to X, calculate -X, and then convert to M. For 1's complement, we can go from hex M = bitstring X to hex N = bitstring -X₂ without calculating X by processing M digit by digit. Let d be a hex digit, then we get the 1's complement of (the bitstring for) d by flipping each bit of (the bitstring for) d. This corresponds to subtracting each bit of d from 1, which corresponds to subtracting 1111₂ d = 15₁₀ d = F₁₆ d. CS 350: Comp Org & Asm Lang Pgmg 3 James Sasaki, 2017

Definition: 15 - d is the 15's complement of the hex digit d. The 15 s complement of a hex string is the result of taking the 15 s complement of each hex digit. Recall that we were trying to take M, a hex representation of bitstring X and find N, the hex representation of -X. Using 1 s complement to interpret X and -X, it turns out that the 15 s complement of M gives you N. Example 7: 3A₁₆ = 0011 1010₂ and the 15 s complement of 3A₁₆ is C5₁₆ = 1100 0101, and in 1 s complement, -(0011 1010) = 1100 0101. To use 2 s complement when going from X to -X, we take the 1 s complement negative of X and add 1. So to go from M = X to N = -X using 2 s complement negative, we can take the 15 s complement of M and add 1. Definition: The 16 s complement of a hex string is its 15 s complement plus 1. Example 8: 3A₁₆ = 0011 1010₂ and the 16 s complement of 3A₁₆ is C6₁₆ = 1100 0110, and in 1 s complement, -(0011 1010) = 1100 0110. G. Taking the (1 or 2's Complement) Negative of Bitstrings Represented Using Octal Now let s assume that M is an octal string that represents some bitstring X, and we want to calculate the octal string N that represents -X when we take the negative in (a) 1 s complement or (b) 2 s complement. It turns out that operations involved are similar to those for hexadecimal numbers. To go from octal M = bitstring X to octal N = bitstring -X using 1 s complement negative, we take the 7 s complement of M by subtracting each octal digit from 7. Example 9: The 7's complement of 765₈ is 012₈, 765₈ = 111 110 101₂, and using 1 s complement, - (111 110 101) = 000 001 010 = 012₈. To take the negative using 2 s complement, we take the 8 s complement of M by adding 1 to the 7 s complement of M. Example 10: The 8's complement of 765₈ is 013₈, 765₈ = 111 110 101₂, and using 2 s complement, - (111 110 101) = 000 001 011 = 013₈. CS 350: Comp Org & Asm Lang Pgmg 4 James Sasaki, 2017

Illinois Institute of Technology Activity 3 Octal and Hexadecimal Data CS 350: Computer Organization & Assembler Language Programming A. Why? Octal and hexadecimal numbers are useful for abbreviating long bitstrings. B. Outcomes After this activity, you should Be able to convert between binary, octal, hexadecimal, and decimal representations of numbers and to take the 1's and 2's complement of octal and hexadecimal numbers (when read as standing for bitstrings). C. Problems 1. Let X be the bitstring 101101101100. a. What is the hex representation of X? b. What is the octal representation of X? c. What decimal value does X represent as an unsigned binary number? d. What decimal value does X represent in 2's complement? 2. Let X be the the bitstring 1101011101. a. What decimal value does X represent as an unsigned binary number? b. What decimal value does X represent in 2's complement? c. What the hex representation of X? (Pad X with leading 0's to get to the next multiple of 4 bits.) d. What is the octal representation of X? (Pad X with leading 0's to get to the next multiple of 3 bits.) 3. Let X be the bitstring represented by hexadecimal FAB. a. What is X? b. What decimal value does FAB₁₆ represent as an unsigned integer? c. What is -X in 2 s complement and what does -X equal in decimal? d. What decimal value does X represent in 2 s complement? e. What is the hex representation of -X? f. What are the 15 s and 16 s complement of FAB₈? (The 16 s complement should match your answer to part (e).) 4. Let X be the bitstring represented by 735₈. a. What is X? b. What decimal value does X represent as an unsigned integer? c. What is -X in 2 s complement and what does -X equal in decimal? d. What decimal value does X represent in 2 s complement? e. What is the octal representation of -X? f. What are the 7 s and 8 s complement of FAB₈? (The 8 s complement should match your answer to part (e).) CS 350: Comp Org & Asm Pgm g 5 James Sasaki, 2017

Illinois Institute of Technology Activity 3 5. Let X be the 12-bit string represented by 1610₈. Let Y be the 10-bit string we get if we truncate the two leftmost bits from X. a. What are X and Y? b. What is -Y in 2 s complement and what does -X equal in decimal? c. What decimal value does Y represent in 2 s complement? d. Turning back to X, what are its 7 s and 8 s complements? 6. Let Z be the 12-bit signed extension of Y from the previous problem: To get Z, copy the sign bit of Y twice and prepend them to Y. a. What is Z and what is its octal representation? b. What is -Z in 2 s complement? What is its octal representation? c. What are the 7 s and 8 s complement of the octal representation of Z? (The 8 s complement should match the octal representation of -Z.) CS 350: Comp Org & Asm Pgm g 6 James Sasaki, 2017

Illinois Institute of Technology Activity 3 Activity 3 Solution 1. (Bitstring 101101101100) a. 1011 0110 1100₂ = B6C₁₆ b. 101 101 101 100₂ = 5554₈ c. 1011 0110 1100₂ = 4 + 8 + 32 + 64 + 256 + 512 + 2048 = 2924₁₀ d. 1011 0110 1100₂ = -0100 1001 0100₂ = -(4 + 16 + 128 + 1024)= -1172 2. (Bitstring 1101011101) a. 11 0101 1101₂ = 1 + 4 + 8 + 16 + 64 + 256 + 512 = 861₁₀ b. 11 0101 1101₂ = -00 1010 0011₂ = -(1 + 2 + 32 + 128) = -163₁₀ c. 0011 0101 1101₂ = 35D₁₆ d. 001 101 011 101₂ = 1535₈ 3. (Hex FAB) a. FAB = 1111 1010 1011₂ b. 15 16² + 10 16 + 11 = 15 256 + 160 + 11 = 3840 + 160 + 11 = 4011 c. -X = -1111 1010 1011₂ = 0000 0101 0101 = 1 + 4 + 16 + 64 = 85 d. X = -(-X) = -85 e. 0000 0101 0101 = 055₁₆ f. 15 s complement of FAB = 054₁₆; 16 s complement is 055₁₆. 4. (Octal 735) a. 735₈ = 111 011 101₂ b. 111 011 101 = 1 + 4 + 8 + 16 + 64 + 128 + 256 = 477₁₀ c. -X = -111 011 101₂ = 000 100 011 = 1 + 2 + 32 = 35₁₀ d. X = -(-X) = -35 e. -X = 000 100 011 = 043₈ f. 7 s complement of 735₈ = 042₈; 8 s complement is 043₈. 5. (Bitstring for 1610₈) a. X = 1610₈ = 001 110 001 000 so Y = 1 110 001 000. b. -Y = -1 110 001 000 = 0 001 111 000 = 8 + 16 + 32 + 64 = 120₁₀ c. Y = - -Y = -120₁₀. d. 7 s complement of 1610₈ = 6167₈; 8 s complement is 6170₈ 6. (Sign-extended 1 110 001 000) a. Z = 111 110 001 000 = 7610₈ b. -Z = -111 110 001 000 = 000 001 111 000 = 0170₈ c. 7 s complement of 7610₈ = 0167₈; 8 s complement = 0170₈. CS 350: Comp Org & Asm Pgm g 7 James Sasaki, 2017