EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Spring 2018 Lecture 31 Bitwise operators
ECE Application Programming: Lecture 31 Lecture outline Announcements/reminders Program 8 due 4/23 Today’s lecture Review: Character & line I/O Finish char/line I/O examples Bitwise operators 4/18/2019 ECE Application Programming: Lecture 31
ECE Application Programming: Lecture 31 Review: I/O Character input int fgetc(FILE *stream); int getchar(); int ungetc(int c, FILE *stream); Line input char *fgets(char *s, int n, FILE *stream); 4/18/2019 ECE Application Programming: Lecture 31
ECE Application Programming: Lecture 30 Review: Examples Show the output of the following short program Input: Test Input 1 23 4 5\n void main() { char c; char buffer[50]; int i; i = 0; while ((c = fgetc(stdin)) != '\n') { if (c != ' ') { buffer[i++] = c; } buffer[i] = '\0'; fputs(buffer, stdout); Output: TestInput12345 4/18/2019 ECE Application Programming: Lecture 30
Review: Examples (cont.) Input: Test1 Test 2 abcdefghijklmnopqrstuvwxyz This is a test of the fgets() function void main() { char str[25]; int i; for (i = 0; i < 5; i++) { fgets(str, 24, stdin); strcat(str, "\n"); fputs(str, stdout); } Output: Test1 Test 2 abcdefghijklmnopqrstuvw xyz This is a test of the f 4/18/2019 ECE Application Programming: Lecture 30
ECE Application Programming: Lecture 30 Examples (cont.) Output: n = 1024, n * 2 = 2048 buffer = Some other stuff Input: 1024Some other stuff void main() { char c; char buffer[50]; int n = 0; // isdigit in <ctype.h> while (isdigit(c = getchar())) { n = n * 10 + (c - 48); // Hint: '0' = 48 } // (ASCII value) ungetc(c, stdin); fgets(buffer, 50, stdin); printf("n = %d, n * 2 = %d\n", n, n * 2); printf("buffer = %s\n", buffer); } 4/18/2019 ECE Application Programming: Lecture 30
Binary and hexadecimal values Humans operate in decimal (base 10) Why don’t computers? Computers operate in binary (base 2) Each digit is a bit (binary digit) Can also use octal (base 8) or hexadecimal (base 16) Hexadecimal commonly used in programming Leading “0x” in C programming indicates hex value Base conversion Binary hex: start with LSB and make 4-bit groups e.g. 001 1011 01112 = 1B716 Note that an extra 0 is implied for the first group: 0010001 Binary decimal: multiply bit 0 by 20, bit 1 by 21, etc. e.g. 01112 = (0 x 23) + (1 x 22) + (1 x 21) + (1 x 20) = = 0 + 4 + 2 + 1 = 710 Decimal binary / hex: repeated integer division, where remainder gives you each digit, starting with LSB 4/18/2019 ECE Application Programming: Lecture 31
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) 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31 (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 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31
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 4/18/2019 ECE Application Programming: Lecture 31
ECE Application Programming: Lecture 31 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 4/18/2019 ECE Application Programming: Lecture 31
ECE Application Programming: Lecture 31 Review: C operators Operator Associativity Innermost ( ) Left to right Unary -, unary ~ Right to left * / % + - << >> NOTE: shift amt < 32 & ^ | 4/18/2019 ECE Application Programming: Lecture 31
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) 4/18/2019 ECE Application Programming: Lecture 31
ECE Application Programming: Lecture 31 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 4/18/2019 ECE Application Programming: Lecture 31
ECE Application Programming: Lecture 31 Final notes Next time Finish bitwise operators Reminders: Program 8 due 4/23 4/18/2019 ECE Application Programming: Lecture 31