Lecture 2. Binary and Hexadecimal Numbers



Similar documents
Computer Science 281 Binary and Hexadecimal Review

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Useful Number Systems

Solution for Homework 2

CSI 333 Lecture 1 Number Systems

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Section 1.4 Place Value Systems of Numeration in Other Bases

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

The string of digits in the binary number system represents the quantity

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

NUMBER SYSTEMS. William Stallings

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Base Conversion written by Cathy Saxton

Binary Numbers. Binary Octal Hexadecimal

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

NUMBER SYSTEMS. 1.1 Introduction

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Systems I: Computer Organization and Architecture

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

Binary Representation

Lecture 11: Number Systems

2011, The McGraw-Hill Companies, Inc. Chapter 3

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Lecture 8: Binary Multiplication & Division

Number Representation

2 Number Systems. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

Binary Adders: Half Adders and Full Adders

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

=

CPEN Digital Logic Design Binary Systems

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Recall the process used for adding decimal numbers. 1. Place the numbers to be added in vertical format, aligning the decimal points.

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

Unsigned Conversions from Decimal or to Decimal and other Number Systems

Binary, Hexadecimal, Octal, and BCD Numbers

Number and codes in digital systems

Digital Design. Assoc. Prof. Dr. Berna Örs Yalçın

Number Systems and Radix Conversion

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Numbering Systems. InThisAppendix...

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

COMPSCI 210. Binary Fractions. Agenda & Reading

Positional Numbering System

Chapter 4: Computer Codes

CS201: Architecture and Assembly Language

HOMEWORK # 2 SOLUTIO

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

The Answer to the 14 Most Frequently Asked Modbus Questions

Chapter 2. Binary Values and Number Systems

Grade 6 Math Circles. Binary and Beyond

Chapter 1: Digital Systems and Binary Numbers

CHAPTER 5 Round-off errors

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

3. Convert a number from one number system to another

Today s topics. Digital Computers. More on binary. Binary Digits (Bits)

PREPARATION FOR MATH TESTING at CityLab Academy

Binary Numbering Systems

THE BINARY NUMBER SYSTEM

YOU MUST BE ABLE TO DO THE FOLLOWING PROBLEMS WITHOUT A CALCULATOR!

Number Systems. Introduction / Number Systems

Decimal to Binary Conversion

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

The Euclidean Algorithm

The gas can has a capacity of 4.17 gallons and weighs 3.4 pounds.

BINARY CODED DECIMAL: B.C.D.

MACHINE INSTRUCTIONS AND PROGRAMS

Decimal Number (base 10) Binary Number (base 2)

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

CS321. Introduction to Numerical Methods

A Step towards an Easy Interconversion of Various Number Systems

Intel Hexadecimal Object File Format Specification Revision A, 1/6/88

Arithmetic in MIPS. Objectives. Instruction. Integer arithmetic. After completing this lab you will:

Machine Architecture and Number Systems. Major Computer Components. Schematic Diagram of a Computer. The CPU. The Bus. Main Memory.

Appendix A Installation of Visual C++ and MASM

Take-Home Exercise. z y x. Erik Jonsson School of Engineering and Computer Science. The University of Texas at Dallas

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

Data Storage 3.1. Foundations of Computer Science Cengage Learning

Logic Reference Guide

Paramedic Program Pre-Admission Mathematics Test Study Guide

1 Description of The Simpletron

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Verilog - Representation of Number Literals

Pre-Algebra Lecture 6

Hexadecimal Object File Format Specification

Counters and Decoders

Cyber Security Workshop Encryption Reference Manual

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

Karnaugh Maps & Combinational Logic Design. ECE 152A Winter 2012

Data Representation. What is a number? Decimal Representation. Interpreting bits to give them meaning. Part 1: Numbers. six seis

Chapter 7 Lab - Decimal, Binary, Octal, Hexadecimal Numbering Systems

3.Basic Gate Combinations

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Transcription:

Lecture 2 Binary and Hexadecimal Numbers Purpose: Review binary and hexadecimal number representations Convert directly from one base to another base Review addition and subtraction in binary representations Determine overflow in unsigned and signed binary addition and subtraction Lecture 2 Page 1

The Need for Other Bases Humans are used to the decimal number system, also called radix-10 or base-10. To state the obvious, base-10 means that a digit has one of ten possible values, 0 through 9. In computers, numbers are stored in binary, also called radix-2 or base-2, using arrays of flip-flops. Each digit may take one of two values, either 0 or 1. Long strings of these 1 s and 0 s are cumbersome to use, so we will usually represent binary numbers using hexadecimal, also called radix-16 or base-16. It is important to note that no computer actually stores values in hardware using hexadecimal. This number system is only a convenience for humans. All of these number systems are positional. Lecture 2 Page 2

Unsigned Decimal Numbers are represented using the digits 0, 1, 2,, 9. Multi-digit numbers are interpreted as in the following example: 793 10 = Unsigned Binary Numbers are represented using the digits 0 and 1. Multi-digit numbers are interpreted as in the following example: 10111 2 = In binary, each digit is called a bit. Since we use binary to represent the values stored in a group of flip-flops, we usually specify a binary system by the number of bits (flip-flops) being used to store each number. When we write numbers in this system, we will write all bits, including leading 0 s. The number above is expressed in 5-bit binary. The number below is in 8-bit binary. 00010111 2 Lecture 2 Page 3

Unsigned Hexadecimal Numbers are represented in hexadecimal using the digits 0, 1, 2,, 9, A, B,, F where the letters represent values: A=10, B=11, and so on to F=15. Note that this gives sixteen possible values for each digit. Multidigit numbers are interpreted as in the following example: 76CA 16 = Notes on Bases Since all three number bases will be used, including the correct subscript when a number is written out of context is mandatory. Pronunciation Words like ten, twenty, and one-thousand refer to specific numbers of items, regardless of how the numbers are written. To avoid confusion, binary and hexadecimal numbers are spoken by naming the digits followed by binary or hexadecimal. For example, 1000 16 is pronounced one zero zero zero hexadecimal. One-thousand is actually 3E8 16. Lecture 2 Page 4

Ranges of Unsigned Number Systems System Lowest Highest Number of Values 4-bit binary (1-digit hex) 8-bit binary (1 byte) (2-digit hex) 16-bit binary (2 bytes) (4-digit hex) n-bit binary Lecture 2 Page 5

2 s Complement Binary Numbers Most microprocessors today use 2 s Complement numbers to represent systems with positive and negative values. Hardware performs addition and subtraction on binary values the same way whether they represent unsigned systems or 2 s complement systems, and this greatly simplifies the design of the processor The only difference between unsigned binary systems and 2 s comp. binary systems is that the most significant bit in signed systems has a weight of -2 n-1. Both systems are also defined by the number of bits being used, and as with unsigned, we must write down all bits. To distinguish between the two, we will use a 2c subscript to indicate a 2 s comp. number. Example: Convert 10011010 2c to decimal. Example: Convert 11011 2c to decimal. Example: Convert 01011 2c to decimal. It is very important to note that microprocessors usually view a group of bits as simply a group of bits. It is the human that interprets the group as an unsigned value, signed value, or also as just a group of bits. Lecture 2 Page 6

Ranges of Signed Number Systems System Least Greatest Number of Values 4-bit binary 8-bit binary 16-bit binary n-bit binary Note that the least representable value has a single 1 in the column with a negative weight and 0 s in the columns with positive weights. The greatest representable number is just the opposite: 1 s in the positively weighted columns and a 0 in the column with a negative weight. Lecture 2 Page 7

Sign Bit Since the leftmost column has a negative weight, and the magnitude of that weight is larger than the weights of all the positive columns added together, any number with a 1 in the leftmost column will be negative. If the leftmost bit has a 0, then there is no negative contribution, and the value will be positive. Hence, the sign of the number can be determined by inspection. Negating a 2 s Complement Number The official way to negate a value is to subtract it from zero. This will generate a number with the same magnitude but with the opposite sign. The second method is to perform the 2 s complement, which is the following two steps: 1. Perform the 1 s complement (flip all the bits) 2. Add 1 Example: Negate 00101001 2c (41 10 ) Lecture 2 Page 8

Converting Between Number Systems Given the three number systems (binary, hexadecimal, and decimal), there are six possible conversions to allow us to convert directly from one to another. Binary to Decimal: This was covered earlier in this lecture by determining the weights for each column and adding them up, either as unsigned or as signed. Hexadecimal to Decimal: This was also covered earlier in this lecture by determining the weights for each column and adding them up. Decimal to Binary (or Hexadecimal): This conversion is more of a process than the others. 1. Successively divide the decimal number by the new base and keep track of the remainders generated. 2. Stop dividing once the quotient reaches 0. 3. Write the remainders in the opposite order than they were generated. 4. Add leading digits if necessary. Lecture 2 Page 9

Example: Convert 53 10 to 8-bit unsigned binary. Example: Convert 172 10 to 2-digit hexadecimal. Lecture 2 Page 10

Note: To convert a negative value, first convert the magnitude to the correct number of bits as done above. Then, negate the result to get the final answer. Binary to Hexadecimal: This conversion is the reason that hexadecimal is used in the first place. Since once hexadecimal digit can represent 16 different values, and four bits can also represent 16 (2 4 ) different values, the bits of the binary number will be grouped together in 4 s and replaced by the hexadecimal digit with the same value. Example: Convert the binary numbers below to hexadecimal. 100101011110 2 011010101011 2c Note that a binary number may not always contain an integer multiple of 4 bits. In these cases, always extend the binary number by padding it with zeros, whether the number is unsigned or signed. Example: Convert the binary numbers below to hexadecimal. 101011110 2c 11011 2c Lecture 2 Page 11

Hexadecimal to Binary: As with binary to hexadecimal, this conversion is by inspection. Each hexadecimal digit is replaced with the four bits that represents the same value. Example: Convert the following hexadecimal numbers to binary. BEFA 16 73FC 16 Hexadecimal is not usually interpreted as signed or unsigned. It is simply a more convenient method for humans to discuss binary patterns. By default, we will assume that the binary pattern has four bits for every hexadecimal digits used, as done above. However, we can also specify a binary system with any number of bits, as done on the latter half of the previous slide. We must explicitly state the binary system being used, and we will ignore the padding 0 s. Example: Convert the following hexadecimal numbers to the specified binary system. 07B 16 to 9-bit signed 1F 16 to 5-bit unsigned Lecture 2 Page 12

Binary Arithmetic Unsigned and signed addition and subtraction generate the same numerical result. The difference is determining if overflow or underflow (usually grouped together generically as overflow ) occurs. The methods below work for both addition and subtraction. For unsigned: For signed: The above methods are easily implemented in hardware. For humans, there is an alternate approach for signed arithmetic. Addition: If the two numbers being added have the same sign, the answer must have that sign. Otherwise, overflow occurs. Signed addition cannot generate overflow if the two numbers being added have different signs. Subtraction: Lecture 2 Page 13

Example: Determine the result, if unsigned overflow occurred, and if signed overflow occurred. (Note that the binary numbers are shown with an unsigned subscript for simplicity.) 0 1 1 0 1 0 1 1 2 + 0 1 0 1 1 0 1 0 2 0 1 1 0 0 1 0 1 2-1 1 0 1 1 0 1 1 2 Lecture 2 Page 14

Modular Number Systems 1111 0000 0001 1110 0010 Wrap-around point for unsigned 1101 0011 1100 0100 1011 Wrap-around point for signed 0101 1010 0110 1001 0111 1000 Here s another way to visualize overflow. Binary systems are modular. For our purposes, modular means that a constant number of digits are used. Above, increasing a value (i.e. add a positive or subtract a negative) moves us clockwise around the number line. Decreasing a value (i.e. subtract a positive or add a negative) moves us counterclockwise. If the wrap-around point is crossed during this move, overflow occurs. Lecture 2 Page 15

Extending Binary Numbers When performing arithmetic operations, the binary numbers must have the same number of bits. Therefore, it is sometimes necessary to extend the shorter number so that it has the same number of bits as the longer number. This must be done in a manner such that the new, longer number still represents the same value as the shorter number. For unsigned: For signed: Example: Extend the binary numbers below to 16 bits. 01101111 2 100101101 2 01110 2c 10011001 2c Note that a binary number can always be extended correctly. Lecture 2 Page 16

Truncating Binary Numbers There are times when a value will be expressed in more bits than needed, and the number of bits being used should be reduced to save space. This is done by discarding the most significant bits of a binary number. Technically, truncation may always be performed. However, we will say that it is not possible if it yields a shorter number that does not represent the same value as the original, longer number. For example, of the 65536 possible 16-bit numbers, only 512 may be correctly truncated to the 512 possible 9-bit numbers. Unsigned: All bits discarded must be 0 s, otherwise the shorter number will not be accurate. Signed: All bits discarded must be the same as the new sign bit of the shorter number, otherwise the shorter number will not be accurate. Example: Determine which of the 16-bit values below can be truncated to 8 bits in the specified system. 0000 0000 1011 0111 2 1111 1111 1011 0111 2 0000 0000 1011 0111 2c 0000 0000 0011 0111 2c 1111 1110 1011 0111 2c 1111 1111 1011 0111 2c Lecture 2 Page 17