Binary Arithmetic Math For Computers.

Slides:



Advertisements
Similar presentations
Week 3. Assembly Language Programming  Difficult when starting assembly programming  Have to work at low level  Use processor instructions >Requires.
Advertisements

HEXADECIMAL NUMBERS Code
DATA REPRESENTATION Y. Colette Lemard February
COE 202: Digital Logic Design Signed Numbers
Binary Addition Rules Adding Binary Numbers = = 1
Floating Point Numbers
First project Create a JApplet with 3 textfields (2 for input and one for the answer), appropriate labels, and buttons for plus, minus and times. Your.
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
Computer Structures Lecture 2: Working with 0’s and 1’s.
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Assembly Language and Computer Architecture Using C++ and Java
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Signed Numbers.
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 =
CSE 378 Floating-point1 How to represent real numbers In decimal scientific notation –sign –fraction –base (i.e., 10) to some power Most of the time, usual.
IT Systems Number Operations EN230-1 Justin Champion C208 –
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
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.
Exponents Scientific Notation
Binary “There are 10 types of people in the world… those that understand binary and those that don’t.”
CENG 311 Machine Representation/Numbers
Simple Data Type Representation and conversion of numbers
Numbers and number systems
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Number Systems Part 2 Numerical Overflow Right and Left Shifts Storage Methods Subtraction Ranges.
Numeration Systems Introduction to Binary, Octal, and Hexadecimal.
IT253: Computer Organization
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
Digital Logic Design Lecture 3 Complements, Number Codes and Registers.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
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.
CSC 221 Computer Organization and Assembly Language
Floating Point Arithmetic
Week 2 - Wednesday.  What did we talk about last time?  C compilation model  Lab 1.
Operations on Bits Arithmetic Operations Logic Operations
Number Systems Revision of conversations What is a register Addition Complementation.
How a Computer Processes Information. Java – Numbering Systems OBJECTIVE - Introduction to Numbering Systems and their relation to Computer Problems Review.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Addition and Substraction
Computer Science Introduction to the Number Base Unit Adapted from Slides by John Owen Computer Science Instructor, Rockport-Fulton High School, Rockport,
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
Numerical formats What’s the main idea? Want to represent numbers (eg: 45, -12, ) using only bits. We’ve already seen (or you can read in the book)
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
{ Binary “There are 10 types of people in the world: Those who understand binary, and those who don't.”
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Dr. ClincyLecture 2 Slide 1 CS Chapter 2 (1 of 5) Dr. Clincy Professor of CS Note: Do not study chapter 2’s appendix (the topics will be covered.
PEMDAS, Calculators, %s, Decimals, & Zeros. P. stands for “Parentheses”… Cute story.
Read the instruments below
William Stallings Computer Organization and Architecture 7th Edition
Addition and Substraction
Topic 3: Data Binary Arithmetic.
Subtraction The arithmetic we did so far was limited to unsigned (positive) integers. Today we’ll consider negative numbers and subtraction. The main problem.
How to represent real numbers
Binary & Hex Review.
Presentation transcript:

Binary Arithmetic Math For Computers

Huh? Binary numbers are NUMBERS That means you can add, subtract, multiply, and divide 2 + 2 = 4 In Binary: 10 + 10 = 100 So you COULD just convert all numbers to decimal, do the math, and then convert the answer back… But I don’t care about the math I want you to understand how a computer does it!

Addition Similar to addition of large decimal numbers You need to “carry” when a number gets too large for a single digit In binary, that’s 1 + 1 = 10 (or 0, carry a 1) If you have 1 + 1 + 1 = 11 (or 1, carry a 1) carried bits 11_ 11 0011 + 1011 11 1110

Addition Practice 1110 + 1010 1001 + 111 1111 0000 + 1111 111 1000 + 1111 1000 1100 + 1100 0110 Take a few minutes to try them (answers in the PowerPoint Notes) 1110 + 1010 = 1 1000, 1001 + 111 = 1 0000, 1111 0000 + 1111 = 1111 1111, 111 1000 + 1111 = 10000111, 1000 1100 + 1100 0110 = 101010010

Overflow 1111 1111 + 100 = 1 0000 0011 The answer is more than 1 byte large A computer typically will make it DROP THE EXTRA BIT ON THE LEFT The computer’s answer: 11 (binary) or 3 (decimal) This is called an overflow error Sometimes overflow behavior is undefined (unpredictable)

Why overflow happens A computer’s processor stores information in something called a register. Registers have a limited space – they can only store a certain number of bits. If a processor does a calculation and the answer exceeds the capacity of the register, then the extra bits are dropped Modern registers are usually 16 or 32 bits, but for this class we’ll only use 8 bits.

Addition Practice Pt. 2 Do the math, but give the answer an 8-bit computer would give 1111 1111 + 1010 1010 1100 + 111 1111 1000 0000 + 1000 0000 1101 1010 + 1110 0110 (answers in the PowerPoint Notes) 1001, 10 1011, 0 , 11000000

Negative Numbers for Computers A computer needs a way to represent negative numbers (there’s no “negative sign” in the comp) One Idea: Use one of the bits to indicate the sign of the number, instead of using it as a digit

Sign Bit (the bad way) So the 1st bit of a number indicates it’s sign Examples: 00000010 is 2 10000010 is -2

Problem with sign bit system 2 zeros is a waste (10000000 and 00000000 are both zero) Computer processors can’t subtract without special instructions We need a way to subtract by adding! Huh?

Twos Complement Consider this idea: a binary digit “place value” could be negative! So the above number is actually 3 (the positive bits) minus 8 = -5 1000 in the above system actually represents the decimal number -8 The example above is 4 bits. Most problems for this class will assume 8 bits. negative eights fours twos ones 1

Twos Complement cont. REALLY IMPORTANT: Notice that with both negative number systems you need to know the number of total bits you are going to use! We’ll assume 8 bits for simplicity. What is the value of the “negative place”? What is the new range of numbers? (Hint: It’s not 0 – 255 anymore) Value of negative place is -128, range of numbers is now -128 to 127

Calculating Twos Complement Example: -1 in (8-bit) twos complement is 1111 1111 Still confused? Remember, everything is the same except for a negative place value! -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1 Btw, a “normal” binary number (where 1111 1111 = 255) is called “unsigned” -128 64 32 16 8 4 2 1

Calculating Twos Complement Pt. 2 Shortcut: Convert the positive binary number, switch all the bits (0s become 1s, 1s become 0s), then add 1 It only doesn’t work for -128 (no positive number) Try some! Set 1 -127 -23 -8 -100 Set 2 -5 -117 -52 -12 Set 1: 1000 0001, 1110 1001, 1111 1000, 1001 1100 Set 2: 1111 1011, 1000 1011, 1100 1100, 1111 0100

Actually Subtracting, finally Once you’ve got Twos Complement figured out, subtracting is IDENTICAL TO ADDING (but now using our twos complement numbers) Example: Decimal 100 - 20 Binary: 0110 0100 - 0001 0100 2s complement of 20: 1110 1100 Overflow bit is dropped, as usual 0101 0000 is… 80 So this 2s complement system USES overflow 0110 0100 +1110 1100 1 0101 0000

Practice Excercises Do in Binary (know how to do decimal conversions, 2s complement conversions, and binary addition) Set 1 12 – 2 66 – 30 35 – 44 Set 2 50 - 10 2 - 1 127 - 128 12 – 2 = 1100 + 11111110, 66 – 30 = 1000010 + 11100010, 35 – 44 = 100011 + 11010100 50 – 10 = 110010 + 11110110, 2 – 1 = 10 + 11111111, 127 – 128 = 1111111 + 10000000

Just to be clear: If you have a binary number like 1101 0110 It could be 214, or -42 There’s no way to tell if it’s negative just by looking at it! Assume it is positive, unless the problem states otherwise.

Multiplication & Division We’ll just talk about multiplying and dividing by the powers of 2 This is called “shifting” Just like when you multiply or divide by 10, you just shift the decimal point In binary, when you multiply or divide by 2, you shift the “binary point” 4 * 2 = 8 100 * 10 = 1000 4 / 2 = 2 100 / 10 = 10

Shifting Details Multiplying can also be called left-shifting Dividing: right-shifting Left-Shifting can give you the overflow error 1000 0000 * 10 = 1 0000 0000 For 2s complement negative numbers, overflow will give really weird results Again, the leftmost bit is dropped Right-shift is DIFFERENT FOR TWOS COMPLEMENT vs. unsigned binary numbers Unsigned: shift in a 0 for the leftmost bit 2s comp: shift in a COPY of the leftmost bit

These two 1s are dropped and disappear due to overflow Shifting Diagram Left-Shifting by 2 (multiply by 4) Right-Shifting by 2 (divide by 4) 1111 1111 1111 1111 11 1111 1100 These 1s are dropped! Twos Complement 1111 1111 11 These two 1s are dropped and disappear due to overflow Unsigned Two copies of the 1 were shifted in on the left. If the leftmost bit was 0, two 0s would have been shifted in. 0011 1111 11 0s are always shifted in for unsigned numbers

More Shifting Examples Unsigned numbers 1011 0111 * 10 = 0110 1110 (overflow) 1111 1111 * 100 = 1111 1100 (overflow) 0001 1111 * 1000 = 1111 1000 1000 0000 / 1 0000 = 0000 1000 0011 0010 / 10 = 0001 1001 0001 0011 / 100 = 0000 0100 (?????) 19 / 4 = 4 So when you divide, you lose precision (the computer will drop bits that go off the right side – this means the answer is always rounded down towards -∞) Twos Complement 1111 1111 * 10 = 1111 1110 1000 0001 * 10 = 0000 0010 (overflow!) -127 * 2 = 2…? 1001 1100 / 100 = 1110 0111 Instead of 0s, 1s were shifted in on the left because that was the leftmost bit of the original byte

Fancy Shifting Animation: Multiplication View this slide in a slide show! Fancy Shifting Animation: Multiplication Also called left-shifting (look at the animation) This red box is a register 0 1 1 0 1 0 1 1 0 0 0 Lets put some bits into the register (8 bits) And the bits that went out of the register are dropped because of overflow Now the processor gets an instruction to multiply this number by 8 So the bits shift to the left by 3 places (because 8 is the 3rd power of 2) And the new empty spaces are filled with zeros Notice that we won’t get the correct answer, because of overflow. If the original number was smaller and had three zeros on the left, then the overflow would have only dropped zeros, and the answer would be accurate.

Negative Number Multiplication Overflow View this slide in a slide show! Negative Number Multiplication Overflow For Negative numbers, be aware that overflow will yield some strange results 1 0 1 0 1 1 0 1 The computer erases the bit that overflowed and puts in a zero on the right, like it’s supposed to. But the new number is 01011010, which equals 90! This happened because the lower limit of 8-bit twos complement negative numbers is -128. However, -83 * 2 would have gone below that. Let’s multiply by 2. This means we left-shift by 1. Take this 8-bit negative binary number: 10101101 = -83 (or positive 173)

Fancy Shifting Animation: Division View this slide in a slide show! Fancy Shifting Animation: Division Also called right-shifting (look at the animation) 1 0 1 0 1 0 1 1 0 0 1 1 The computer drops the bits that went out of the register to the right. If the original number was positive (171), then the computer will put zeros on the left. The final answer is: 42. Notice that this isn’t a totally accurate answer (it should be 42.75) Because the computer dropped some bits we lost precision. Basically, the computer will always round down. Dividing by powers of 2 is very similar to multiplying. Here, we have our original binary number in the register. (10101011 = 171 OR -85) If the original number was negative (-85), then the only difference would be that ones come in on the left side instead of zeros. This time our answer is -22. It should be -21.75, but the computer rounded down again. Just remember that when you round a negative number DOWN, it becomes more negative! Let’s try dividing by 4. When that happens, the number in the register will shift to the right by 2 places. (4 is the 2nd power of 2)

Microsoft Calculator It’s actually useful for this binary stuff Use it to double-check and test yourself Put it into Scientific mode (under view) and you’ll see buttons for decimal, binary, hex, and octal

Calculator Cont. If you click on the decimal (Dec) button and then enter a negative decimal number, then click the Binary (Bin) button, you’ll see that negative number in Twos Complement form If you do any arithmetic in Binary, and the Byte button is activated, you’ll see the “computer’s answer” (overflow) You can then click over to Decimal to see what that number would be

Shifting Excercises Convert to Binary, then give the computer’s answers Use Calc for the initial conversions and answer-checking Btw, I use an asterisk (*) for multiplication -50 * 2 12 * 4 78 * 4 -100 * 2 10 / 2 -120 / 8 12 / 8 -12 / 4

Restating the Obvious The MATH ISN’T IMPORTANT. I can get the answers from a calculator. Understanding these processes gives you an idea of how the computer works. If the only thing you understand is that computers are actually pretty simple machines that need lots of instructions to work properly, then you’re doing pretty good (ok, you won’t so well on the test if you can’t do the calculations, but at least you have the general idea)