Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Similar documents
Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Binary Arithme-c CS 64: Computer Organiza-on and Design Logic Lecture #2

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

Binary Arithme-c 2 Intro to Assembly CS 64: Computer Organiza-on and Design Logic Lecture #3

COMP 122/L Lecture 2. Kyle Dewey

Beginning C Programming for Engineers

data within a computer system are stored in one of 2 physical states (hence the use of binary digits)

CSCI 2212: Intermediate Programming / C Chapter 15

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

CS 64 Week 0 Lecture 1. Kyle Dewey

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Integer Representation

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

CS & IT Conversions. Magnitude 10,000 1,

Signed Binary Numbers

BINARY SYSTEM. Binary system is used in digital systems because it is:

CS 64 Week 1 Lecture 1. Kyle Dewey

Final Labs and Tutors

Module 1: Information Representation I -- Number Systems

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Octal and Hexadecimal Integers

Inf2C - Computer Systems Lecture 2 Data Representation

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

EE292: Fundamentals of ECE

Lecture 13 Bit Operations

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Chapter 2 Number System

CS 261 Fall Binary Information (convert to hex) Mike Lam, Professor

TOPICS. Other Number Systems. Other Number Systems 9/9/2017. Octal Hexadecimal Number conversion

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-2700: Digital Logic Design Winter Notes - Unit 4. hundreds.

Arithmetic Operations

Learning the Binary System

T02 Tutorial Slides for Week 2

CMPE223/CMSE222 Digital Logic Design. Positional representation

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

What Is It? Instruction Register Address Register Data Register

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

CS 107 Lecture 2: Bits and Bytes (continued)

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-278: Digital Logic Design Fall Notes - Unit 4. hundreds.

Arithmetic and Bitwise Operations on Binary Data

Number Representations

Chapter 1 Review of Number Systems

Number System. Introduction. Decimal Numbers

Module 2: Computer Arithmetic

Fundamentals of Programming Session 2

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

COMP2611: Computer Organization. Data Representation

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Chapter 2: Number Systems

Welcome to Computer Organization and Design Logic CS 64: Computer Organization and Design Logic Lecture #1 Winter 2018

Representation of Information

Computer Architecture and Organization: L04: Micro-operations

Number representations

COMP Overview of Tutorial #2

Electronic Data and Instructions

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

Intermediate Programming & Design (C++) Notation

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

Bitwise Instructions

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

CS33 Project Gear Up. Data

Harry H. Porter, 2006

Number Systems CHAPTER Positional Number Systems

Computer Organization & Systems Exam I Example Questions

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

BITWISE OPERATORS. There are a number of ways to manipulate binary values. Just as you can with

CPE300: Digital System Architecture and Design

Topic Notes: Bits and Bytes and Numbers

Survey. Motivation 29.5 / 40 class is required

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

Level ISA3: Information Representation

Microcomputers. Outline. Number Systems and Digital Logic Review

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

Number Systems and Their Representations

MC1601 Computer Organization

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Arithmetic Processing

Unit 2: Data Storage CS 101, Fall 2018

EE 109 Unit 6 Binary Arithmetic

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Topic Notes: Bits and Bytes and Numbers

Bits, Bytes and Integers

Binary Values. CSE 410 Lecture 02

Arithmetic for Computers

CHAPTER 1 Numerical Representation

Bits, Words, and Integers

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

Applied Computer Programming

Advanced Computer Architecture-CS501

Solutions - Homework 2 (Due date: October 4 5:30 pm) Presentation and clarity are very important! Show your procedure!

The Design of C: A Rational Reconstruction

Content. Information Coding. Where are we? Where are we? Bit vectors (aka Words )

Chapter 4. Operations on Data

Xuan Guo. Lecture XIV: Review of Chapter 3 & 4. CSC 3210 Computer Organization and Programming Georgia State University. March 5, 2015.

Introduction to Computers and Programming. Numeric Values

Extended Introduction to Computer Science CS1001.py. Lecture 5 Part A: Integer Representation (in Binary and other bases)

Transcription:

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Ziad Matni Dept. of Computer Science, UCSB

Adding this Class The class is full I will not be adding more ppl L Even if others drop 4/5/18 Matni, CS64, Sp18 2

Lecture Outline Review of positional notation, binary logic Bitwise operations Bit shift operations Two s complement Addition and subtraction in binary Multiplication in binary 4/5/18 Matni, CS64, Sp18 3

Positional Notation in Decimal Continuing with our example 642 in base 10 positional notation is: 6 x 10 2 + 4 x 10 1 + 2 x 10 0 = 6 x 100 = 600 = 4 x 10 = 40 = 2 x 1 = 2 = 642 in base 10 6 4 2 100 10 1 642 (base 10) = 600 + 40 + 2 4/5/18 Matni, CS64, Sp18 4

Positional Notation This is how you convert any base number into decimal! What if 642 is expressed in the base of 13? 6 x 13 2 = 6 x 169 = 1014 + 4 x 13 1 = 4 x 13 = 52 + 2 x 13º = 2 x 1 = 2 6 4 2 13 2 13 1 13 0 642 (base 13) = 1014 + 52 + 2 = 1068 (base 10) 4/5/18 Matni, CS64, Sp18 5

Positional Notation in Binary 11101 in base 2 positional notation is: 1 x 2 4 = 1 x 16 = 16 + 1 x 2 3 = 1 x 8 = 8 + 1 x 2 2 = 1 x 4 = 4 + 0 x 2 1 = 1 x 2 = 0 + 1 x 2 0 = 1 x 1 = 1 So, 11101 in base 2 is 16 + 8 + 4+ 0 + 1 = 29 in base 10 4/5/18 Matni, CS64, Sp18 6

Convenient Table HEXADECIMAL BINARY 0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 HEXADECIMAL (Decimal) BINARY A (10) 1010 B (11) 1011 C (12) 1100 D (13) 1101 E (14) 1110 F (15) 1111 4/5/18 Matni, CS64, Sp18 7

Always Helpful to Know N 2 N 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024 = 1 kilobits N 2 N 11 2048 = 2 kb 12 4 kb 13 8 kb 14 16 kb 15 32 kb 16 64 kb 17 128 kb 18 256 kb 19 512 kb 20 1024 kb = 1 megabits N 2 N 21 2 Mb 22 4 Mb 23 8 Mb 24 16 Mb 25 32 Mb 26 64 Mb 27 128 Mb 28 256 Mb 29 512 Mb 30 1 Gb 4/5/18 Matni, CS64, Sp18 8

Converting Binary to Octal and Hexadecimal (or any base that s a power of 2) NOTE THE FOLLOWING: Binary is 1 bit Octal is 3 bits Hexadecimal is 4 bits Use the group the bits technique Always start from the least significant digit Group every 3 bits together for bin à oct Group every 4 bits together for bin à hex 4/5/18 Matni, CS64, Sp18 9

Converting Binary to Octal and Hexadecimal Take the example: 10100110 to octal: 1 0 1 0 0 1 1 0 2 4 6 246 in octal to hexadecimal: 1 0 1 0 0 1 1 0 10 6 A6 in hexadecimal 4/5/18 Matni, CS64, Sp18 10

Converting Decimal to Other Bases Algorithm for converting number in base 10 to other bases While (the quotient is not zero) 1. Divide the decimal number by the new base 2. Make the remainder the next digit to the left in the answer 3. Replace the original decimal number with the quotient 4. Repeat until your quotient is zero Example: What is 98 (base 10) in base 8? 98 / 8 = 12 R 2 12 / 8 = 1 R 4 1 / 8 = 0 R 1 1 4 2 4/5/18 Matni, CS64, Sp18 11

In-Class Exercise: Converting Decimal into Binary & Hex Convert 54 (base 10) into binary and hex: 54 / 2 = 27 R 0 27 / 2 = 13 R 1 13 / 2 = 6 R 1 6 / 2 = 3 R 0 3 / 2 = 1 R 1 1 / 2 = 0 R 1 54 (decimal) = 110110 (binary) = 36 (hex) Sanity check: 110110 = 2 + 4 + 16 + 32 = 54 4/5/18 Matni, CS64, Sp18 12

Binary Logic Refresher NOT, AND, OR X NOT X X 0 1 1 0 X Y X AND Y X && Y X.Y 0 0 0 0 1 0 1 0 0 1 1 1 X Y X OR Y X Y X + Y 0 0 0 0 1 1 1 0 1 1 1 1 4/5/18 Matni, CS64, Sp18 13

Binary Logic Refresher Exclusive-OR (XOR) The output is 1 only if the inputs are opposite X Y X XOR Y X O + Y 0 0 0 0 1 1 1 0 1 1 1 0 4/5/18 Matni, CS64, Sp18 14

Bitwise NOT Similar to logical NOT (!), except it works on a bit-by-bit manner In C/C++, it s denoted by a tilde: ~ ~(1001) = 0110 4/5/18 Matni, CS64, Sp18 15

Exercises Sometimes hexadecimal numbers are written in the 0xhh notation, so for example: The hex 3B would be written as 0x3B What is ~(0x04)? Ans: 0xFB What is ~(0xE7)? Ans: 0x18 4/5/18 Matni, CS64, Sp18 16

Bitwise AND Similar to logical AND (&&), except it works on a bit-by-bit manner In C/C++, it s denoted by a single ampersand: & (1001 & 0101) = 1 0 0 1 & 0 1 0 1 = 0 0 0 1 4/5/18 Matni, CS64, Sp18 17

Exercises What is (0xFF) & (0x56)? Ans: 0x56 What is (0x0F) & (0x56)? Ans: 0x06 What is (0x11) & (0x56)? Ans: 0x10 Note how & can be used as a masking function 4/5/18 Matni, CS64, Sp18 18

Bitwise OR Similar to logical OR ( ), except it works on a bitby-bit manner In C/C++, it s denoted by a single pipe: (1001 0101) = 1 0 0 1 0 1 0 1 = 1 1 0 1 4/5/18 Matni, CS64, Sp18 19

Exercises What is (0xFF) (0x92)? Ans: 0xFF What is (0xAA) (0x55)? Ans: 0xFF What is (0xA5) (0x92)? Ans: B7 4/5/18 Matni, CS64, Sp18 20

Bitwise XOR Works on a bit-by-bit manner In C/C++, it s denoted by a single carat: ^ (1001 ^ 0101) = 1 0 0 1 ^ 0 1 0 1 = 1 1 0 0 4/5/18 Matni, CS64, Sp18 21

Exercises What is (0xA1) ^ (0x13)? Ans: 0xB2 What is (0xFF) ^ (0x13)? Ans: 0xEC Note how (1^b) is always ~b and how (0^b) is always b 4/5/18 Matni, CS64, Sp18 22

Bit Shift Left Move all the bits N positions to the left What do you do the positions now empty? You put in N 0s Example: Shift 1001 2 positions to the left 1001 << 2 = 100100 Why is this useful as a form of multiplication? 4/5/18 Matni, CS64, Sp18 23

Multiplication by Bit Left Shifting Veeeery useful in CPU (ALU) design Why? Because you don t have to design a multiplier You just have to design a way for the bits to shift 4/5/18 Matni, CS64, Sp18 24

Bit Shift Right Move all the bits N positions to the right, subbing-in either N 0s or N 1s on the left Takes on two different forms Example: Shift 1001 2 positions to the right 1001 >> 2 = either 0010 or 1110 The information carried in the last 2 bits is lost. If Shift Left does multiplication, what does Shift Right do? It divides, but it truncates the result 4/5/18 Matni, CS64, Sp18 25

Two Forms of Shift Right Subbing-in 0s makes sense What about subbing-in the leftmost bit with 1? It s called arithmetic shift right: 1100 (arithmetic) >> 1 = 1110 It s used for twos-complement purposes What? 4/5/18 Matni, CS64, Sp18 26

Negative Numbers in Binary So we know that, for example, 6 (10) = 110 (2) But what about 6 (10)??? What if we added one more bit on the far left to denote negative? i.e. becomes the new MSB So: 110 (+6) becomes 1110 ( 6) But this leaves a lot to be desired Bad design choice 4/5/18 Matni, CS64, Sp18 27

Twos Complement Method This is how Twos Complement fixes this. Let s write out -6 (10) in 2s-Complement binary in 4 bits: First take the unsigned (abs) value (i.e. 6) and convert to binary: Then negate it (i.e. do a NOT function on it): Now add 1: 0110 1001 1010 So, 6 (10) = 1010 (2) 4/5/18 Matni, CS64, Sp18 28

Let s do it Backwards By doing it THE SAME EXACT WAY! 2s-Complement to Decimal method is the same! Take 1010 from our previous example Negate it and it becomes 0101 Now add 1 to it & it becomes 0110, which is 6 (10) 4/5/18 Matni, CS64, Sp18 29

Another View of 2s Complement NOTE: In Two s Complement, if the number s MSB is 1, then that means it s a negative number and if it s 0 then the number is positive. 4/5/18 Matni, CS64, Sp18 30

Another View of 2s Complement NOTE: Opposite numbers show up as symmetrically opposite each other in the circle. NOTE AGAIN: When we talk of 2s complement, we must also mention the number of bits involved 4/5/18 Matni, CS64, Sp18 31

Ranges The range represented by number of bits differs between positive and negative binary numbers Given N bits, the range represented is: 0 to +2 N 1 for positive numbers and 2 N-1 to +2 N-1 1 for 2 s Complement negative numbers 4/5/18 Matni, CS64, Sp18 32

Addition We have an elementary notion of adding single digits, along with an idea of carrying digits Example: when adding 3 to 9, we put forward 2 and carry the 1 (i.e. to mean 12) We can build on this notion to add numbers together that are more than one digit long 1 1 Example: 1 2 3 + 3 8 9 5 1 2 4/5/18 Matni, CS64, Sp18 33

Addition in Binary Same mathematical principal applies 1 1 1 1 0 0 1 1 + 1 1 0 1 3 + 13 1 0 0 0 0 16 4/5/18 Matni, CS64, Sp18 34

Exercises Implementing an 8-bit adder: What is (0x52) + (0x4B)? Ans: 0x9D, output carry bit = 0 What is (0xCA) + (0x67)? Ans: 0x31, output carry bit = 1 4/5/18 Matni, CS64, Sp18 35

YOUR TO-DOs Assignment #1 Look for it over the weekend on the class website LAB #1 is on MONDAY! Next week, we will discuss more Arithmetic topics and start exploring Assembly Language Do your readings! (again: found on the class website) 4/5/18 Matni, CS64, Sp18 36

4/5/18 Matni, CS64, Sp18 37