1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

Slides:



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

Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
ICS 2005 Instructor: Peter A. Dinda TA: Bin Lin Recitation 2.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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.
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 Bitwise Operators. 2 Bitwise Operators (integers) Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement"
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to use the bitwise logical operators in programs ❏ To be able to use.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP Spring 2014 C Part V.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
CSE 220 – C Programming Expressions.
CSE 220 – C Programming Bitwise Operators.
The Machine Model Memory
Data types Data types Basic types
Operators And Expressions
Instructor: David Ferry
Chapter 3 - Operators Arithmetic Operators Assignment
Bit Operations Horton pp
Number Systems and Bitwise Operation
Enumerations.
Introduction to Programming and the C Language
Lecture 5 from (Chapter 4, pages 73 to 96)
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Andy Wang Object Oriented Programming in C++ COP 3330
More about Numerical Computation
Chapter 14 Bitwise Operators Objectives
Enumerations.
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bits and Bytes Boolean algebra Expressing in C
Bits and Bytes Topics Representing information as bits
Chapter-3 Operators.
Bits and Bytes Topics Representing information as bits
Homework Homework Continue Reading K&R Chapter 2 Questions?
Comp Org & Assembly Lang
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Bitwise operators.
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Bitwise Operators.
DATA TYPES There are four basic data types associated with variables:
Bit Manipulations CS212.
Bit Operations Horton pp
Presentation transcript:

1 Bitwise Operators

2 Bits and Constants

3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement" operator ~

4 ExpressionRepresentationValue a b a & b a ^ b a | b ~ ( a | b ) ~ a & ~ b Representations int a = 33333, b = ; The value of each bit is determined only by the bit(s) in its position

5 Left Shifts short a = 0x68ab;... a <<= 3; /* shift left 3 bits */ Same as: a = a << 3; Bits positions vacated by shift are filled with zeros

6 Right Shifts - Unsigned unsigned short a = 0x98ab;... a >>= 5; /* shift right 5 bits */ For unsigned data type, bits positions vacated by shift are filled with zeros.

7 Right Shifts - Signed (machine dependent) short a = 0x98ab;... a >>= 5; /* shift right 5 bits */ Bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type

8 Right Shifts (Signed) short a = 0x78ab;... a >>= 5; /* shift right 5 bits */ Bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type

9 Expressio n RepresentationAction c unshifted c << left shifted 4 a unshifted a >> right shifted 3 b unshifted b >> right shifted 3 Representations char c = 'Z'; int a = 1 << 31; /* shift 1 to the high bit */ unsigned b = 1 << 31; For signed data types bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type

10 Implementation Note x<<n is equivalent to multiplication by 2 n. x>>n is equal to x/2 n Shifting is much faster than actual multiplication (*) or division (/) ==> Multiplications / divisions by powers of 2 should be implemented using shifts. 0x0001 0x0002 0x0004 0x0008 0x0010 0x0020 0x

11 int main(void){ int num=0; do{ printf("Enter an integer:\n"); scanf("%d", &num); bit_print(num); putchar('\n'); } while (num!=0); return 0; } Printing the bits of an integer Prints the binary representation of an integer. E.g: (MSB) (LSB)

12 #include void bit_print(int a) { int i; int n = sizeof(int) * CHAR_BIT; /* #define CHAR_BIT 8 (in )*/ int mask = 1 << (n - 1); /* mask = */ for (i = 1; i <= n; ++i) { putchar((a & mask)? '1' : '0'); a <<= 1; if (i % CHAR_BIT == 0 && i < n) putchar(' '); } n is the number of bits in an integer Prints the most significant bit of a Prints a space between the bytes (condition) ? (if true) : (if false) if (a&mask == 0) putchar(`0`); else putchar(`1`); i % 8 == i & 7

13 Pack 4 chars into an int #include int pack( char a, char b, char c, char d ) { int p = a; p = (p << CHAR_BIT) | b; p = (p << CHAR_BIT) | c; p = (p << CHAR_BIT) | d; return p; } p = a p = 0 0 a b 0 0 a b p = 0 a b c 0 a b c p = a b c d a b c d Most significant Least significant

14 Unpack a byte from an int #include char unpack( int p, int k ) { unsigned mask = 0xFF; int n = k * CHAR_BIT; mask <<= n; return ( ( p & mask ) >> n ); } k = 0, 1, 2 or 3 n = 0, 8, 16 or 24 k th byte is on

15 OperatorsAssociativity () []. -> ++(postfix) --(postfix)left to right +(unary) -(unary) ++(prefix) --(prefix) ! sizeof(type) &(address) *(dereference) ~ right to left * / %left to right + -left to right >left to right >=left to right == !=left to right & ^ | &&left to right ||left to right ?:right to left = += -= *= /= &= >>= etcright to left,(comma operator)left to right Operator precedence and associativity - final look (a+b > b) is equivalent to (((a+b) >b)