SlideShare uma empresa Scribd logo
1 de 3
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

Mais conteúdo relacionado

Mais procurados

Basic logic gates
Basic logic gatesBasic logic gates
Basic logic gates
Kumar
 

Mais procurados (20)

8086 Instruction set
8086 Instruction set8086 Instruction set
8086 Instruction set
 
Boolean expression org.
Boolean expression org.Boolean expression org.
Boolean expression org.
 
Logic gate
Logic gateLogic gate
Logic gate
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
 
8051 architecture
8051 architecture8051 architecture
8051 architecture
 
digital logic design Chapter 2 boolean_algebra_&_logic_gates
digital logic design Chapter 2 boolean_algebra_&_logic_gatesdigital logic design Chapter 2 boolean_algebra_&_logic_gates
digital logic design Chapter 2 boolean_algebra_&_logic_gates
 
Binary and hex input/output (in 8086 assembuly langyage)
Binary and hex input/output (in 8086 assembuly langyage)Binary and hex input/output (in 8086 assembuly langyage)
Binary and hex input/output (in 8086 assembuly langyage)
 
Karnaugh map peresentation
Karnaugh map  peresentationKarnaugh map  peresentation
Karnaugh map peresentation
 
Adder & subtractor (Half adder, Full adder, Half subtractor, Full subtractor)
Adder & subtractor (Half adder, Full adder, Half subtractor, Full subtractor)Adder & subtractor (Half adder, Full adder, Half subtractor, Full subtractor)
Adder & subtractor (Half adder, Full adder, Half subtractor, Full subtractor)
 
Adder ppt
Adder pptAdder ppt
Adder ppt
 
Arithmetic micro operations
Arithmetic micro operationsArithmetic micro operations
Arithmetic micro operations
 
1’s and 2’s complements
1’s and 2’s complements1’s and 2’s complements
1’s and 2’s complements
 
Multiplication & division instructions microprocessor 8086
Multiplication & division instructions microprocessor 8086Multiplication & division instructions microprocessor 8086
Multiplication & division instructions microprocessor 8086
 
Assignment on alp
Assignment on alpAssignment on alp
Assignment on alp
 
Registers and counters
Registers and countersRegisters and counters
Registers and counters
 
Lecture #3 Flag Register.pptx
Lecture #3 Flag Register.pptxLecture #3 Flag Register.pptx
Lecture #3 Flag Register.pptx
 
Parallel Adder and Subtractor
Parallel Adder and SubtractorParallel Adder and Subtractor
Parallel Adder and Subtractor
 
Binary Arithmetic Operations
Binary Arithmetic OperationsBinary Arithmetic Operations
Binary Arithmetic Operations
 
Basic logic gates
Basic logic gatesBasic logic gates
Basic logic gates
 
Encoders
EncodersEncoders
Encoders
 

Destaque

C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
vinay arora
 

Destaque (8)

logical 8051
logical 8051logical 8051
logical 8051
 
Module 00 Bitwise Operators in C
Module 00 Bitwise Operators in CModule 00 Bitwise Operators in C
Module 00 Bitwise Operators in C
 
Operating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory ManagementOperating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory Management
 
Bitwise AND operator in c
Bitwise AND operator in cBitwise AND operator in c
Bitwise AND operator in c
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
Deep C
Deep CDeep C
Deep C
 

Semelhante a C bitwise operators

Lecture 11 bitwise_operator
Lecture 11 bitwise_operatorLecture 11 bitwise_operator
Lecture 11 bitwise_operator
eShikshak
 
09. session 9 operators and statements
09. session 9   operators and statements09. session 9   operators and statements
09. session 9 operators and statements
Phúc Đỗ
 
Logic microoperations
Logic microoperationsLogic microoperations
Logic microoperations
Nitesh Singh
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
CtOlaf
 

Semelhante a C bitwise operators (20)

Cse lecture-4.2-c bit wise operators and expression
Cse lecture-4.2-c bit wise operators and expressionCse lecture-4.2-c bit wise operators and expression
Cse lecture-4.2-c bit wise operators and expression
 
Sc
ScSc
Sc
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
 
Report on c
Report on cReport on c
Report on c
 
Lecture 11 bitwise_operator
Lecture 11 bitwise_operatorLecture 11 bitwise_operator
Lecture 11 bitwise_operator
 
Bit manipulation
Bit manipulationBit manipulation
Bit manipulation
 
Bit shift operators
Bit shift operatorsBit shift operators
Bit shift operators
 
09. session 9 operators and statements
09. session 9   operators and statements09. session 9   operators and statements
09. session 9 operators and statements
 
Bitwise Operations(1).pdf
Bitwise Operations(1).pdfBitwise Operations(1).pdf
Bitwise Operations(1).pdf
 
Low Level Prog. (from 201-c).ppt
Low Level Prog. (from 201-c).pptLow Level Prog. (from 201-c).ppt
Low Level Prog. (from 201-c).ppt
 
Java - Operators
Java - OperatorsJava - Operators
Java - Operators
 
Java 2
Java 2Java 2
Java 2
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
 
Microoperations
MicrooperationsMicrooperations
Microoperations
 
Python : basic operators
Python : basic operatorsPython : basic operators
Python : basic operators
 
Logic microoperations
Logic microoperationsLogic microoperations
Logic microoperations
 
unit 3.pptx
unit 3.pptxunit 3.pptx
unit 3.pptx
 
Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...
Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...
Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...
 
Logic Micro Operation
Logic Micro OperationLogic Micro Operation
Logic Micro Operation
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
 

Mais de Suneel Dogra (20)

Business model
Business modelBusiness model
Business model
 
Internet
InternetInternet
Internet
 
Html
HtmlHtml
Html
 
Dreamweaver
DreamweaverDreamweaver
Dreamweaver
 
Advanced html
Advanced htmlAdvanced html
Advanced html
 
Sql
SqlSql
Sql
 
File organisation
File organisationFile organisation
File organisation
 
Distributed databases
Distributed databasesDistributed databases
Distributed databases
 
Database models
Database models Database models
Database models
 
Data base management system
Data base management systemData base management system
Data base management system
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1
 
Internet security
Internet securityInternet security
Internet security
 
What is the linux
What is the linuxWhat is the linux
What is the linux
 
He 12 different types of servers that every techie should know about
He 12 different types of servers that every techie should know aboutHe 12 different types of servers that every techie should know about
He 12 different types of servers that every techie should know about
 
Bachelor of computer application b.c.a.-2014
Bachelor of computer application b.c.a.-2014Bachelor of computer application b.c.a.-2014
Bachelor of computer application b.c.a.-2014
 
Cloud computing application
Cloud computing applicationCloud computing application
Cloud computing application
 
Fast track to linux
Fast track to linuxFast track to linux
Fast track to linux
 
A sorted linear array
A sorted linear array A sorted linear array
A sorted linear array
 
String in c
String in cString in c
String in c
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

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