EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

DATA REPRESENTATION CONVERSION.
Chapter Chapter Goals Know the different types of numbers Describe positional notation.
Chapter 02 Binary Values and Number Systems Nell Dale & John Lewis.
Number System Conversions Lecture L2.2 Section 2.3.
Number Systems.
CS105 INTRODUCTION TO COMPUTER CONCEPTS BINARY VALUES & NUMBER SYSTEMS Instructor: Cuong (Charlie) Pham.
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,
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
Chapter 2 Binary Values and Number Systems. 2 2 Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645,
Number systems, Operations, and Codes
Positional Notation 642 in base 10 positional notation is:
Number Base Conversions
Number Systems Binary to Decimal Octal to Decimal Hexadecimal to Decimal Binary to Octal Binary to Hexadecimal Two’s Complement.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
Cis303a_chapt03_exam1_answer.ppt CIS303A: System Architecture Exam 1: Chapter 3 Answer List the characters (digits) for the following bases. 1) Decimal:
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.
Binary Values. Numbers Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645, 32 Negative Numbers.
ECE Application Programming
Lecturer: Santokh Singh
ECE Application Programming
ECE Application Programming
ECE Application Programming
By: Jonathan O. Cabriana
Integer Real Numbers Character Boolean Memory Address CPU Data Types
ECE Application Programming
ECE Application Programming
ECE Application Programming
Data Representation Integers
ECE Application Programming
ECE 103 Engineering Programming Chapter 3 Numbers
Bit Operations Horton pp
Number System conversions
Number Systems and Bitwise Operation
Fundamentals & Ethics of Information Systems IS 201
University of Gujrat Department of Computer Science
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
Numbering System TODAY AND TOMORROW 11th Edition
There are 10 types of people of people in this world…
EECE.2160 ECE Application Programming
Homework Homework Continue Reading K&R Chapter 2 Questions?
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
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
Bitwise operators.
Module 10 Operations on Bits
Remember the 10 types of people of people in this world…
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
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.2160 ECE Application Programming Instructors: Dr. Michael Geiger & Dr. Peilong Li Spring 2017 Lecture 35 Bitwise operators

ECE Application Programming: Lecture 30 Lecture outline Announcements/reminders Program 9 due 4/28 No late penalties—only extra credit +15% for submitting assignment “on time” (by 4/28) +10% for submitting assignment 4/29-5/1 +5% for submitting assignment 5/2-5/4 All outstanding program regrades due 4/28 Remember, e-mail the appropriate TA and CC your instructor when you resubmit—do not just e-mail Dr. Geiger Exam 2 resubmissions due 4/28 Receive credit for Question 2b Today’s lecture Bitwise operators 2/28/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Exam 2 Preview Review: I/O Unformatted I/O: size_t fwrite(pointer, element size, # elements, file_handle) size_t fread(pointer, element size, # elements, file_handle) Check for EOF using either fscanf() result or feof(FILE *) Character input int fgetc(FILE *stream); int getchar(); int ungetc(int c, FILE *stream); Line input char *fgets(char *s, int n, FILE *stream); 2/28/2019 ECE Application Programming: Exam 2 Preview

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: 0010001 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 2/28/2019 ECE Application Programming: Lecture 30

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) 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30 (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 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30

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 2/28/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 30 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 2/28/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 30 Review: C operators Operator Associativity Innermost ( ) Left to right Unary -, unary ~ Right to left * / % + - << >> NOTE: shift amt < 32 & ^ | 2/28/2019 ECE Application Programming: Lecture 30

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) 2/28/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 30 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 2/28/2019 ECE Application Programming: Lecture 30

ECE Application Programming: Lecture 30 Final notes Next time Finish bitwise operators Reminders: Program 9 due 4/28 No late penalties—only extra credit +15% for submitting assignment “on time” (by 4/28) +10% for submitting assignment 4/29-5/1 +5% for submitting assignment 5/2-5/4 All outstanding program regrades due 4/28 Remember, e-mail the appropriate TA and CC your instructor when you resubmit—do not just e-mail Dr. Geiger Exam 2 resubmissions due 4/28 Receive credit for Question 2b 2/28/2019 ECE Application Programming: Lecture 30