SlideShare a Scribd company logo
C Bitwise Operators
Bitwise operators are used to manipulate one or more bits from integral operands like
char, int, short, long, here we will see the basics of bitwise operators, and some useful
tips for manipulating the bits to achieve a task. Bitwise operators operate on
individual bits of integer (int and long) values. This is useful for writing low-level
hardware or OS code where the ordinary abstractions of numbers, characters, pointers,
and so on, are insufficient. Bit manipulation code tends to be less "portable". Code is
"portable" if without any programmer intervention it compiles and runs correctly on
different types of computers. The bit wise operations are commonly used with
unsigned types. These operators perform bit wise logical operations on values. Both
operands must be of the same type and width: the resultant value will also be this type
and width. A bitwise operator works on each bit of data. Bitwise operators are used in
bit level programming the Bitwise operators supported by C language are listed in the
following table.
Assume variable A holds 60 and variable B holds 13 then:
Operator Description Example
&
Binary AND Operator copies a bit to the
result if it exists in both operands.
(A & B) will give 12 which
is 0000 1100
|
Binary OR Operator copies a bit if it exists in
either operand.
(A | B) will give 61 which is
0011 1101
^
Binary XOR Operator copies the bit if it is set
in one operand but not both.
(A ^ B) will give 49 which is
0011 0001
~
Binary Ones Complement Operator is unary
and has the effect of 'flipping' bits.
(~A ) will give -61 which is
1100 0011 in 2's complement
form due to a signed binary
number.
<<
Binary Left Shift Operator. The left operands
value is moved left by the number of bits
specified by the right operand.
A << 2 will give 240 which
is 1111 0000
>>
Binary Right Shift Operator. The left
operands value is moved right by the number
of bits specified by the right operand.
A >> 2 will give 15 which is
0000 1111
Bitwise OR – |
Bitwise OR operator | takes 2 bit patterns, and perform OR operations on each pair of
corresponding bits. The following example will explain it.
1010
1100
--------
OR 1110
--------
The Bitwise OR, will take pair of bits from each position, and if any one of the bit is
1, the result on that position will be 1.
Bitwise AND – &
Bitwise AND operator &, takes 2 bit patterns, and perform AND operations with it.
1010
1100
-------
AND 1000
-------
The Bitwise AND will take pair of bits from each position, and if only both the bit is
1, the result on that position will be 1
One’s Complement operator – ~
One’s complement operator (Bitwise NOT) is used to convert each “1-bit to 0-bit”
and “0-bit to 1-bit”, in the given binary pattern. It is a unary operator i.e. it takes only
one operand.
1001
NOT
-------
0110
-------
Bitwise XOR – ^
Bitwise XOR ^, takes 2 bit patterns and perform XOR operation with it.
0101
0110
------
XOR 0011
------
The Bitwise XOR will take pair of bits from each position, and if both the bits are
different, the result on that position will be 1. If both bits are same, then the result on
that position is 0.
Bit a Bit b a & b a | b a ^ b ~a
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=3;
printf("%dn",a|b);
printf("%dn",a&b);
printf("%dn",a^b);
getch();
}
Right Shift
a = 0000 0011 = 3
(a<<=1) = 00000110 = 6
(a<<=2) = 00011000 = 24
(a<<=3) = 11000000 = 192
Left Shift
a=11000000 =192
(a>>=1) = 01100000 = 96
(a>>=2) = 00011000 = 24
(a>>=3) = 0000 0011 = 3
a = 12
b = 10
---------------------------------
a in Binary : 0000 0000 0000 1100
b in Binary : 0000 0000 0000 1010
---------------------------------
a | b : 0000 0000 0000 1110
a & b : 0000 0000 0000 1000
a ^ b : 0000 0000 0000 0110
a | b : 0000 0000 0000 1110 = 14
a & b : 0000 0000 0000 1000 = 8
a ^ b : 0000 0000 0000 0110 = 6

More Related Content

What's hot (20)

PPTX
Stack and its Applications : Data Structures ADT
Soumen Santra
 
PPT
Data structure lecture 5
Kumar
 
PPTX
Control structures in c++
Nitin Jawla
 
PPT
Binary operator overloading
BalajiGovindan5
 
PPTX
Expression and Operartor In C Programming
Kamal Acharya
 
PPT
stack presentation
Shivalik college of engineering
 
PPT
Java operators
Shehrevar Davierwala
 
PDF
Packages - PL/SQL
Esmita Gupta
 
PPT
Circular queues
Ssankett Negi
 
PPTX
Quick sort
Dhruv Sabalpara
 
PPTX
Floating point representation
missstevenson01
 
PPTX
1s and 2s complement
Then Murugeshwari
 
PPT
Infix to Postfix Conversion Using Stack
Soumen Santra
 
PPT
Queue implementation
Rajendran
 
PDF
Function overloading ppt
Prof. Dr. K. Adisesha
 
PPTX
Python list
ArchanaBhumkar
 
PPT
Infix prefix postfix
Self-Employed
 
PPTX
queue & its applications
somendra kumar
 
PPT
One Dimensional Array
dincyjain
 
PPTX
Operator.ppt
Darshan Patel
 
Stack and its Applications : Data Structures ADT
Soumen Santra
 
Data structure lecture 5
Kumar
 
Control structures in c++
Nitin Jawla
 
Binary operator overloading
BalajiGovindan5
 
Expression and Operartor In C Programming
Kamal Acharya
 
Java operators
Shehrevar Davierwala
 
Packages - PL/SQL
Esmita Gupta
 
Circular queues
Ssankett Negi
 
Quick sort
Dhruv Sabalpara
 
Floating point representation
missstevenson01
 
1s and 2s complement
Then Murugeshwari
 
Infix to Postfix Conversion Using Stack
Soumen Santra
 
Queue implementation
Rajendran
 
Function overloading ppt
Prof. Dr. K. Adisesha
 
Python list
ArchanaBhumkar
 
Infix prefix postfix
Self-Employed
 
queue & its applications
somendra kumar
 
One Dimensional Array
dincyjain
 
Operator.ppt
Darshan Patel
 

Viewers also liked (8)

PPT
logical 8051
VJ Aiswaryadevi
 
PDF
Module 00 Bitwise Operators in C
Tushar B Kute
 
PDF
Operating Systems - Dynamic Memory Management
Emery Berger
 
PDF
Bitwise AND operator in c
Innovative
 
PPT
C language Unit 2 Slides, UPTU C language
Anurag University Hyderabad
 
PPT
C Prog. - Operators and Expressions
vinay arora
 
PPSX
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
PDF
Deep C
Olve Maudal
 
logical 8051
VJ Aiswaryadevi
 
Module 00 Bitwise Operators in C
Tushar B Kute
 
Operating Systems - Dynamic Memory Management
Emery Berger
 
Bitwise AND operator in c
Innovative
 
C language Unit 2 Slides, UPTU C language
Anurag University Hyderabad
 
C Prog. - Operators and Expressions
vinay arora
 
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
Deep C
Olve Maudal
 
Ad

Similar to C bitwise operators (20)

PPTX
Cse lecture-4.2-c bit wise operators and expression
FarshidKhan
 
PPTX
CH-2 Encodings.pptx Classes 9 to 12 coading
10shsicse
 
PPTX
Sc
Subha Selvam
 
PPTX
btwggggggggggggggggggggggggggggggisop correct (1).pptx
Orin18
 
PPTX
object oriented programming in javaTERNARY OPERATORS.pptx
riazahamed37
 
PPTX
Bitwise operators
Megha Sharma
 
DOC
Report on c
jasmeen kr
 
PDF
Lecture 11 bitwise_operator
eShikshak
 
PPT
Bit manipulation
Priyanka Yadav
 
DOCX
Bit shift operators
alldesign
 
PPTX
09. session 9 operators and statements
Phúc Đỗ
 
PDF
Bitwise Operations(1).pdf
DalvinCalvin
 
PPT
Low Level Prog. (from 201-c).ppt
LearnWithJCM
 
PPT
Java 2
Preethi Nambiar
 
PPT
Java - Operators
Preethi Nambiar
 
PPTX
Explanation and importance of Bitwiseoperator.pptx
pratyushghimire193
 
PPT
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
PPTX
Microoperations
Rakesh Pillai
 
PDF
Python : basic operators
S.M. Salaquzzaman
 
PDF
Logic microoperations
Nitesh Singh
 
Cse lecture-4.2-c bit wise operators and expression
FarshidKhan
 
CH-2 Encodings.pptx Classes 9 to 12 coading
10shsicse
 
btwggggggggggggggggggggggggggggggisop correct (1).pptx
Orin18
 
object oriented programming in javaTERNARY OPERATORS.pptx
riazahamed37
 
Bitwise operators
Megha Sharma
 
Report on c
jasmeen kr
 
Lecture 11 bitwise_operator
eShikshak
 
Bit manipulation
Priyanka Yadav
 
Bit shift operators
alldesign
 
09. session 9 operators and statements
Phúc Đỗ
 
Bitwise Operations(1).pdf
DalvinCalvin
 
Low Level Prog. (from 201-c).ppt
LearnWithJCM
 
Java - Operators
Preethi Nambiar
 
Explanation and importance of Bitwiseoperator.pptx
pratyushghimire193
 
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
Microoperations
Rakesh Pillai
 
Python : basic operators
S.M. Salaquzzaman
 
Logic microoperations
Nitesh Singh
 
Ad

More from Suneel Dogra (20)

PPT
Business model
Suneel Dogra
 
PDF
Internet
Suneel Dogra
 
PDF
Html
Suneel Dogra
 
PDF
Dreamweaver
Suneel Dogra
 
PDF
Advanced html
Suneel Dogra
 
PDF
Sql
Suneel Dogra
 
PDF
File organisation
Suneel Dogra
 
PDF
Distributed databases
Suneel Dogra
 
PDF
Database models
Suneel Dogra
 
PDF
Data base management system
Suneel Dogra
 
PPT
Web sitedesignpart1
Suneel Dogra
 
PPT
Web sitedesignpart1
Suneel Dogra
 
PPT
Internet security
Suneel Dogra
 
PDF
What is the linux
Suneel Dogra
 
DOC
He 12 different types of servers that every techie should know about
Suneel Dogra
 
PDF
Bachelor of computer application b.c.a.-2014
Suneel Dogra
 
DOC
Cloud computing application
Suneel Dogra
 
DOC
Fast track to linux
Suneel Dogra
 
DOC
A sorted linear array
Suneel Dogra
 
DOC
String in c
Suneel Dogra
 
Business model
Suneel Dogra
 
Internet
Suneel Dogra
 
Dreamweaver
Suneel Dogra
 
Advanced html
Suneel Dogra
 
File organisation
Suneel Dogra
 
Distributed databases
Suneel Dogra
 
Database models
Suneel Dogra
 
Data base management system
Suneel Dogra
 
Web sitedesignpart1
Suneel Dogra
 
Web sitedesignpart1
Suneel Dogra
 
Internet security
Suneel Dogra
 
What is the linux
Suneel Dogra
 
He 12 different types of servers that every techie should know about
Suneel Dogra
 
Bachelor of computer application b.c.a.-2014
Suneel Dogra
 
Cloud computing application
Suneel Dogra
 
Fast track to linux
Suneel Dogra
 
A sorted linear array
Suneel Dogra
 
String in c
Suneel Dogra
 

Recently uploaded (20)

PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
Top Managed Service Providers in Los Angeles
Captain IT
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 

C bitwise operators

  • 1. C Bitwise Operators Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long, here we will see the basics of bitwise operators, and some useful tips for manipulating the bits to achieve a task. Bitwise operators operate on individual bits of integer (int and long) values. This is useful for writing low-level hardware or OS code where the ordinary abstractions of numbers, characters, pointers, and so on, are insufficient. Bit manipulation code tends to be less "portable". Code is "portable" if without any programmer intervention it compiles and runs correctly on different types of computers. The bit wise operations are commonly used with unsigned types. These operators perform bit wise logical operations on values. Both operands must be of the same type and width: the resultant value will also be this type and width. A bitwise operator works on each bit of data. Bitwise operators are used in bit level programming the Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13 then: Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 | Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111 Bitwise OR – | Bitwise OR operator | takes 2 bit patterns, and perform OR operations on each pair of corresponding bits. The following example will explain it. 1010 1100 -------- OR 1110 -------- The Bitwise OR, will take pair of bits from each position, and if any one of the bit is 1, the result on that position will be 1.
  • 2. Bitwise AND – & Bitwise AND operator &, takes 2 bit patterns, and perform AND operations with it. 1010 1100 ------- AND 1000 ------- The Bitwise AND will take pair of bits from each position, and if only both the bit is 1, the result on that position will be 1 One’s Complement operator – ~ One’s complement operator (Bitwise NOT) is used to convert each “1-bit to 0-bit” and “0-bit to 1-bit”, in the given binary pattern. It is a unary operator i.e. it takes only one operand. 1001 NOT ------- 0110 ------- Bitwise XOR – ^ Bitwise XOR ^, takes 2 bit patterns and perform XOR operation with it. 0101 0110 ------ XOR 0011 ------ The Bitwise XOR will take pair of bits from each position, and if both the bits are different, the result on that position will be 1. If both bits are same, then the result on that position is 0. Bit a Bit b a & b a | b a ^ b ~a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0 #include<stdio.h> #include<conio.h> void main() { int a=5,b=3; printf("%dn",a|b); printf("%dn",a&b); printf("%dn",a^b); getch(); }
  • 3. Right Shift a = 0000 0011 = 3 (a<<=1) = 00000110 = 6 (a<<=2) = 00011000 = 24 (a<<=3) = 11000000 = 192 Left Shift a=11000000 =192 (a>>=1) = 01100000 = 96 (a>>=2) = 00011000 = 24 (a>>=3) = 0000 0011 = 3 a = 12 b = 10 --------------------------------- a in Binary : 0000 0000 0000 1100 b in Binary : 0000 0000 0000 1010 --------------------------------- a | b : 0000 0000 0000 1110 a & b : 0000 0000 0000 1000 a ^ b : 0000 0000 0000 0110 a | b : 0000 0000 0000 1110 = 14 a & b : 0000 0000 0000 1000 = 8 a ^ b : 0000 0000 0000 0110 = 6