Base Conversion written by Cathy Saxton



Similar documents
Section 1.4 Place Value Systems of Numeration in Other Bases

Useful Number Systems

NUMBER SYSTEMS. William Stallings

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

=

CSI 333 Lecture 1 Number Systems

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

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

Decimal to Binary Conversion

3. Convert a number from one number system to another

Binary, Hexadecimal, Octal, and BCD Numbers

COMPSCI 210. Binary Fractions. Agenda & Reading

Computer Science 281 Binary and Hexadecimal Review

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

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

Lecture 11: Number Systems

CS201: Architecture and Assembly Language

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

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

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

Numbering Systems. InThisAppendix...

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

Chapter 4: Computer Codes

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

Memory is implemented as an array of electronic switches

Positional Numbering System

Lecture 2. Binary and Hexadecimal Numbers

Binary Numbers. Binary Octal Hexadecimal

Counting in base 10, 2 and 16

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

Unsigned Conversions from Decimal or to Decimal and other Number Systems

CPEN Digital Logic Design Binary Systems

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

NUMBER SYSTEMS. 1.1 Introduction

Systems I: Computer Organization and Architecture

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

Binary Representation

Solution Guide Chapter 14 Mixing Fractions, Decimals, and Percents Together

Preliminary Mathematics

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

Chapter 2. Binary Values and Number Systems

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

Number Systems and Radix Conversion

Subnetting Examples. There are three types of subnetting examples I will show in this document:

Storing Measurement Data

Numeration systems. Resources and methods for learning about these subjects (list a few here, in preparation for your research):

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

6 The Hindu-Arabic System (800 BC)

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

Cyber Security Workshop Encryption Reference Manual

THE BINARY NUMBER SYSTEM

Activity 1: Using base ten blocks to model operations on decimals

Number Representation

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

HOMEWORK # 2 SOLUTIO

Session 7 Fractions and Decimals

4.3 TABLE 3 TABLE five

Decimal Notations for Fractions Number and Operations Fractions /4.NF

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

The Hexadecimal Number System and Memory Addressing

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 TUTORIAL OUTCOME 2 Part 1

Binary Adders: Half Adders and Full Adders

APPENDIX B. Routers route based on the network number. The router that delivers the data packet to the correct destination host uses the host ID.

Stanford Math Circle: Sunday, May 9, 2010 Square-Triangular Numbers, Pell s Equation, and Continued Fractions

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

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

We can express this in decimal notation (in contrast to the underline notation we have been using) as follows: b + 90c = c + 10b

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

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

IT:101 Cisco Networking Academy I Subnetting

Prerequisite knowledge Students should have a good understanding of how our decimal number system works as well as understand place value.

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

2.6 Exponents and Order of Operations

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

Digital codes. Resources and methods for learning about these subjects (list a few here, in preparation for your research):

26 Integers: Multiplication, Division, and Order

Introduction to Python

IP Subnetting and Related Topics A Tutorial by Chris Uriarte <chrisjur@cju.com> Updated April 2001

Click on the links below to jump directly to the relevant section

Chapter 1: Digital Systems and Binary Numbers

A Step towards an Easy Interconversion of Various Number Systems

The Crescent Primary School Calculation Policy

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

The programming language C. sws1 1

Math Circle Beginners Group October 18, 2015

1. How many unique network IDs are there in class A addresses? # of unique hosts?

Student Outcomes. Lesson Notes. Classwork. Discussion (10 minutes)

Seriously Simple Sums! Vedic Maths Free Tutorial. Maths Tips and Tricks to Improve Your Math Abilities

IB Math Research Problem

IBM Emulation Mode Printer Commands

Grade 6 Math Circles. Binary and Beyond

0.8 Rational Expressions and Equations

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

Basics of Counting. The product rule. Product rule example. 22C:19, Chapter 6 Hantao Zhang. Sample question. Total is 18 * 325 = 5850

Chapter 1. Binary, octal and hexadecimal numbers

Expert Reference Series of White Papers. Simple Tricks To Ace the Subnetting Portion of Any Certification Exam COURSES.

Copyright 2012 Pearson Education, Inc. Chapter 1 INTRODUCTION TO COMPUTING AND ENGINEERING PROBLEM SOLVING

5.5. Solving linear systems by the elimination method

Fractions to decimals

Transcription:

Base Conversion written by Cathy Saxton 1. Base 10 In base 10, the digits, from right to left, specify the 1 s, 10 s, 100 s, 1000 s, etc. These are powers of 10 (10 x ): 10 0 = 1, 10 1 = 10, 10 2 = 100, 10 3 = 1000, etc. Each digit in a base 10 number can have a value from 0 to 9. Base 10 numbers are commonly referred to as decimal numbers. 2. Notation As we work with numbers, the following notation will be used. The top row of the table shows the value for each place in the number. The second row shows the actual digits in the number. 253 is represented by: 100 s 10 s 1 s places 2 5 3 digits 3. General Base Definitions Base n means that each place (x) in the number represents n x. (n 0, n 1, n 2, n 3, etc.) Valid digits in base n are 0 to n-1. When working with numbers in different bases, a subscript is usually used to indicate the base (e.g. 253 10 means 253 in base 10). We can represent any number in any base. The number will usually look different when expressed in another base, but its value is the same. When a number is stored in the computer s memory, it has a specific value; we can show it using base 10, base 2, base 16, etc., but the value never changes -- only the representation changes. 4. Base 2: Binary Each digit in a base 2 number can have a value of 0 or 1. Places are 1 s, 2 s, 4 s, 8 s, etc. (2 0 = 1, 2 1 = 2, 2 2 = 4, 2 3 = 8, etc.) Computers store numbers in binary, which is the common name for base 2 numbers. Each binary digit corresponds to one bit of memory. There are 8 bits (or 8 binary digits) in a byte. The computer stores values in memory by turning these bits on (1) or off (0) to correspond to the binary representation of the number. 4.1. Converting binary to decimal Converting from binary (base 2) to decimal (base 10) requires doing a little math with the digits in each place. Here is an example for converting a binary value of 1001 to its decimal equivalent. 1001 2 =? 10 Remember that the places are 1 s, 2 s, 4 s, 8 s, etc: (2 3 ) (2 2 ) (2 1 ) (2 0 ) 8 s 1 0 0 1 We make the following calculation to get the equivalent decimal number: 1001 2 = (1 * 8) + (0 * 4) + (0 * 2) + (1 * 1) = 8 + 0 + 0 + 1 = 9 10 Here s another example: 11010 2 =? 10 1 1 0 1 0 11010 2 = (1 * 16) + (1 * 8) + (0 * 4) + (1 * 2) + (0 * 1) = 16 + 8 + 0 + 2 + 0 = 26 10 11-15-2006 Base Conversion 1

4.2. Converting decimal to binary Converting from decimal to binary is a little trickier. Let s consider the case of converting a decimal 5 to its binary representation. 5 10 =? 2 We ll calculate the appropriate digit for each place in the resulting binary number, working from the left to the right. First, we determine the largest power of 2 that s less than or equal to the number. In this case, 8 (2 3 ) is too large, so we start with the 4 (2 2 ). Therefore, we know the result will contain digits in three places -- 4 s, 2 s and 1 s:??? We need to figure out which digits should be 1 s, and which should be 0 s (since these are the only legal values for a binary digit). We can see that putting a 1 in the 4 s place will help: 1?? This accounts for 4 of the 5 units that we need to represent. That means that we can t have any 2 s (or the total would become larger than our goal of 5). So, we put a 0 in the 2 s place: 1 0? That leaves the 1 s place to fill, and we see that putting a 1 there will give us the desired result: 1 0 1 this is the equivalent binary value 5 10 = (1 * 4) + (0 * 2) + (1 * 1) = 101 2 Let s work though another example: 22 10 =? 2 In this example, we need to start with the 16 s place (2 4 ); the next place (2 5 ) is the 32 s, which is too large:????? Putting a 1 in the 16 s place means that we ve accounted for 16 of 22 units. This leaves 22-16 = 6 remaining: 1???? total so far: 16 remaining: 6 We can t use any 8 s since we only have 6 units remaining. (You can also observe that 16 + 8 = 24, which is too large.) So, we put a 0 in the 8 s place: 1 0??? total so far: 16 remaining: 6 We now see whether we can use a 4. Since 4 is less than the remaining 6, the 4 will be helpful, so we put a 1 in the 4 s place. That means that we now have remaining: 6 (our previous value) - 4 (what we just accounted for) = 2. 1 0 1?? total so far: 16 + 4 = 20 remaining: 2 Next we look at the 2 s. Since that s exactly what we still need, we put a 1 in the 2 s place. That leaves us with 0 remaining. Since we don t need anything more, we put a 0 in the 1 s place: 1 0 1 1 0 the binary representation for 22 10 22 10 = (1 * 16) + (0 * 8) + (1 * 4) + (1 * 2) + (0 * 1) = 10110 2 2 Base Conversion 11-15-2006

5. Base 16: Hexadecimal As we look at a numbers stored by the computer, it quickly gets cumbersome to deal with long sequences of 0 s and 1 s in the binary representation. Programmers often use base 16 since it provides a convenient way to express numbers and is easily converted to and from base 2. For a base 16 number, the places are 1 s, 16 s, 256 s, etc. (16 0 = 1, 16 1 = 16, 16 2 = 256). Each digit can hold a value from 0 to 15. In order to have single characters for representing 10, 11, 12, 13, 14, and 15, we use A, B, C, D, E, and F, respectively. Thus, the valid values for a base 16 digit are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Base 16 numbers are commonly referred to as hexadecimal or hex numbers. In C/C++ programming, hex numbers often use the prefix 0x to indicate hex numbers, e.g. 0x3AC. 5.1. Converting hex to decimal Converting from hex (base 16) to decimal (base 10) is similar to converting binary to decimal. Here is an example for converting a hex value of 45 to its decimal representation. 45 16 =? 10 Remember that the places are 1 s and 16 s: (16 1 ) (16 0 ) 4 5 We can make the following calculation to get the equivalent decimal number: 45 16 = (4 * 16) + (5 * 1) = 64 + 5 = 69 10 Here s another example. 3AC 16 =? 10 (16 2 ) (16 1 ) (16 0 ) 3 A (=10) C (=12) 3AC 16 = (3 * 256) + (10 * 16) + (12 * 1) = 768 + 160 + 12 = 940 10 5.2. Converting decimal to hex To convert from decimal to hex, we use the same technique as we did above when converting from decimal to binary. Let s consider the case of converting a decimal 73 to its hex equivalent. 73 10 =? 16 We ll calculate the appropriate digit for each place in the resulting hex number, working from the left to the right. First, we determine the largest power of 16 that s less than or equal to the number. In this case, 256 (16 2 ) is too large, so we ll start with the 16 s place. So, we know the result will contain digits in two places -- 16 s and 1 s:?? 73 / 16 = 4 with a remainder of 9. So, there are four 16 s in 73. Now we know part of the result: 4? So, we ve accounted for 4 * 16 = 64 of the 73. That leaves the remainder, 9, which we take care of by putting it in the 1 s place: 4 9 73 10 = (4 * 16) + (9 * 1) = 49 16 11-15-2006 Base Conversion 3

Let s work though another example: 940 10 =? 16 In this case, we start with 256 (16 2 ). The next place is 16 3 = 4096, which is larger than our value. The resulting hex number will have 3 digits:??? Calculate the digit for the 256 s place: 940 / 256 = 3 with a remainder of 172. So, we ll put a 3 in the 256 s place. 3?? Calculate how much is left over: we ve accounted for 3 * 256 = 768; that leaves 940-768 = 172. Calculate the digit for the 16 s place: 172 / 16 = 10 with a remainder of 12. So, we need ten 16 s. Recall that 10 is represented with A. 3 A? The remainder, 12, tells us how much we still need to take care of. We put that value in the 1 s place, using C to represent 12. 940 10 = (3 * 256) + (10 * 16) + (12 * 1) = 3AC 16 6. Converting Between Binary and Hexadecimal Converting between base 2 and base 16 is much simpler than converting either to base 10. The minimum value that can be expressed with four binary digits is 0000, or 0 10. The maximum value that can be expressed with four binary digits is 1111, or 15 10 (8 + 4 + 2 + 1). So, we see that four binary digits can be used to represent the numbers 0-15, which is exactly the range represented by a single hex digit. Here are conversions from binary to hex: 0000 2 = 0 16 0001 2 = 1 16 0010 2 = 2 16 0011 2 = 3 16 0100 2 = 4 16 0101 2 = 5 16 0110 2 = 6 16 0111 2 = 7 16 1000 2 = 8 16 1001 2 = 9 16 1010 2 = A 16 1011 2 = B 16 1100 2 = C 16 1101 2 = D 16 1110 2 = E 16 1111 2 = F 16 Notice that every value that can be expressed with four binary digits can be expressed with a single hex digit, and every value that can be expressed with a single hex digit can be expressed with four binary digits. Every hex digit can be represented with four binary digits. Every four binary digits can be converted to one hex digit. To convert from binary to hex, separate the binary number into groups of four digits starting on the right. Each group can be converted to a hex value. For example, 10010010 can be visualized as two 4-digit groups: 1001 0010; each group is converted to a hex value, and we get a result of 92: 1 0 0 1 0 0 1 0 9 2 10010010 2 = 92 16 For binary numbers like 100110 that don t divide evenly into 4-digit groups, add 0 s to the left as necessary: 0010 0110 2 = 26 16. Converting hex to binary is just the reverse; be careful to write four binary digits for each hex digit -- that means starting with a zero for values less than 8. B4 16 = 1011 0100 2 You don t need the zeros at the beginning of the binary number: 6F 16 = _110 1111 2 4 Base Conversion 11-15-2006

Memory is just a continuous span of 1 s and 0 s. Modern computers typically like to retrieve 32 bits at a time. While we often use all 32 bits to represent a single value, it is also common to use that 32-bit space to store 2 or 4 values. Consider a 32-bit number such as: 00000000010001011010000000100011 The following table shows how we can interpret that memory -- as a single 32-bit value, two 16-bit values, or four 8-bit values: Binary Representation Hex Decimal One Value: 00000000010001011010000000100011 0045A023 4562979 Two Values: 0000000001000101, 1010000000100011 0045, A023 69, 40995 Four Values: 00000000, 01000101, 10100000, 00100011 00, 45, A0, 23 0, 69, 160, 35 Notice that by using the hex representation for the 32-bit value, you can easily determine the 2 or 4 values that are held in the 32 bits of memory. Ultimately, the computer uses binary to store numbers. Unfortunately, representing numbers in binary is very verbose and hard to read. Using a base that s a power of 2 retains the binary nature of computer math, but yields more compact and readable numbers. Base 4 isn t much of an improvement. Base 8 is good, but represents binary digits in groups of three, while computers generally use groups of eight. Base 256 would require too many symbols. Base 16 represents binary digits in convenient groups of four, and only requires 16 symbols. That is why hex is commonly used when programming. 7. Other Interesting Facts When we put a 0 at the end of a base 10 number, it has the effect of multiplying the value by 10; e.g. adding a 0 after 37 gives us 370, or 37 * 10. The same works for other bases, with the multiplier being the base number; e.g. 101 2 (5 10 ) becomes 1010 2 (10 10 = 5 [the original value] * 2 [the base]). Dropping the last digit does an integer divide by the base; e.g. 368 / 10 = 36. Base 8 s common name is octal. In C or C++, if you start an integer with a 0, it will be interpreted as an octal value. Conversion from binary to octal is like converting to hex, but using groups of three binary digits; e.g. 110 101 2 = 65 8. When converting between hex and octal, the easiest way is to start by representing the value in binary! 8. Summary base common name places valid digit values 10 decimal 1, 10, 100, 1000, etc. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 2 binary 1, 2, 4, 8, 16, 32, etc. 0, 1 16 hexadecimal, hex 1, 16, 256, 4096, etc. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F 11-15-2006 Base Conversion 5