Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2018 Lecture 36: Bitwise operators (continued)

2 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

3 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, = A-F 0x0 = 00002, 0x1 = … 0x9 = 10012 0xA = 10102, 0xB = … 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

4 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

5 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

6 Bitwise Logical Operations
& = ? A B A&B 1 9/24/2019 ECE Application Programming: Lecture 36

7 Bitwise Logical Operations
& = ? A B A&B 1 9/24/2019 ECE Application Programming: Lecture 36

8 Bitwise Logical Operations
& = ? A B A&B 1 9/24/2019 ECE Application Programming: Lecture 36

9 Bitwise Logical Operations
| = ? A B A | B 1 9/24/2019 ECE Application Programming: Lecture 36

10 Bitwise Logical Operations
ECE 160 02/02/2005 Bitwise Logical Operations | = ? A B A | B 1 9/24/2019 ECE Application Programming: Lecture 36 (c) 2005, P. H. Viall

11 Bitwise Logical Operations
| = ? A B A | B 1 9/24/2019 ECE Application Programming: Lecture 36

12 Bitwise Logical Operations
^ = ? A B A ^ B 1 9/24/2019 ECE Application Programming: Lecture 36

13 Bitwise Logical Operations
^ = ? A B A ^ B 1 9/24/2019 ECE Application Programming: Lecture 36

14 Bitwise Logical Operations
^ = ? A B A ^ B 1 9/24/2019 ECE Application Programming: Lecture 36

15 Bitwise Logical Operations
ABCD | FF00 & FFCD 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

16 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 = ( ) << 5 = x >> n shifts x right by n bits Equivalent to x / 2n e.g. 8 >> 3 = ( ) >> 3 = 9/24/2019 ECE Application Programming: Lecture 36

17 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

18 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

19 ECE Application Programming: Lecture 36
Example: Solution First step: convert A & B to binary (or hex) A = 7 = = 0x7 B = 10 = = 0xA Now solve problems A & B = 0111 & 1010 = 00102 A | ~B = 0111 | ~1010 = 0111 | 0101 = Upper 28 bits = 1! Final answer: 0xFFFFFFF7 A ^ C = ( ) ^ ( ) = = 0xFFFFFFF8 A << 4 = 0111 << (4 bits) = = 0x70 B >> 5 = 1010 >> (5 bits) = 0 Only lowest 4 bits of B contain non-zero values! A | (B << 2) = | (1010 << 2 bits) = 0111 | = 9/24/2019 ECE Application Programming: Lecture 36

20 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

21 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


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google