EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2018 Lecture 36: Bitwise operators (continued)
ECE Application Programming: Lecture 36 Lecture outline Announcements/reminders Office hours Tuesday 12-1:30, not Thursday this week Today: Program 6 regrades due Th 12/13: last day of classes; Program 8 due P8 deals with file I/O (lectures 32-33) M 12/17: Exam 3, 3-6 PM, Ball 210 Course evals online; will also have hard copies available You’ll submit eval at exam W 12/19: All code due by 12:00 PM (noon) Program 9: Worth up to 4 points extra credit on final avg Resubmission deadline for P7 & P8 Today’s class Bitwise operators 9/24/2019 ECE Application Programming: Lecture 36
Review: binary and hexadecimal All data encoded in binary (base 2) Useful knowing bit patterns to evaluate bitwise ops Hexadecimal (base 16) used with bitwise ops Closer to binary than base 10 is Each hexadecimal digit = 4 bits Leading 0x indicates hexadecimal constant Digits 0-9 same as decimal, 10-15 = A-F 0x0 = 00002, 0x1 = 00012 … 0x9 = 10012 0xA = 10102, 0xB = 10112 … 0xF = 11112 Variables typically declared as unsigned int Strictly non-negative whole numbers 32 bits in length 9/24/2019 ECE Application Programming: Lecture 36
Review: Bitwise Logical Operations Deal with individual bits of a value Each bit is evaluated separately There is no "Carry" as with addition…i.e. the results of an operation in one bit position has no effect on an adjacent bit. Operators & AND | OR ^ XOR ~ bitwise NOT (flip all bits) 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations AND A B A&B 1 NOT A ~ A 1 OR XOR (exclusive or) A B A|B 1 A B A^B 1 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations 10111001 & 11110000 = ? 1 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0 --------------- 1 0 1 1 0 0 0 0 A B A&B 1 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations 10101010 & 11110000 = ? 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 --------------- A B A&B 1 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations 10101010 & 11110000 = ? 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 --------------- 1 0 1 0 0 0 0 0 A B A&B 1 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations 10111001 | 11110000 = ? 1 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0 --------------- 1 1 1 1 1 0 0 1 A B A | B 1 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations ECE 160 02/02/2005 Bitwise Logical Operations 10101010 | 11110000 = ? 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 --------------- A B A | B 1 9/24/2019 ECE Application Programming: Lecture 36 (c) 2005, P. H. Viall
Bitwise Logical Operations 10101010 | 11110000 = ? 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 --------------- 1 1 1 1 1 0 1 0 A B A | B 1 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations 10111001 ^ 11110000 = ? 1 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0 --------------- 0 1 0 0 1 0 0 1 A B A ^ B 1 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations 10101010 ^ 11110000 = ? 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 --------------- A B A ^ B 1 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations 10101010 ^ 11110000 = ? 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 --------------- 0 1 0 1 1 0 1 0 A B A ^ B 1 9/24/2019 ECE Application Programming: Lecture 36
Bitwise Logical Operations ABCD | FF00 & 5555 1111 1111 0000 0000 0101 0101 0101 0101 ------------------- 5500 0101 0101 0000 0000 0101 0101 0000 0000 1010 1011 1100 1101 ------------------- FFCD 1111 1111 1100 1101 NOTE: & is a higher precedence than | similar to * being a higher precedence than + in algebra. A B A&B A|B A^B 1 9/24/2019 ECE Application Programming: Lecture 36
ECE Application Programming: Lecture 36 Bit shifts Bit shift operators Left shift: << Right shift: >> Shifts in 0s (with unsigned ints) x << n shifts x left by n bits Equivalent to x * 2n e.g. 1 << 5 = (0000 ... 0001) << 5 = 0000 0010 0000 x >> n shifts x right by n bits Equivalent to x / 2n e.g. 8 >> 3 = (0000 ... 1000) >> 3 = 0000 ... 0001 9/24/2019 ECE Application Programming: Lecture 36
ECE Application Programming: Lecture 36 Review: C operators Operator Associativity Innermost ( ) Left to right Unary -, unary ~ Right to left * / % + - << >> NOTE: shift amt < 32 & ^ | 9/24/2019 ECE Application Programming: Lecture 36
Example: Bitwise operations Evaluate each of the following expressions if you have the following unsigned ints: A = 7, B = 10, and C = 0xFFFFFFFF A & B A | ~B A ^ C A << 4 B >> 5 A | (B << 2) 9/24/2019 ECE Application Programming: Lecture 36
ECE Application Programming: Lecture 36 Example: Solution First step: convert A & B to binary (or hex) A = 7 = 01112 = 0x7 B = 10 = 10102 = 0xA Now solve problems A & B = 0111 & 1010 = 00102 A | ~B = 0111 | ~1010 = 0111 | 0101 = 01112 Upper 28 bits = 1! Final answer: 0xFFFFFFF7 A ^ C = (0000 ... 0111) ^ (1111 ... 1111) = 1111 ... 10002 = 0xFFFFFFF8 A << 4 = 0111 << (4 bits) = 011100002 = 0x70 B >> 5 = 1010 >> (5 bits) = 0 Only lowest 4 bits of B contain non-zero values! A | (B << 2) = 0111 | (1010 << 2 bits) = 0111 | 101000 = 1011112 9/24/2019 ECE Application Programming: Lecture 36
ECE Application Programming: Lecture 36 Hexadecimal output To print a number in hex, use %x or %X %x prints characters a-f in lowercase %X prints characters A-F in uppercase To show leading 0x, use the # flag To show leading 0s, use precision with total # digits Field width + 0 flag also works unless value = 0 Examples (assume var1 = 0x1A2B) printf(“%x”, var1) 1a2b printf(“%X”, var1) 1A2B printf(“%#x”, var1) 0x1a2b printf(“%.6x”, var1) 001a2b printf(“%#.6x”, var1) 0x001a2b 9/24/2019 ECE Application Programming: Lecture 36
ECE Application Programming: Lecture 36 Next time Exam 3 Preview Reminders: Office hours Tuesday 12-1:30, not Thursday this week Today: Program 6 regrades due Th 12/13: last day of classes; Program 8 due P8 deals with file I/O (lectures 32-33) M 12/17: Exam 3, 3-6 PM, Ball 210 Course evals online; will also have hard copies available You’ll submit eval at exam W 12/19: All code due by 12:00 PM (noon) Program 9: Worth up to 4 points extra credit on final avg Resubmission deadline for P7 & P8 9/24/2019 ECE Application Programming: Lecture 36