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