Computer Science 101 Number Systems. Humans Decimal Numbers (base 10) Decimal Numbers (base 10) Sign-Magnitude (-324) Sign-Magnitude (-324) Decimal Fractions.

Slides:



Advertisements
Similar presentations
A-Level Computing#BristolMet Session Objectives#9 express numbers in binary, binary-coded decimal (BCD), octal and hexadecimal; describe and use two’s.
Advertisements

HEXADECIMAL NUMBERS Code
Computer Science 101 Binary Systems. Humans Decimal Numbers (base 10) Decimal Numbers (base 10) Sign-Magnitude (-324) Sign-Magnitude (-324) Decimal Fractions.
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
Binary Representation Introduction to Computer Science and Programming I Chris Schmidt.
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Representations Example: Numbers –145 –CVL – –91 –
Connecting with Computer Science, 2e
Assembly Language and Computer Architecture Using C++ and Java
Introduction to Programming with Java, for Beginners
Assembly Language and Computer Architecture Using C++ and Java
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
1 CSE1301 Computer Programming Lecture 28 Number Representation (Integer)
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
Professor Jennifer Rexford COS 217
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
Number System and Codes
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Computer Fluency Binary Systems. Humans Decimal Numbers (base 10) Decimal Numbers (base 10) Sign-Magnitude (-324) Sign-Magnitude (-324) Decimal Fractions.
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
Number Systems Computer Science 210 Computer Organization.
Chapter 2 Bits, Data Types, and Operations. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-2 How do we.
Agenda Data Representation Purpose Numbering Systems Binary, Octal, Hexadecimal (Hex) Numbering System Conversions Binary to Octal, Octal to Binary Binary.
Computer Science 101 HTML. World Wide Web Invented by Tim Berners-Lee at CERN, the European Laboratory for Particle Physics in Geneva, Switzerland (roughly.
Real Numbers and the Decimal Number System
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Simple Data Type Representation and conversion of numbers
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
Lecture 5.
Computers Organization & Assembly Language
Computer Science 111 Fundamentals of Programming I Number Systems.
IT253: Computer Organization
Data Representation and Computer Arithmetic
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
PART 2 Data Types 1. Integers 2. 2’s Complement 3. Conversion
Data Representation Dr. Ahmed El-Bialy Dr. Sahar Fawzy.
46 Number Systems Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs Display: Seven segment displays Inputs: Switches.
Number Systems and Codes. CS2100 Number Systems and Codes 2 NUMBER SYSTEMS & CODES Information Representations Number Systems Base Conversion Negative.
The Teacher CP4 Binary and all that… CP4 Revision.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
Lecture 2 Binary Values and Number Systems. The number 943 is an example of a number written in positional notation. The relative positions of the digits.
The Teacher CP4 Binary and all that… CP4 Revision.
1 COMS 161 Introduction to Computing Title: The Digital Domain Date: September 6, 2004 Lecture Number: 6.
AEEE2031 Data Representation and Numbering Systems.
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 2: Number Systems & x86 Instructions Constantine D. Polychronopoulos Professor, ECE Office: 463.
COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.
Number Representation (Part 1) Computer Architecture (Fall 2006)
HTML & Color How to Use Color  Backgrounds-  Usually a light color is best  Should have a color based on a theme or plan  Can have a dark.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
69 Decimal (Base 10) Numbers n Positional system - each digit position has a value n 2534 = 2*1, * *10 + 4*1 n Alternate view: Digit position.
Lecture 2: 8/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
1 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
Computer Science 210 Computer Organization
Bits, Data Types, and Operations
Computer Science 210 Computer Organization
CSE 102 Introduction to Computer Engineering
University of Gujrat Department of Computer Science
Computer Science 210 Computer Organization
Data Representation Data Types Complements Fixed Point Representation
Data Representation in Computer Systems
Computer Science 210 Computer Organization
Presentation transcript:

Computer Science 101 Number Systems

Humans Decimal Numbers (base 10) Decimal Numbers (base 10) Sign-Magnitude (-324) Sign-Magnitude (-324) Decimal Fractions (23.27) Decimal Fractions (23.27) Letters for text Letters for text

Computers Binary Numbers (base 2) Binary Numbers (base 2) Two’s complement and sign-magnitude Two’s complement and sign-magnitude Binary fractions and floating point Binary fractions and floating point ASCII codes for characters (A  65) ASCII codes for characters (A  65)

Why binary? Information is stored in computer via voltage levels. Information is stored in computer via voltage levels. Using decimal would require 10 distinct and reliable levels for each digit. Using decimal would require 10 distinct and reliable levels for each digit. This is not feasible with reasonable reliability and financial constraints. This is not feasible with reasonable reliability and financial constraints. Everything in computer is stored using binary: numbers, text, programs, pictures, sounds, videos,... Everything in computer is stored using binary: numbers, text, programs, pictures, sounds, videos,...

Morse Code Morse Code

Morse Code Tree

Decimal: Non-negatives Base 10 Base 10 Uses decimal digits: 0,1,2,3,4,5,6,7,8,9 Uses decimal digits: 0,1,2,3,4,5,6,7,8,9 Positional System - position gives power of the base Positional System - position gives power of the base Example: 3845 = 3x x x x10 0 Example: 3845 = 3x x x x10 0 Positions: … Positions: …543210

Binary: Non-negatives Base 2 Base 2 Uses binary digits (bits): 0,1 Uses binary digits (bits): 0,1 Positional system Positional system Example: 1101 = 1x x x x2 0 Example: 1101 = 1x x x x2 0

Conversions ExternalInternal (Human) (Computer) A ExternalInternal (Human) (Computer) A Humans want to see and enter numbers in decimal. Humans want to see and enter numbers in decimal. Computers must store and compute with bits. Computers must store and compute with bits.

Binary to Decimal Conversion Algorithm: Algorithm: Expand binary number using positional scheme.Expand binary number using positional scheme. Perform computation using decimal arithmetic.Perform computation using decimal arithmetic. Example:  1x x x x x2 0 = = = Example:  1x x x x x2 0 = = = 25 10

Decimal to Binary - Algorithm 1 Algorithm: While N  0 do Set N to N/2 (whole part) Record the remainder (1 or 0) end-of-loop Set A to remainders in reverse order Algorithm: While N  0 do Set N to N/2 (whole part) Record the remainder (1 or 0) end-of-loop Set A to remainders in reverse order

Decimal to binary - Example Example: Convert to binary N Rem N Rem Example: Convert to binary N Rem N Rem = =

Decimal to Binary - Algorithm 2 Algorithm: Set A to 0 (all bits 0) While N  0 do Find largest P with 2 P  N Set bit in position P of A to 1 Set N to N - 2 P end-of-loop Algorithm: Set A to 0 (all bits 0) While N  0 do Find largest P with 2 P  N Set bit in position P of A to 1 Set N to N - 2 P end-of-loop

Decimal to binary - Example Example: Convert to binary N Power P A Example: Convert to binary N Power P A = =

Binary Addition One bit numbers: | | 1 10 One bit numbers: | | 1 10 Example (53) (45) (98) Example (53) (45) (98)

Overflow In a given type of computer, the size of integers is a fixed number of bits. In a given type of computer, the size of integers is a fixed number of bits. 16 or 32 bits are popular choices 16 or 32 bits are popular choices It is possible that addition of two n bit numbers yields a result requiring n+1 bits. It is possible that addition of two n bit numbers yields a result requiring n+1 bits. Overflow is the term for an operation whose results exceed the size allowed for a number. Overflow is the term for an operation whose results exceed the size allowed for a number.

Overflow Example Suppose we are dealing with 5 bit numbers; so a number is not allowed to be more than 5 bits (really restrictive, but for an example this is fine) (decimal 21) (decimal 20) (1)01001 (decimal 9) Suppose we are dealing with 5 bit numbers; so a number is not allowed to be more than 5 bits (really restrictive, but for an example this is fine) (decimal 21) (decimal 20) (1)01001 (decimal 9)

Negatives: Sign-Magnitude system With a fixed number of bits, say N With a fixed number of bits, say N The leftmost bit is used to give the signThe leftmost bit is used to give the sign  0 for positive number  1 for negative number The other N-1 bits are for the magnitudeThe other N-1 bits are for the magnitude Example: -25 with 8 bit numbers Example: -25 with 8 bit numbers Sign: 1 since negative Sign: 1 since negative Magnitude: for 25 Magnitude: for 25 8-bit result: bit result: Note: This would be 153 as a positive. Note: This would be 153 as a positive.

Sign-Magnitude: Pros and Cons Pro: Pro: Easy to comprehendEasy to comprehend Easy to convertEasy to convert Con: Con: Addition complicated (expensive) If signs same then … else if positive part larger …Addition complicated (expensive) If signs same then … else if positive part larger … Two representations of 0Two representations of 0

Negatives: Two’s complement system Same as sign-magnitude for positives Same as sign-magnitude for positives With N bit numbers, to compute negative With N bit numbers, to compute negative Invert all the bitsInvert all the bits Add 1Add 1 Example: -25 in 8-bit two’s complement Example: -25 in 8-bit two’s complement 25   Invert bits: Invert bits: Add 1: Add 1:

2’s Complement: Pros and Cons Con: Con: Not so easy to comprehendNot so easy to comprehend Human must convert negative to identifyHuman must convert negative to identify Pro: Pro: Addition is exactly same as for positives No additional hardware for negatives, and subtraction.Addition is exactly same as for positives No additional hardware for negatives, and subtraction. One representation of 0One representation of 0

2’s Complement: Examples Compute negative of -25 (8-bits) Compute negative of -25 (8-bits) We found -25 to be We found -25 to be Invert bits: Invert bits: Add 1: Add 1: Recognize this as 25 in binary Recognize this as 25 in binary Add -25 and 37 (8-bits) Add -25 and 37 (8-bits) (-25) ( 37) (1) (-25) ( 37) (1) Recognize as 12 Recognize as 12

Facts about 2’s Complement Leftmost bit still tells whether number is positive or negative as with sign-magnitude Leftmost bit still tells whether number is positive or negative as with sign-magnitude 2’s complement is same as sign-magnitude for positives 2’s complement is same as sign-magnitude for positives

2’s complement to decimal (examples) Assume we’re using 8-bit 2’s complement: Assume we’re using 8-bit 2’s complement: X = X = = = = 39 (decimal) So, X = -39 X = X = = = = 39 (decimal) So, X = -39 X = Since X is positive, we have X = = 89 X = Since X is positive, we have X = = 89

Ranges for N-bit numbers Unsigned (positive) Unsigned (positive) 0000…00 or …00 or …11 which is 2 N …11 which is 2 N -1 For N=8, 0 – 255 For N=8, 0 – 255 Sign-magnitude Sign-magnitude 1111…11which is -(2 N-1 -1) 1111…11which is -(2 N-1 -1) 0111…11which is 2 N …11which is 2 N-1 -1 For N=8, -127 to 127 For N=8, -127 to 127 2’s Complement 2’s Complement 1000…00 which is -2 N …00 which is -2 N …11which is 2 N …11which is 2 N For N=8, -128 to 127 For N=8, -128 to 127

2’s Complement Overflow Example Assume we are using 5-bit 2’s complement numbers (decimal 9) (decimal 13) (decimal -10) Assume we are using 5-bit 2’s complement numbers (decimal 9) (decimal 13) (decimal -10)

Overflow Example (Adds and displays sum)

Overflow - Explanation We had = -6 We had = -6 Why? Why? = and has 32 bit binary representation 0111…111. This is largest 2’s complement 32 bit number = and has 32 bit binary representation 0111…111. This is largest 2’s complement 32 bit number would have representation … would have representation …101. When we add this to itself, we get X = 1111…1010 (overflow)When we add this to itself, we get X = 1111…1010 (overflow) So, -X would be 000… = 00…0110 = 6So, -X would be 000… = 00…0110 = 6 So, X must be -6.So, X must be -6.

Python and integer overflow

Python – type long In Python, when whole numbers get too big for the 32 bits allotted, they are converted to a type called “long”. In Python, when whole numbers get too big for the 32 bits allotted, they are converted to a type called “long”. Numbers of this type are allowed to grow arbitrarily long (restricted by available memory). Numbers of this type are allowed to grow arbitrarily long (restricted by available memory). This is handled by software of Python system. This is handled by software of Python system.

Python example

Octal Numbers Base 8 Digits 0,1,2,3,4,5,6,7 Base 8 Digits 0,1,2,3,4,5,6,7 Number does not have so many digits as binary Number does not have so many digits as binary Easy to convert to and from binary Easy to convert to and from binary Often used by people who need to see the internal representation of data, programs, etc. Often used by people who need to see the internal representation of data, programs, etc.

Octal Conversions Octal to Binary Octal to Binary Simply convert each octal digit to a three bit binary number. Simply convert each octal digit to a three bit binary number. Example: = Example: = Binary to Octal Binary to Octal Starting at right, group into 3 bit sections Starting at right, group into 3 bit sections Convert each group to an octal digit Convert each group to an octal digit Example = = Example = =

Hexadecimal Base 16 Digits 0,…,9,A,B,C,D,E,F Base 16 Digits 0,…,9,A,B,C,D,E,F Hexadecimal  Binary Hexadecimal  Binary Just like Octal, only use 4 bits per digit. Just like Octal, only use 4 bits per digit. Example: 98C3 16 = Example: 98C3 16 = Example = = 34EB Example = = 34EB

Python example

Red Green Blue - RGB In this system colors are created from the primary colors Red, Green and Blue. In this system colors are created from the primary colors Red, Green and Blue. A color is specified by giving three values in the range A color is specified by giving three values in the range The first number is the Red value, the second is Green and third Blue. The first number is the Red value, the second is Green and third Blue.

RGB - Examples R=212 G=88 B=200 R=240 G=244 B=56 R=150 G=150 B=150 R=255 G=255 B=255

RGB - In binary R=212 G=88 B=200 R = 212  R = 212  G = 88  G = 88  B = 200  B = 200  Color stored in 3 eight bit groups: Color stored in 3 eight bit groups: Using 24 bits this way, there would be about 16 million colors. Using 24 bits this way, there would be about 16 million colors.

RGB - In hexadecimal R=212 G=88 B=200 Color stored in 3 eight bit groups: Color stored in 3 eight bit groups: Note that each 8 bit group can be expressed with two hex digits is given by D is is C8 Note that each 8 bit group can be expressed with two hex digits is given by D is is C8 Color given by D454C8 in hexadecimal Color given by D454C8 in hexadecimal

Background Color We can add a background color to our web page by adding a BGColor attribute to the Body tag: We can add a background color to our web page by adding a BGColor attribute to the Body tag: The value can be either a “known” color or a color specified with the 6 hex digit system. The value can be either a “known” color or a color specified with the 6 hex digit system.

Background Color (cont.) There is a long list of “known” colors, but only 16 that are guaranteed to validate with all browsers: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow There is a long list of “known” colors, but only 16 that are guaranteed to validate with all browsers: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow To specify a background color with hex digits use the form for example To specify a background color with hex digits use the form for example