Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructors: Dr. Lin Li & Dr. Michael Geiger Fall 2019 Lectures 35 & 36: Bitwise operators
2
ECE Application Programming: Lecture 35
Lecture outline Announcements/reminders M 12/9: Last day for P7 submissions (initial submission) Th 12/12: last day of classes; P8 & Ch. 9 exercises due P8 deals with structures (lectures 26-27, 29-30) M 12/16: Exam 3, 3-6 PM, Ball 210 Will post course evals online; you’ll submit eval at exam T 12/17: All code due by end of day Program 9: Worth up to 4 points extra credit on final avg Resubmission deadline for P6, P7, & P8 Today’s class Bitwise operators Common bitwise operations 3/14/2020 ECE Application Programming: Lecture 35
3
Binary and hexadecimal values
Humans operate in decimal (base 10) Computers operate in binary (base 2) Each digit is a bit (binary digit) Hexadecimal (base 16) commonly used in programming Leading “0x” in C programming indicates hex value Base conversion Binary decimal: multiply bit 0 by 20, bit 1 by 21, etc. e.g = (0 x 23) + (1 x 22) + (1 x 21) + (1 x 20) = = = 710 Binary hex: start with LSB and make 4-bit groups e.g = 1B716 Note that an extra 0 is implied for the first group: 0010001 3/14/2020 ECE Application Programming: Lecture 35
4
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) 3/14/2020 ECE Application Programming: Lecture 35
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 3/14/2020 ECE Application Programming: Lecture 35
6
Bitwise Logical Operations
& = ? A B A&B 1 3/14/2020 ECE Application Programming: Lecture 35
7
Bitwise Logical Operations
& = ? A B A&B 1 3/14/2020 ECE Application Programming: Lecture 35
8
Bitwise Logical Operations
& = ? A B A&B 1 3/14/2020 ECE Application Programming: Lecture 35
9
Bitwise Logical Operations
| = ? A B A | B 1 3/14/2020 ECE Application Programming: Lecture 35
10
Bitwise Logical Operations
ECE 160 02/02/2005 Bitwise Logical Operations | = ? A B A | B 1 3/14/2020 ECE Application Programming: Lecture 35 (c) 2005, P. H. Viall
11
Bitwise Logical Operations
| = ? A B A | B 1 3/14/2020 ECE Application Programming: Lecture 35
12
Bitwise Logical Operations
^ = ? A B A ^ B 1 3/14/2020 ECE Application Programming: Lecture 35
13
Bitwise Logical Operations
^ = ? A B A ^ B 1 3/14/2020 ECE Application Programming: Lecture 35
14
Bitwise Logical Operations
^ = ? A B A ^ B 1 3/14/2020 ECE Application Programming: Lecture 35
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 3/14/2020 ECE Application Programming: Lecture 35
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 = 3/14/2020 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 & ^ | 3/14/2020 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) 3/14/2020 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 | = 3/14/2020 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 3/14/2020 ECE Application Programming: Lecture 36
21
Example: Common bitwise operations
Given an unsigned int, n, and a number, b, how would you: Clear all bits of n? Clear the lower 16 bits of n (mask out lower bits)? Flip all bits of n? Flip bit b of n? Set bit b of n (i.e., make sure bit b is 1)? Clear bit b of n (i.e., make sure bit b is 0)? Note: 0 ≤ b ≤ 31; least significant (rightmost) bit is bit 0 3/14/2020 ECE Application Programming: Lecture 32
22
ECE Application Programming: Lecture 32
Example solution Given an unsigned int, n, and a number, b, how would you: Clear all bits of n? n = 0; Clear the lower 16 bits of n (mask out lower bits)? X & 0 = 0, regardless of whether X = 0 or X = 1 Should AND lower 16 bits with 0 n = n & 0xFFFF0000; Flip all bits of n? n = ~n; 3/14/2020 ECE Application Programming: Lecture 32
23
Example solution (cont.)
Given an unsigned int, n, and a number, b, how would you: Flip bit b of n? X ^ 1 = ~X, regardless of whether X = 0 or X = 1 Need 1 in bit position b (1 << b) n = n ^ (1 << b); Set bit b of n (i.e., make sure bit b is 1)? X | 1 = 1, regardless of whether X = 0 or X = 1 n = n | (1 << b); Clear bit b of n (i.e., make sure bit b is 0)? As shown before, X & 0 = 0 To get 0 in specific bit position, shift 1 to that position and then invert bit mask: ~(1 << b) n = n & ~(1 << b); 3/14/2020 ECE Application Programming: Lecture 32
24
Common bitwise operations
General operation Logical operation Bit mask values in positions that change Bit mask values in positions staying same Example: modify bits 8-23 (middle 16 bits) Set bit(s) Bits changed to 1 OR 1 n = n | 0x00FFFF00 Clear bit(s) Bits changed to 0 AND n = n & 0xFF0000FF Flip bit(s) All 0 1; All 1 0 XOR n = n ^ 0x00FFFF00 3/14/2020 ECE Application Programming: Lecture 32
25
ECE Application Programming: Lecture 32
Extracting bits Very common to extract bits from larger value One example: instruction decoding Instruction: basic operation executed by processor Decoding: figure out what each bit group “means” First bits typically operation; others choose data to be used Examples: 0xABCD1234 = Lowest 16 bits = 0x1234 Upper 16 bits = 0xABCD Bits = 0xAB Bits 1-6 look at lowest 8 bits (bits 0-7) bits 1-6 = = 0x1A 3/14/2020 ECE Application Programming: Lecture 32
26
Extracting bits (cont.)
Isolate bits you want AND with bit mask to clear unwanted bits Positions you want to keep = 1 Positions you want to clear = 0 Examples: To get lowest 16 bits mask = 0x0000FFFF To get upper 16 bits mask = 0xFFFF0000 To get bits mask = 0xFF000000 To get bits 1-6 mask = = 0x E 3/14/2020 ECE Application Programming: Lecture 32
27
Extracting bits (cont.)
Shift bits to right Shift amount = original position of lowest bit Examples: Lowest 16 bits bits 0-15 no shift Upper 16 bits bits shift right by 16 Bits shift right by 24 Bits 1-6 shift right by 1 Order doesn’t really matter Could shift first and then AND to mask out upper bits Can combine steps in single operation Upper 16 bits of x = (x & 0xFFFF0000) >> 16 Bits 1-6 of x = (x & 0x E) >> 1 3/14/2020 ECE Application Programming: Lecture 32
28
ECE Application Programming: Lecture 35
Next time Exam 3 Preview (Wednesday, 12/11) Reminders: M 12/9: Last day for P7 submissions (initial submission) Th 12/12: last day of classes; P8 & Ch. 9 exercises due P8 deals with structures (lectures 26-27, 29-30) M 12/16: Exam 3, 3-6 PM, Ball 210 Will post course evals online; you’ll submit eval at exam T 12/17: All code due by end of day Program 9: Worth up to 4 points extra credit on final avg Resubmission deadline for P6, P7, & P8 3/14/2020 ECE Application Programming: Lecture 35
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.