Boolean Algebra, Bitwise Operations

Slides:



Advertisements
Similar presentations
How to Convert Decimal Numbers to Binary EXAMPLES.
Advertisements

Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Binary Operations Math/Logical. Binary Math Decimal Addition Example ) Add = 15 Write down 5, carry ) Add 3 +
Solve an equation with variables on both sides
Binary Arithmetic Math For Computers.
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
1 Lecture 2: Number Systems Binary numbers Base conversion Arithmetic Number systems  Sign and magnitude  Ones-complement  Twos-complement Binary-coded.
Data Representation Number Systems.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Add, Subtract, Multiply, and Divide Decimals
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Conversion Between Lengths Positive number pack with leading zeros +18 = = Negative numbers pack with leading ones -18 =
Chapter 7 Arithmetic Operations and Circuits Binary Arithmetic Addition –When the sum exceeds 1, carry a 1 over to the next-more-significant column.
Number Systems Part 2 Numerical Overflow Right and Left Shifts Storage Methods Subtraction Ranges.
Binary Numbers.
XOR and XNOR Logic Gates. XOR Function Output Y is TRUE if input A OR input B are TRUE Exclusively, else it is FALSE. Logic Symbol  Description  Truth.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Chapter 2 Binary Values and Number Systems. 2 2 Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645,
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
1 Arithmetic and Logic Operations Patt and Patel Ch. 2 & 3.
Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Addition and Substraction
1 4. Computer Maths and Logic 4.1 Number Systems.
Introduction To Number Systems Binary System M. AL-Towaileb1.
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Logic Design Dr. Oliver Faust.
In decimal we are quite familiar with placing a “-” sign in front of a number to denote that it is negative The same is true for binary numbers a computer.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
COSC2410: LAB 2 BINARY ARITHMETIC SIGNED NUMBERS FLOATING POINT REPRESENTATION BOOLEAN ALGEBRA 1.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Chapter 9 Computer Arithmetic
William Stallings Computer Organization and Architecture 8th Edition
David Kauchak CS 52 – Spring 2017
Compsci 210 Tutorial Five.
Digital Logic & Design Adil Waheed Lecture 02.
Chapter 4 Operations on Bits.
Negative Binary Numbers
Lesson Objectives Aims
Integer Real Numbers Character Boolean Memory Address CPU Data Types
Negative Binary Numbers
Number Systems.
William Stallings Computer Organization and Architecture 7th Edition
Addition and Substraction
Topic 3: Data Binary Arithmetic.
Teaching Computing to GCSE
Teaching Computing to GCSE
ECEG-3202 Computer Architecture and Organization
Bitwise Operators CS163 Fall 2018.
Numbers and Arithmetic and Logical Operation
Binary “There are 10 types of people in the world: Those who understand binary, and those who don't.”
Homework Homework Continue Reading K&R Chapter 2 Questions?
Chapter 8 Computer Arithmetic
Number Systems Rayat Shikshan Sanstha’s
Binary to Decimal Conversion
Numbers and Arithmetic and Logical Operation
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
COE 202: Digital Logic Design Number Systems Part 2
Two’s Complement & Binary Arithmetic
Presentation transcript:

Boolean Algebra, Bitwise Operations Tutorial Two Boolean Algebra, Bitwise Operations CompSci 210 - Semester Two 2017

Boolean Values Two types: Generally given the familiar values 1 and 0 True False Generally given the familiar values 1 and 0 CompSci 210 - Semester Two 2017

Boolean Operators AND OR NOT 𝐴 𝑇 Symbol: ∙ Conjunction Symbol: + T ∙ T = T, T ∙ F = F, 1 ∙ 1 = 1 OR Symbol: + Disjunction T + T = T, T + F = T, 0 + 0 = 0 NOT Symbol: line over the operator Negation = F, = 1 𝐴 𝑇 CompSci 210 - Semester Two 2017

Truth Tables AND A B A ∙ B 1 CompSci 210 - Semester Two 2017

Truth Tables OR A B A + B 1 CompSci 210 - Semester Two 2017

Truth Tables NOT A 1 𝐴 CompSci 210 - Semester Two 2017

Bitwise Operations Work on bit patterns, individual bits Manipulate values for comparisons/Calculations Bit Masking Very Fast (Directly supported by CPU) CompSci 210 - Semester Two 2017

Bitwise Operators XOR AND OR NOT Symbol: ^ Exclusive OR Symbol: & CompSci 210 - Semester Two 2017

Bitwise operations XOR 1000 ^ 1011 = 0011 0111 ^ 1010 = 1101 CompSci 210 - Semester Two 2017

Bitwise operations AND 1000 & 1011 = 1000 0111 & 1000 = 0000 CompSci 210 - Semester Two 2017

Bitwise operations OR 1000 | 1011 = 1011 0111 | 1000 = 1111 CompSci 210 - Semester Two 2017

Bitwise operations NOT ~1010 = 0101 ~0111 = 1000 CompSci 210 - Semester Two 2017

ADDITION Take the two binary numbers 01 (1) and 10 (2) To add these two numbers, first align them vertically Then, as with adding two decimal numbers, start from the right, and go left 01 + 10 11 Note: The above is not in two’s compliment. CompSci 210 - Semester Two 2017

Addition Cont. What happens if we have two 1’s in the same column? Like in decimal addition, you place the remainder of the addition in that column, and then the carry into the column one to its left 11 01 (1)00 What’s with the brackets? CompSci 210 - Semester Two 2017

Subtraction Subtraction is a subset of addition, in that adding a numbers negative is the same as subtracting that number 10 – 5 = 5 10 + (-5) = 5 The same is true for binary arithmetic. This is something important to remember for later when working with the LC-3, as it doesn’t have a subtraction instruction. CompSci 210 - Semester Two 2017

Exercise 1 Calculate the result of the following 2’s Compliment arithmetic 10+1111 0111 + 111001 1111 −0001 0000 −1010 CompSci 210 - Semester Two 2017

Solution 1 B “0111 + 111001” left pad any numbers so both have the same number of bits 0111 = 000111 Now align them vertically, and add the 000111 + 111001 000000 (carry 1) 000000 (Carry 1) … (1) 000000 CompSci 210 - Semester Two 2017

Exercise 1 Solutions 10 + 1111 (-2 + -1) 0111 + 111001 (+7 + -7) 10 + 1111 (-2 + -1) 0111 + 111001 (+7 + -7) 1111 −0001 (-1 – 1) 0000 −1010 (0 −−6) Now we’ll solve them all using the processes mentioned before (We won’t be aligning them for simplicities sake) CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110 + 1111 000111 + 111001 1111 −0001 0000 −1010 Step 1: Left pad any numbers that need to be CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110 + 1111 000111 + 111001 1111+1111 0000+ 0110 Step 2: Convert subtractions to additions CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110+1111 Carry: 0, Remainder: 1 Step 3: Add from right to left CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110+1111 Carry: 10, Remainder: 01 CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110+1111 Carry: 110, Remainder: 101 CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110+1111 Carry: 1110, Remainder: 1101 CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110+1111 Carry: 1110, Remainder: (1)1101 CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110+1111 Carry: 1110, Remainder: (1)1101 CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110+1111 Carry: 1110, Remainder: (1)1101 CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110+1111 = 1101 000111 + 111001 = 000000 1110+1111 = 1101 000111 + 111001 = 000000 1111+1111 = 1110 0000+ 0110 = 0110 Step 4: Truncate carry bits CompSci 210 - Semester Two 2017

Exercise 1 Solutions 1110+1111 = 1101 000111 + 111001 = 000000 1110+1111 = 1101 000111 + 111001 = 000000 1111−0001 = 1110 0000−1010 = 0110 Step 5: Revert back to original equations CompSci 210 - Semester Two 2017

Exercise 1 Solutions −2+−1 = -3 +7+−7 = 0 −1−1 = -2 0−−6 = 6 −2+−1 = -3 +7+−7 = 0 −1−1 = -2 0−−6 = 6 Step 6: Convert to decimal to check answer CompSci 210 - Semester Two 2017

Carry and Overflow Carry Flag: overflow flag: Set in the following: 1111 + 0001 = (1)0000 (MSB carried out) 0000 – 0001 = 0111 (MSB carried in) overflow flag: 0100 + 0100 = 1000 (4 + 4 = -8) 1000 + 1000 = 0000 (-8 + -8 = 0) In unsigned arithmetic we care about the carry flag In signed arithmetic we care about the overflow flag CompSci 210 - Semester One 2017

Bit Shifting Left Shift (<<) Right Shift (>>) Equivalent to multiplying the value by 2 𝑛 00010111 (+23) << 1 (n) = 00101110 (+46) Right Shift (>>) Equivalent to dividing, and rounding down, the value by 2 𝑛 10010111 (-105) >> 1 (n) = 11001011 (-53) This can change depending on if the value being shifted is signed or unsigned CompSci 210 - Semester Two 2017