CSC 2400: Computer Systems Bit- Level vs. Logical Opera<ons Bit- Level Operators Common to C and Java &,, ~, ^, <<, >> 1
Used in Networking Used in Encryption/ Decryption 2
Used in Compression Look at the DEFLATE algorithm, for instance - https://en.wikipedia.org/wiki/deflate - everything in bits, not bytes Overview of C bit-level operators ~ Bitwise NOT ( flips bits) & Bitwise AND ^ Bitwise XOR Bitwise OR << Bitwise left shift (shifts bits to left) >> Bitwise right shift (shifts bits to right) 3
Observation 1 Addition q 2 s comp. addition is just binary addition. - assume all integers have the same number of bits - for now, assume that sum fits in n-bit 2 s comp. representation 01101000 (104) 11110110 (-10) + 11110000 (-16) + (-9) 01011000 (88) (-19) Assuming 8-bit 2 s complement numbers. Observation 2 Sign Extension q To add two numbers, we must represent them with the same number of bits. q If we just pad with zeroes on the left: 4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 00001100 (12, not -4; NOT GOOD!) q Instead, replicate the MS bit -- the sign bit: 4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 11111100 (still -4) 4
Bit-Level Operations in C q q Operations &,, ~, ^ available in C - Apply to any integral data type o long, int, short, char - View arguments as bit vectors - Arguments applied bit-wise Examples (char data type) - ~0x41 --> 0xBE ~01000001 2 --> 10111110 2 - ~0x00 --> 0xFF ~00000000 2 --> 11111111 2-0x69 & 0x55 --> 0x41 01101001 2 & 01010101 2 --> 01000001 2-0x69 0x55 --> 0x7D 01101001 2 01010101 2 --> 01111101 2 Bitwise Operator: NOT (~) q NOT (~) is the same as one s complement - Turns 0 to 1, and 1 to 0 q Examples: - Assume that x is an integer on 8 bits. Set x = 1. What is the value of ~x? 0 1 ~x - Set the least significant bit of y to 0: y = y & ; 5
Bitwise Operators: AND (&) and OR (I) Bitwise AND (&) Bitwise OR ( ) & 0 1 0 1 0 1 0 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 & 15 0 0 0 0 1 1 1 1 15 0 0 0 0 1 1 1 1 Bitwise Operators: AND (&) and OR (I) Assume that x is an integer variable. q Determine the least significant bit of x: int bit = x & ; q Set the least significant bit of x to 1: x = x ; 6
Bitwise Operator: XOR q XOR (^) - 0 if both bits are the same - 1 if the two bits are different ^ 0 1 0 1 q For an integer x, what is the value of x^x Relations Between Operations q DeMorgan s Laws - Express & in terms of, and vice-versa o A & B = ~(~A ~B) A and B are true if and only if neither A nor B is false o A B = ~(~A & ~B) A or B are true if and only if A and B are not both false q Exclusive-Or using Inclusive Or o A ^ B = (~A & B) (A & ~B) Exactly one of A and B is true o A ^ B = (A B) & ~(A & B) Either A is true, or B is true, but not both 7
Bitwise Operator: LeQ ShiQ q LeV shiv: x << y (same as mulxply by 2 y ) - ShiV bit- vector x lev y posixon - Fill blanks with 0 53 53<<2 0 0 1 1 0 1 0 1 Bitwise Operator: Right ShiQ q Right shiv: x >> y (same as divide by 2 y ) - ShiV bit- vector x right y posixons - Fill in blanks with sign bit (this is called arithme.c shi/) 53 0 0 1 1 0 1 0 1-75 1 0 1 1 0 1 0 1 53>>2-75>>2 sign extension sign extension Note: Java also has the unsigned shift operator >>> Not available in C. 8
Bitwise Operators q Print all the bits of a character from right to lev (starxng with the least significant bit): char c = 0xB5; 0 0 1 1 0 1 0 1 void reverse_decimal_to_binary(char c) { /* add code here */ } Bitwise Operators q Print all the bits of a character from lev to right (starxng with the most significant bit): char c = 0xB5; 1 0 1 1 0 1 0 1 void decimal_to_binary(char c) { /* add code here */ } 9
Bitmasks q Used to change or query one or more bits in a variable. q The bitmask indicates which bits are to be affected. q Common operaxons: - Set one or more bits (set to 1) - Clear one or more bits (set to zero) - Read one or more bits q Examples: - Set bit 2 of x (bit 0 is least significant): x = x - Clear bit 3 of x: x = x - Read bit 4 of x: bit = x Bitmasks q Set the least significant byte of x to FF: x = x ; q Clear the least significant byte of x: x = x ; q Read the least significant byte of x: byte = x ; 10
Bitmasks q Set to 1 bits 2, 4 and 7 of x (0 is least significant): x = x ; q Clear bits 3, 4 and 5 of x: x = x ; Contrast: Logical Operators 11
Familiar Operators (common to C, Java) Category Operators Arithmetic ++expr --expr expr++ expr-- expr1*expr2 expr1/expr2 expr1%expr2 expr1+expr2 expr1-expr2 Assignment expr1=expr2 expr1*=expr2 expr1/=expr2 expr1%=expr2 expr1+=expr2 expr1-=expr2 Relational expr1<expr2 expr1<=expr2 expr1>expr2 expr1>=expr2 expr1==expr2 expr1!=expr2 Logical!expr expr1&&expr2 expr1 expr2 Function Call Cast Conditional func(paramlist) (type)expr expr1?expr2:expr3 Logical Operators && (Logical AND), (Logical OR),! (Logical NOT) q Always return 0 or 1 q View 0 as False q Anything nonzero as True q Examples (char data type) -!0x41 --> -!0x00 --> -!!0x41 --> - 0x69 && 0x55 --> - 0x69 0x55 --> 12
What is the Output? int a = 0x43, b = 0x21; printf("a b = %x\n", a b); Printf("a b = %x\n", a b); printf("a & b = %x\n", a & b); printf("a && b = %x\n", a && b); Mental Exercise /* * isnotequal - return 0 if x == y, and 1 otherwise * Examples: isnotequal(5,5) = 0, isnotequal(4,5) = 1 */ int isnotequal (int x, int y) { return ; } 13
What did we learn? q Bit- level operators (&,, ~, ^, <<, >>) - Operate on bits (view arguments as bit vectors) q Logical operators (&&,,!) - Always return 0 (False) or 1 (True) 14