Bit Manipulation.Pptx
Total Page:16
File Type:pdf, Size:1020Kb
9/27/17 What can we do at the bit level? Bit Manipula+on • (bit level) Boolean operations (NOT, CS 203 OR, AND, XOR) Fall 2017 • Shift left • Shift right 1 2 What sorts of things can we do with bit manipula+on? Boolean Algebra: TruthTables • Multiply/divide False True 0 1 • NOT (~) • Mask True False 1 0 • Swap • 0 1 AND (&) 0 0 0 1 0 1 Where: 0 = False; 1 = True 3 4 Boolean algebra and binary manipulation: Boolean Algebra: Truth Tables Bitwise application of Boolean algebra X = [xn-1 xn-2 … x2 x1 x0] 0 1 • OR (|) Y = [y y … y y y ] 0 0 1 n-1 n-2 2 1 0 ---------------------------------------------------------- 1 1 1 ~X = [~x ~x … ~x ~x ~x ] 0 1 n-1 n-2 2 1 0 • XOR (^) 0 0 1 X&Y = [(xn-1&yn-1) (xn-2&yn-2) … (x1&y1) (x0&y0)] 1 1 0 X|Y = [(xn-1|yn-1) (xn-2|yn-2) … (x1|y1) (x0|y0)] X^Y = [(xn-1^yn-1) (xn-2^yn-2) … (x1^y1) (x0^y0)] Where: 0 = False; 1 = True 5 6 1 9/27/17 Boolean algebra and binary manipulation: Boolean algebra and binary manipulation: Bitwise application of Boolean algebra Bitwise application of Boolean algebra • Given X = [00101111]; Y = [10111000] • Given X = [00101111];Y = [10111000] • ~X = ? • X&Y = ? • ~X = [11010000] X&Y = [00101000] • X|Y = ? • X^Y = ? • X|Y =[10111111] X^Y = [10010111] 7 8 Subtracting by adding: 2’s Using 2’s complement to complement subtract by adding • X – Y = X + (-Y) make use of this fact!!! • 5 – 3 • 1’s complement --- flip the bits [00000101] – [00000011] • 2’s complement of a number is the 1’s complement + 1 = [00000101] + 2’s complement ([00000011]) = [00000101] + ([11111100] + [00000001]) [00000000] = [00000101] + [11111101] 1’s complement [11111111] = [00000010] = 2 2’s complement [00000000] 10 When using 2’s complement, do not have the 2- zero issue 9 10 What can we do?: What can we do?: XOR as a programmable inverter Masking • Given a bit a; how you set it determines whether • A series of bits (word, half-word, etc.) can act as a another bit b is inverted (flipped) or left mask unchanged. • Given a mask M, M&x = x’ such that the bits of x’ are 1 iff the same place bit of M and of x are • EXAMPLES: both 1 • a = 0, then a^b = b (a leaves b unchanged) • EXAMPLE • a = 1, then a^b = ~b (a as inverter) M = [11000100] x = [01001010] • Try it!!!! x’ = [01000000] • Used for bits; used for signals 11 12 2 9/27/17 Valuable use of masking Valuable use of masking • LEGv8: Instruction format for I (indirect) • LEGv8: Instruction format for I (indirect) ALU opcode ALU immediate Rn Rd opcode Rn Rd immediate 31 22 21 10 9 5 4 0 31 22 21 10 9 5 4 0 What is the result of applying a mask of: What is the result of applying a mask of: • 0000001F ? • 0000001F gives Rd • 000003E0 ? • 000003E0 gives Rn • 003FFC00 ? • 003FFC00 gives ALU immediate • FFC00000 ? 13 • FFC00000 gives opcode 14 Another use of masking Shifting bits • Syntax • X << k shift x left k times • Sub-net masking X = [00101101] • “pick off” parts of an IP address X << 4 = [11010000] • X >> k shift x right k times X >> 4 = [00000010] 15 16 Shift right – logical or arithmetic? Logical or Arithmetic Shift? • X = [00101101] Y = [11001010] • NO STANDARD • Logical shift (zero fill): • Most C compilers use arithmetic X >> 4 = [00000010] right shifts for signed data • Y >> 4 = [00001100] ALL use logical right shifts for unsigned data • Arithmetic shift (msb fill): X >> 4 = [00000010] QUESTION: Y >> 4 = [11111100] Can you posit a guess as to why? 17 18 3 9/27/17 Shifting … what we can do Use for shifting Example: 2 0 X = 510 = [00000101] = 2 + 2 Example continued: 2 0 3 8 * 5: X = 510 = [00000101] = 2 + 2 ; 8 = 2 3 5 3 8 * X = 2 * 5 = 4010 = [00101000] = 2 + 2 3 8*X = 2 *5 = 4010 = [00101000] = [00101000] = 3210 +810 = X << 3 What did you notice? 19 20 Multiplication (and division) Multiplication by any number • 2k * X = X << k • Shift and add • Multiply by a power of 2 by shifting left the • How it was done before the days of hardware exponent of 2 number of times multiplication • Example: • Divide by a power of 2 by shifting right 1310*510 = 6510 • But need to handle remainder (unless int) 13 * 5 = (8*5) + (4*5) + (1*5) • Need to know if signed or unsigned (and whether = (23 * 5) + (22 * 5) + (20 * 5) logical or arithmetic shift is used for signed) = (5 << 3) + (5 << 2) + 5 21 22 Shift and add Shifting example – c and LEGv8 Claim: (5 << 3) + (5 << 2) + 5 = 13 * 5 • Problem: multiply the number 5 by 4 In C 5 = 00000101 int x = 5; x = x<<2; 5 << 3 = 00101000 fprintf(stderr, “ %i\n”,x); 5 << 2 = 00101000 In assembler 01010000 = 26+24 = 32+16 = 40 10 LDUR X1,#5 LSL X1,X1,#2 23 24 4 9/27/17 Swapping Swapping • In 3 steps --- Proof • In 3 steps • Recall that for any z, z^z = 0 Given X = a, Y = b 1. Y = X^X 1. Y = a ^ b 2. X = X^Y 2. X = a ^ (a ^ b) = (a ^ a) ^ b = 0 ^ b = b 3. Y = X^Y 3. Y = b ^ (a ^ b) = a ^ (b ^ b) = a ^ 0 = a QED nb: no temp variable is needed 25 26 Underflow: What to do? Overflow: What to do? • A shift right requires assigning values to • A “carry” occurs from some arithmetic operation the bits to the left of the shift • If it is an operation on a register, a flag in the ALU can be set to indicate overflow • Use the arithmetic or logical shift paradigm • If it is an operation on 2-registers; the discussed earlier flag can be used as input to the next word being processed. n.b. assembly language programmers MUST pay attention to n.b. assembly language programmers MUST pay attention to this. this. 27 28 So What? Coming to a CS203 class near you: Circuits!! 29 5 .