Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.

Slides:



Advertisements
Similar presentations
7-5 Microoperation An elementary operations performed on data stored in registers or in memory. Transfer Arithmetic Logic: perform bit manipulation on.
Advertisements

Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Introduction to C Programming CE Lecture 8 Bitwise Operations.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
C expressions (Reek, Ch. 5) 1CS 3090: Safety Critical Programming in C.
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.
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?
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
1 Programming in Machine Language SCSC 311 Spring 2011.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
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.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
REGISTER TRANSFER LANGUAGE MICROOPERATIONS. TODAY OUTLINES Logic Microoperations Shift Microoperations.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
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.
10/12/2015 Updated from Alex’s excellent note
Bit Manipulation. Get Bit from the particular Position Suppose you want to get a bit from the 4 th position. You can call int get_bit_val(int number_send,int.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Module 5 JavaScript Operators. CS346 Javascript-52 Examples  JS-5 Examples.
20. LOW-LEVEL PROGRAMMING. Bitwise Shift Operators The bitwise shift operators are: > right shift The expression i
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
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.
Announcements Quiz this Thursday 1. Multi dimensional arrays A student got a warning when compiling code like: int foo(char **a) { } int main() { char.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Lecture 10 union, enumeration, bit
From bits to bytes to ints
CSE 220 – C Programming Expressions.
Instructor: David Ferry
Bit Operations Horton pp
Number Systems and Bitwise Operation
By: Muhammad Zidny Naf’an
Andy Wang Object Oriented Programming in C++ COP 3330
More about Numerical Computation
Chapter 14 Bitwise Operators Objectives
Bits and Bytes Topics Representing information as bits
REGISTER TRANSFER LANGUAGE
Bits and Bytes Topics Representing information as bits
Bits and Bytes Boolean algebra Expressing in C
The University of Adelaide, School of Computer Science
Introduction to Programming
Bits and Bytes Topics Representing information as bits
Bitwise Operators CS163 Fall 2018.
Embedded Programming in C
CS-401 Computer Architecture & Assembly Language Programming
CET 3510 – Lecture 11 Bit Manipulation in a High-Level Programming Language Dr. José M. Reyes Álamo.
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bit-wise and bit-shift operators
Homework Homework Continue Reading K&R Chapter 2 Questions?
Comp Org & Assembly Lang
Bitwise Operators.
Bitwise operators.
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Bitwise Operators.
Bit Manipulations CS212.
Bit Operations Horton pp
Presentation transcript:

Bitwise Operators in C

Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.

Bitwise Operators in C There are six bit operators: bitwise AND(&) bitwise OR(|) bitwise XOR(^) bitwise complement(~) left shift(<<) right shift(>>)

Bitwise Operators in C Bitwise and: c1 & c2 # include main() { char c1 = 4,c2 = 6,c3 ; c3 = c1 & c2; printf("\n Bitwise AND i.e. c1 & c2 = %d",c3); } Bitwise AND i.e. c1 & c2 = 4 Suppose c1 = 4, c2 = 6; The value of c1 & c2 is interpreted: &

Bitwise Operators in C Bitwise or: c1 | c2 # include main() { char c1 = 4,c2 =6,c3 = 3; c3 = c1 | c2; printf("\n Bitwise OR i.e. c1 | c2 = %c",c3); } Bitwise OR i.e. c1 | c2 = ? Suppose c1 = 4, c2 = 6; The value of c1 | c2 is interpreted as follows: |

Bitwise Operators in C Bitwise XOR: c1 ^ c2 # include main() { char c1 = 4,c2 = 6,c3 = 3; c3 = c1 ^ c2; printf("\n Bitwise XOR i.e. c1 ^ c2 = %c",c3); } Suppose c1 = 4, c2 = 6; The value of c1 ^ c2 is interpreted as follows: ^

Bitwise Operators in C Complement: ~ # include main() { char c1 = 4,c2 = 6,c3 = 3; c3 = ~c1; printf("\n ones complement of c1 = %c",c3); } Suppose c1 = 4, c2 = 6; The value of ~ c1 is interpreted as follows: ~

Bitwise Operators in C Left shift operator # include main() { char c1 = 1,c2 = 2,c3 = 3; c3 = c1<<2; printf("\n left shift by 2 bits c1 << 2 = %c",c3); } c3 = c1 << 2; The bits are shifted left by two places. c1 is It is shifted 2 bits to the left ** While shifting, the high-order (left) bits are discarded. The vacuum on the right side is filled with 0s.

Bitwise Operators in C Right shift operation # include main() { char c1 = 1,c2 = 2,c3 = 3; c3 = c1>>2; printf("\n right shift by 2 bits c1 >> 2 = %c",c3); }

Bitwise Operators in C Bitwise AND xi yi xi &1 yi Variable b3 b2 b1 b0 x y z = x & y

Bitwise Operators in C Bitwise OR xi yi xi |1 yi Variable b3 b2 b1 b0 x y z = x | y

Bitwise Operators in C Bitwise XOR xi yi xi ^1 yi Variable b3 b2 b1 b0 x y z = x ^ y

Bitwise Operators in C Bitwise NOT xi ~1 xi Variable b3 b2 b1 b0 x z = ~x