Binary and Hexadecimal Numbers

Slides:



Advertisements
Similar presentations
Base Conversion COP Practice Problem  Print a String in reverse order:  For example, if we want to print “HELLO” backwards,  we first print:
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
John Owen, Rockport Fulton HS
 Instructor: Rob Nash  Readings: Berger, Chapter 1; Rosen, Chapter 3.
Number Systems. 2 The total number of allowable symbols in a number system is called the radix or base of the system. Decimal Numbers: radix = 10 (symbols:
Data Storage Introduction to computer, 2nd semester, 2010/2011 Mr.Nael Aburas Faculty of Information Technology Islamic.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Number Systems 0, 1 0, 1, 2, 3, 4, 5, 6, 7 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 0, 1, 2, 3,
NUMBER SYSTEM AND CONVERSION Digital Circuit Choopan Rattanapoka.
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
Chapter 1 Basic Principles of Digital Systems. 2 Analog vs. Digital Analog: –A way of representing a physical quantity by a proportional continuous voltage.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Comp Sci 251 Intro 1 Computer organization and assembly language Wing Huen.
Data Representation – Chapter 3 Section 3-1. Terminology “Digital” –Discrete, well defined values/steps –Opposite of analog –Analogy: digital is to analog.
Chapter 2: The Logic of Compound Statements 2.5 Application: Number Systems and Circuits for Addition 1 Counting in binary is just like counting in decimal.
Numbering System Base Conversion. Number systems Decimal – 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary – 0, 1 Octal – 0, 1, 2, 3, 4, 5, 6, 7 Hexadecimal system.
An introduction to Numbers Dr Andrew French. You will need to consult your times table and your tables of integer powers.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Number Systems 0, 1 0, 1, 2,
Section 4.3 Other Bases.
1 Computer Science LESSON 1 on Number Bases. 2 Objective In this lesson you’ll learn about different Number Bases, specifically about those used by the.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
Binary and Hexadecimal Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Numeral Systems Rubel Biswas.
1. Binary, Decimal, Hexadecimal and Octal. 2. Conversion between various number systems. Number Systems:
 Human language : commonly used to express feeling and understand what other people expression.  Computer language : are a languages by which a user.
Appendix F Number Systems binary 0, 1 0, 1, 2, 3, 4, 5, 6, 7
Hexadecimal, Signed Numbers, and Arbitrariness
Data Representations and Computer Arithmetic
Number Representation
Assembly Language (CSW 353)
Digital Electronics INTRODUCTION ANALOG VS DIGITAL NUMBER SYSTEMS.
CHAPTER 1 : INTRODUCTION
Object Oriented Programming
COMPUTING FUNDAMENTALS
Computer Science LESSON 1 on Number Bases.
Octal & Hexadecimal Number Systems
Base ‘b’ number for i = 0 to n – 1 for an n digit quantity
CSE 102 Introduction to Computer Engineering
Octal & Hexadecimal Number Systems
2.0 COMPUTER SYSTEM 2.2 Number System and Representation
Number System conversions
Number Systems.
Octal & Hexadecimal Number Systems
Data Storage Introduction to computer, 2nd semester, 2010/2011
Binary, Octal and Hex Numbers Copyright Thaddeus Konar
Number Systems and Binary Arithmetic
Binary, Octal and Hex Numbers Copyright Thaddeus Konar
Unit 2.6 Data Representation Lesson 1 ‒ Numbers
Overview Digital Systems and Computer Systems
CDA 3100 Summer 2013.
Hexadecimal Conversions
Digital Logic & Design Lecture 02.
CS 100: Roadmap to Computing
Octal & Hexadecimal Number Systems
Introduction to Computer Programming
Digital Systems and Binary Numbers
Chapter 2: Number Systems
Binary Values and Number Systems
Objective 1.02 Understand Numbering Systems
AP Computer Science LESSON 1 on Number Bases.
John Owen, Rockport Fulton HS
Chapter 2 Number Systems.
CDA 3100 Fall 2012.
Lecture 22: Number Systems
CS 100: Roadmap to Computing
Number systems Converting numbers between binary, octal, decimal, hexadecimal (the easy way)
Binary Values and Number Systems
Chapter 2 Number Systems.
Section 4.3 Other Bases.
Dr. Clincy Professor of CS
Presentation transcript:

Binary and Hexadecimal Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Decimal Numbers A decimal number is an integer written the way we are used to write numbers, using digits from 0 to 9. Why do we use 10 digits, and not 2, or 20, or 60, or 150? It is a result of: Having 10 fingers in our hands, which makes it easier to count up to 10. Cultural convention.

Babylonian Numbers Babylonians used 60 digits instead of 10.

Babylonian Numbers Babylonians used 60 digits instead of 10. While we do not use that system for writing numbers today, it has survived in our modern culture. Where? In the way we measure time. 24 hours. 60 minutes. 60 seconds. Also in the way we measure angles. 360 degrees.

Binary Numbers Computers, in their electronic circuits, essentially use two digits: 1 and 0. 1 corresponds to a certain electrical (or magnetic, or optical) signal. 0 corresponds to another electrical (or magnetic, or optical) signal. This means that we need to represent all numbers using those two digits. Numbers represented using 1 and 0 are called binary numbers. Java (like any modern programming language) allows us to use decimal numbers. Inside the computer, before the program gets executed, all decimal numbers are translated to binary.

Binary Numbers Since 1310 is our introductory course, it is a good place for you to learn some basics about binary numbers. We will do a simplified version. We will not worry about representing: Floating point numbers. Negative numbers. We will learn how to represent positive integers in binary. We will learn how to translate from binary to decimal and the other way around.

Reading a Binary Number In general, a binary number is represented like this: bn-1 bn-2 … b2 b1 b0 Each digit bi is either 1 or 0. For example, consider binary number 10011. What is n? What is b0? What is b1? What is b2? What is b3? What is b4?

Reading a Binary Number In general, a binary number is represented like this: bn-1 bn-2 … b2 b1 b0 Each digit bi is either 1 or 0. For example, consider binary number 10011. What is n? 5 What is b0? 1 What is b1? 1 What is b2? 0 What is b3? 0 What is b4? 1

Reading a Binary Number In general, a binary number is represented like this: bn-1 bn-2 … b2 b1 b0 This binary number can be translated to decimal using this formula: bn-1*2n-1 + bn-2*2n-2 + … + b2 * 22 + b1 * 21 + b0 * 20. As a quick review: What is 20? What is 21? What is 22? What is 23?

Reading a Binary Number In general, a binary number is represented like this: bn-1 bn-2 … b2 b1 b0 This binary number can be translated to decimal using this formula: bn-1*2n-1 + bn-2*2n-2 + … + b2 * 22 + b1 * 21 + b0 * 20. As a quick review: What is 20? 1 What is 21? 2 What is 22? 4 What is 23? 8

Translating from Binary to Decimal In general, a binary number is represented like this: bn-1 bn-2 … b2 b1 b0 This binary number can be translated to decimal using this formula: bn-1*2n-1 + bn-2*2n-2 + … + b2 * 22 + b1 * 21 + b0 * 20. So, how do we translate binary number 10011 to decimal?

Translating from Binary to Decimal In general, a binary number is represented like this: bn-1 bn-2 … b2 b1 b0 This binary number can be translated to decimal using this formula: bn-1*2n-1 + bn-2*2n-2 + … + b2 * 22 + b1 * 21 + b0 * 20. So, how do we translate binary number 10011 to decimal? 1*24 + 0*23 + 0*22 + 1*21 + 1*20 = 16 + 2 + 1 = 19.

Translating from Binary to Decimal In general, a binary number is represented like this: bn-1 bn-2 … b2 b1 b0 This binary number can be translated to decimal using this formula: bn-1*2n-1 + bn-2*2n-2 + … + b2 * 22 + b1 * 21 + b0 * 20. How do we translate binary number 101000 to decimal?

Translating from Binary to Decimal In general, a binary number is represented like this: bn-1 bn-2 … b2 b1 b0 This binary number can be translated to decimal using this formula: bn-1*2n-1 + bn-2*2n-2 + … + b2 * 22 + b1 * 21 + b0 * 20. How do we translate binary number 101000 to decimal? 1*25 + 0*24 + 1*23 + 0*22 + 0*21 + 0*20 = 32 + 8 = 40.

binaryToDecimal Function Let's write a function binaryToDecimal that translates a binary number into a decimal number. What arguments should the function take? What type should the function return?

binaryToDecimal Function Let's write a function binaryToDecimal that translates a binary number into a decimal number. What arguments should the function take? The binary number can be represented as a string. It can also be represented as an int. We will go with the string choice. What type should the function return? The decimal number should be an int.

binaryToDecimal Function public static int binaryToDecimal(String text) { int result = 0; for (int i = 0; i < text.length(); i++) String c = text.substring(i, i+1); if ("01".indexOf(c) == -1) System.out.printf("Error: invalid binary number %s, exiting...\n", text); System.exit(0); } int digit = Integer.parseInt(c); int power_of_2 = (int) (Math.pow(2, text.length() - i - 1)); result = result + digit * power_of_2; return result;

Translating from Decimal to Binary How can we translate a decimal number, such as 37, to binary?

Translating from Decimal to Binary How can we translate a decimal number, such as 37, to binary? Answer: we repeatedly divide by 2, until the result is 0, and use the remainder as a digit. 37 / 2 = 18, with remainder ???

Translating from Decimal to Binary How can we translate a decimal number, such as 37, to binary? Answer: we repeatedly divide by 2, until the result is 0, and use the remainder as a digit. 37 / 2 = 18, with remainder 1. So, b0 is 1. 18 / 2 = 9, with remainder ???

Translating from Decimal to Binary How can we translate a decimal number, such as 37, to binary? Answer: we repeatedly divide by 2, until the result is 0, and use the remainder as a digit. 37 / 2 = 18, with remainder 1. So, b0 is 1. 18 / 2 = 9, with remainder 0. So, b1 is 0. 9 / 2 = 4, with remainder ???

Translating from Decimal to Binary How can we translate a decimal number, such as 37, to binary? Answer: we repeatedly divide by 2, until the result is 0, and use the remainder as a digit. 37 / 2 = 18, with remainder 1. So, b0 is 1. 18 / 2 = 9, with remainder 0. So, b1 is 0. 9 / 2 = 4, with remainder 1. So, b2 is 1. 4 / 2 = 2, with remainder ???

Translating from Decimal to Binary How can we translate a decimal number, such as 37, to binary? Answer: we repeatedly divide by 2, until the result is 0, and use the remainder as a digit. 37 / 2 = 18, with remainder 1. So, b0 is 1. 18 / 2 = 9, with remainder 0. So, b1 is 0. 9 / 2 = 4, with remainder 1. So, b2 is 1. 4 / 2 = 2, with remainder 0. So, b3 is 0. 2 / 2 = 1, with remainder ???

Translating from Decimal to Binary How can we translate a decimal number, such as 37, to binary? Answer: we repeatedly divide by 2, until the result is 0, and use the remainder as a digit. 37 / 2 = 18, with remainder 1. So, b0 is 1. 18 / 2 = 9, with remainder 0. So, b1 is 0. 9 / 2 = 4, with remainder 1. So, b2 is 1. 4 / 2 = 2, with remainder 0. So, b3 is 0. 2 / 2 = 1, with remainder 0. So, b4 is 0. 1 / 2 = 0, with remainder ???

Translating from Decimal to Binary How can we translate a decimal number, such as 37, to binary? Answer: we repeatedly divide by 2, until the result is 0, and use the remainder as a digit. 37 / 2 = 18, with remainder 1. So, b0 is 1. 18 / 2 = 9, with remainder 0. So, b1 is 0. 9 / 2 = 4, with remainder 1. So, b2 is 1. 4 / 2 = 2, with remainder 0. So, b3 is 0. 2 / 2 = 1, with remainder 0. So, b4 is 0. 1 / 2 = 0, with remainder 1. So, b5 is 1. So, 37 in binary is ???

Translating from Decimal to Binary How can we translate a decimal number, such as 37, to binary? Answer: we repeatedly divide by 2, until the result is 0, and use the remainder as a digit. 37 / 2 = 18, with remainder 1. So, b0 is 1. 18 / 2 = 9, with remainder 0. So, b1 is 0. 9 / 2 = 4, with remainder 1. So, b2 is 1. 4 / 2 = 2, with remainder 0. So, b3 is 0. 2 / 2 = 1, with remainder 0. So, b4 is 0. 1 / 2 = 0, with remainder 1. So, b5 is 1. So, 37 in binary is 100101.

decimalToBinary Function Let's write a function decimalToBinary that translates a decimal number into a binary number. What arguments should the function take? What type should the function return?

decimalToBinary Function Let's write a function decimalToBinary that translates a decimal number into a binary number. What arguments should the function take? The decimal number should be an int. What type should the function return? Again, the most simple choice is to represent the binary number as a string.

decimalToBinary Function public static String decimalToBinary(int number) { String result = ""; while(true) int remainder = number % 2; String digit = Integer.toString(remainder); result = digit + result; number = number / 2; if (number == 0) break; } return result;

Hexadecimal Numbers Another very popular way to represent numbers among programmers is hexadecimal numbers. Hexadecimal numbers use 16 digits: 0123456789abcdef a stands for 10. b stands for 11. c stands for 12. d stands for 13. e stands for 14. f stands for 15.

Reading a Hexadecimal Number A hexadecimal number is represented like this: hn-1 hn-2 … h2 h1 h0 Each digit hi is one of the sixteen digits: 0123456789abcdef. For example, consider hexadecimal number 5b7. What is n? What is h0? What is h1? What is h2?

Reading a Hexadecimal Number A hexadecimal number is represented like this: hn-1 hn-2 … h2 h1 h0 Each digit hi is one of the sixteen digits: 0123456789abcdef. For example, consider hexadecimal number 5b7. What is n? 3 What is h0? 7 What is h1? b What is h2? 5

Reading a Hexadecimal Number A hexadecimal number is represented like this: hn-1 hn-2 … h2 h1 h0 This hexadecimal number can be translated to decimal using this formula: bn-1*16n-1 + bn-2*16n-2 + … + b2 * 162 + b1 * 161 + b0*160. Note: if bi is one of abcdef, use the corresponding number (10, 11, 12, 13, 14, 15) when you plug it into the formula. This looks like the formula for binary numbers, except that here we use powers of 16 instead of powers of 2.

From Hexadecimal to Decimal A hexadecimal number is represented like this: hn-1 hn-2 … h2 h1 h0 This hexadecimal number can be translated to decimal using this formula: bn-1*16n-1 + bn-2*16n-2 + … + b2 * 162 + b1 * 161 + b0*160. So, how do we translate hexadecimal number 5b7 to decimal?

From Hexadecimal to Decimal A hexadecimal number is represented like this: hn-1 hn-2 … h2 h1 h0 This hexadecimal number can be translated to decimal using this formula: bn-1*16n-1 + bn-2*16n-2 + … + b2 * 162 + b1 * 161 + b0*160. So, how do we translate hexadecimal number 5b7 to decimal? (note that we plug in 11 for b). 5*162 + 11*161 + 7*160 = 5*256 + 11*16 + 7 = 1463.

From Hexadecimal to Decimal A hexadecimal number is represented like this: hn-1 hn-2 … h2 h1 h0 This hexadecimal number can be translated to decimal using this formula: bn-1*16n-1 + bn-2*16n-2 + … + b2 * 162 + b1 * 161 + b0*160. So, how do we translate hexadecimal number bad to decimal?

From Hexadecimal to Decimal A hexadecimal number is represented like this: hn-1 hn-2 … h2 h1 h0 This hexadecimal number can be translated to decimal using this formula: bn-1*16n-1 + bn-2*16n-2 + … + b2 * 162 + b1 * 161 + b0*160. So, how do we translate hexadecimal number bad to decimal? 11*162 + 10*161 + 13*160 = 11*256 + 10*16 + 13 = 2989.

hexToDecimal Function Let's write a function hexToDecimal that translates a hexadecimal number into a decimal number. What arguments should the function take? What type should the function return?

hexToDecimal Function Let's write a function hexToDecimal that translates a hexadecimal number into a decimal number. What arguments should the function take? The hexadecimal number can be represented as a string. What type should the function return? The decimal number should be an int.

hexToDecimal Function public static int hexToDecimal(String text) { int result = 0; String digits = "0123456789abcdef"; for (int i = 0; i < text.length(); i++) String c = text.substring(i, i+1); int digit = digits.indexOf(c); if (digit == -1) System.out.printf("Error: invalid hexadecimal number %s, exiting...\n", text); System.exit(0); } int power = (int) (Math.pow(16, text.length() - i - 1)); result = result + digit * power; return result;

From Decimal to Hexadecimal How can we translate a decimal number, such as 540, to hexadecimal?

From Decimal to Hexadecimal How can we translate a decimal number, such as 540, to hexadecimal? Answer: we repeatedly divide by 16, until the result is 0, and use the remainder as a digit. If the remainder is 10, 11, 12, 13, 14, or 15, we use respectively digit a, b, c, d, e, or f. 540 / 16 = 33, with remainder 12. So, h0 is ???

From Decimal to Hexadecimal How can we translate a decimal number, such as 540, to hexadecimal? Answer: we repeatedly divide by 16, until the result is 0, and use the remainder as a digit. If the remainder is 10, 11, 12, 13, 14, or 15, we use respectively digit a, b, c, d, e, or f. 540 / 16 = 33, with remainder 12. So, h0 is c. 33 / 16 = ??? with remainder ???

From Decimal to Hexadecimal How can we translate a decimal number, such as 540, to hexadecimal? Answer: we repeatedly divide by 16, until the result is 0, and use the remainder as a digit. If the remainder is 10, 11, 12, 13, 14, or 15, we use respectively digit a, b, c, d, e, or f. 540 / 16 = 33, with remainder 12. So, h0 is c. 33 / 16 = 2 with remainder 1. So, h1 is 1. 2 / 16 = ??? with remainder ???

From Decimal to Hexadecimal How can we translate a decimal number, such as 540, to hexadecimal? Answer: we repeatedly divide by 16, until the result is 0, and use the remainder as a digit. If the remainder is 10, 11, 12, 13, 14, or 15, we use respectively digit a, b, c, d, e, or f. 540 / 16 = 33, with remainder 12. So, h0 is c. 33 / 16 = 2 with remainder 1. So, h1 is 1. 2 / 16 = 0 with remainder 2. So, h2 is 2. So, 540 in hexadecimal is ???

From Decimal to Hexadecimal How can we translate a decimal number, such as 540, to hexadecimal? Answer: we repeatedly divide by 16, until the result is 0, and use the remainder as a digit. If the remainder is 10, 11, 12, 13, 14, or 15, we use respectively digit a, b, c, d, e, or f. 540 / 16 = 33, with remainder 12. So, h0 is c. 33 / 16 = 2 with remainder 1. So, h1 is 1. 2 / 16 = 0 with remainder 2. So, h2 is 2. So, 540 in hexadecimal is 21c.

decimalToHexadecimal Function Let's write a function decimalToHexadecimal that translates a decimal number into a hexadecimal number. What arguments should the function take? What type should the function return?

decimalToHexadecimal Function Let's write a function decimalToHexadecimal that translates a decimal number into a hexadecimal number. What arguments should the function take? The decimal number should be an int. What type should the function return? The hexadecimal number should be a string.

decimalToHexadecimal Function public static String decimalToHexadecimal(int number) { String result = ""; String digits = "0123456789abcdef"; while(true) int remainder = number % 16; String digit = digits.substring(remainder, remainder+1); result = digit + result; number = number / 16; if (number == 0) break; } return result;