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

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

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

CS 64 Week 0 Lecture 1. Kyle Dewey

Beginning C Programming for Engineers

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

COMP 122/L Lecture 2. Kyle Dewey

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

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

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 & IT Conversions. Magnitude 10,000 1,

Signed Binary Numbers

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

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

Final Labs and Tutors

Integer Representation

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

T02 Tutorial Slides for Week 2

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

Welcome to Computer Organization and Design Logic

Octal and Hexadecimal Integers

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

EE292: Fundamentals of ECE

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

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

Lecture 13 Bit Operations

Module 1: Information Representation I -- Number Systems

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

Number System. Introduction. Decimal Numbers

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

Arithmetic Operations

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

Harry H. Porter, 2006

COMP Overview of Tutorial #2

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

CS 107 Lecture 2: Bits and Bytes (continued)

Module 2: Computer Arithmetic

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

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

Chapter 1 Review of Number Systems

What Is It? Instruction Register Address Register Data Register

CS 64 Week 1 Lecture 1. Kyle Dewey

COMP2611: Computer Organization. Data Representation

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

Chapter 2: Number Systems

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

Number Systems and Their Representations

Number Systems CHAPTER Positional Number Systems

Chapter 2 Number System

Number representations

Computer Architecture and Organization: L04: Micro-operations

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

Introduction to Computers and Programming. Numeric Values

Survey. Motivation 29.5 / 40 class is required

Representation of Information

Intermediate Programming & Design (C++) Notation

Learning the Binary System

Unit 2: Data Storage CS 101, Fall 2018

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

Bitwise Instructions

CS33 Project Gear Up. Data

Fundamentals of Programming Session 2

Octal & Hexadecimal Number Systems. Digital Electronics

CMPE223/CMSE222 Digital Logic Design. Positional representation

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

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

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Arithmetic and Bitwise Operations on Binary Data

Level ISA3: Information Representation

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

CPE300: Digital System Architecture and Design

Topic Notes: Bits and Bytes and Numbers

MC1601 Computer Organization

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

CS 101: Computer Programming and Utilization

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

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

Number Representations

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

Binary Values. CSE 410 Lecture 02

Arithmetic for Computers

Microcomputers. Outline. Number Systems and Digital Logic Review

Applied Computer Programming

Topic Notes: Bits and Bytes and Numbers

Bits, Words, and Integers

Electronic Data and Instructions

Bits, Bytes and Integers

The Design of C: A Rational Reconstruction

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

EE 109 Unit 6 Binary Arithmetic

Lecture 2: Number Systems

Expressions. Operands. Operators

CHAPTER 1 Numerical Representation

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

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Numbers and Representations

MACHINE LEVEL REPRESENTATION OF DATA

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

Transcription:

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

Administrative Stuff The class is full I will not be adding more ppl L Did you check out the syllabus? Did you check out the class website? Did you check out Piazza (and get access to it)? Did you go to lab yesterday? Do you understand how you will be submitting your assignments? 10/3/18 Matni, CS64, Fa18 2

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

What s in a Number? 642 What is that??? Well, what NUMERICAL BASE are you expressing it in? 10/3/18 Matni, CS64, Fa18 4

Positional Notation of Decimal Numbers 642 in base 10 (decimal) can be described in positional notation as: 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 10/3/18 Matni, CS64, Fa18 5

Numerical Bases and Their Symbols How many symbols or digits do we use in Decimal (Base 10)? Base 2 (Binary)? Base 16 (Hexadecimal)? Base N? 10/3/18 Matni, CS64, Fa18 6

Positional Notation This is how you convert any base number into decimal! Each digit gets multiplied by B N Where: B = the base N = the position of the digit Example: given the number 613 in base 7: Number in decimal = 6 x 7 2 + 1 x 7 1 + 3 x 7 0 = 304 10/3/18 Matni, CS64, Fa18 7

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 10/3/18 Matni, CS64, Fa18 8

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 10/3/18 Matni, CS64, Fa18 9

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 10/3/18 Matni, CS64, Fa18 10

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 10/3/18 Matni, CS64, Fa18 11

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 10/3/18 Matni, CS64, Fa18 12

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 10/3/18 Matni, CS64, Fa18 13

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 10/3/18 Matni, CS64, Fa18 14

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 10/3/18 Matni, CS64, Fa18 15

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 10/3/18 Matni, CS64, Fa18 16

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 10/3/18 Matni, CS64, Fa18 17

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 10/3/18 Matni, CS64, Fa18 18

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 10/3/18 Matni, CS64, Fa18 19

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 10/3/18 Matni, CS64, Fa18 20

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 10/3/18 Matni, CS64, Fa18 21

Exercises What is (0xFF) (0x92)? Ans: 0xFF What is (0xAA) (0x55)? Ans: 0xFF What is (0xA5) (0x92)? Ans: B7 10/3/18 Matni, CS64, Fa18 22

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 10/3/18 Matni, CS64, Fa18 23

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 10/3/18 Matni, CS64, Fa18 24

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

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 (which is relatively easier) 10/3/18 Matni, CS64, Fa18 26

Bit Shift Right Move all the bits N positions to the right, subbing-in either N number of 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 10/3/18 Matni, CS64, Fa18 27

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? 10/3/18 Matni, CS64, Fa18 28

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 10/3/18 Matni, CS64, Fa18 29

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) according to this rule 10/3/18 Matni, CS64, Fa18 30

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) 10/3/18 Matni, CS64, Fa18 31

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. 10/3/18 Matni, CS64, Fa18 32

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 10/3/18 Matni, CS64, Fa18 33

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 10/3/18 Matni, CS64, Fa18 34

YOUR TO-DOs Assignment #1 Due on Friday!!! Next week, we will discuss a few more Arithmetic topics and start exploring Assembly Language! Do your readings! (again: found on the class website) 10/3/18 Matni, CS64, Fa18 35

10/3/18 Matni, CS64, Fa18 36