EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
Advertisements

A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
ECE Application Programming
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
Microprocessor Systems Design I
Microprocessor Systems Design I
ECE Application Programming
Bit Operations Horton pp
What to bring: iCard, pens/pencils (They provide the scratch paper)
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
Formatting Output.
LING 388: Computers and Language
EECE.3170 Microprocessor Systems Design I
Bits, Bytes, and Integers 2nd Lectures
Homework Homework Continue Reading K&R Chapter 2 Questions?
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
EECE.2160 ECE Application Programming
ECE Application Programming
Bitwise Operators.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Module 10 Operations on Bits
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
EECE.2160 ECE Application Programming
Bit Operations Horton pp
EECE.2160 ECE Application Programming
Presentation transcript:

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