SlideShare a Scribd company logo
1 of 19
Operators And Expression
Bit wise Operators
Bit wise Operators
 One of C’s powerful features is a set of bit manipulation operators.
 These permit the programmer to access and manipulate individual bits within
a piece of data to perform bit operations.
 Decimal values are converted into binary values which are the sequence of
bits and bit wise operators work on these bits.
 These operators can operate upon ints and chars but not on floats and
doubles.
now delve inside the byte and see how it is constructed and how it
can be manipulated effectively. So let us take apart the byte... bit
by bit.
Bitwise Operators
One of C’s powerful features is a set of bit manipulation operators.
These permit the programmer to access and manipulate individual
bits within a piece of data. The various Bitwise Operators available
in C are shown in Figure 14.1.
Operator Meaning
~ One’s complement
>> Right shift
<< Left shift
& Bitwise AND
| Bitwise OR
^ Bitwise XOR(Exclusive OR)
 Let us first take a look at the bit numbering scheme in
integers and characters. Bits are numbered from zero
onwards, increasing from right to left as shown below:
Chapter 14: Operations On Bits 483
us first take a look at the bit numbering scheme in integers and
characters. Bits are numbered from zero onwards, increasing from
right to left as shown below:
7 6 5 4 3 2 1 0
012312 11 10 9 8 7 6 5 4
Character
16-bit Integer
15 14 13
Chapter 14: Operations On Bits 483
us first take a look at the bit numbering scheme in integers and
characters. Bits are numbered from zero onwards, increasing from
right to left as shown below:
7 6 5 4 3 2 1 0
012312 11 10 9 8 7 6 5 4
Character
16-bit Integer
15 14 13
Bit wise Operators
 One’s complement operator is represented by the symbol ~
 On taking one’s complement of a number, all 1’s present in
the number are changed to 0’s and all 0’s are changed to 1’s.
 For example one’s complement of 1010 is 0101. Similarly,
one’s complement of 1111 is 0000. Note that here when we
talk of a number we are talking of binary equivalent of the
number.
 Thus, one’s complement of 65 means one’s complement of
0000 0000 0100 0001, which is binary equivalent of 65.
One’s complement of 65 therefore would be, 1111 1111 1011
1110.
One’s Complement Operator (~)
 In real-world situations where could the one’s complement
operator be useful?
 Since it changes the original number beyond recognition,
one potential place where it can be effectively used is in
development of a file encryption utility
Use of one’s complement
operator
 The right shift operator is represented by >>.
 It needs two operands. It shifts each bit in its left operand to the right.
 The number of places the bits are shifted depends on the number
following the operator (i.e. its right operand).
 Thus, ch >> 3 would shift all bits in ch three places to the right.
Similarly, ch >> 5 would shift all bits 5 places to the right.
 For example, if the variable ch contains the bit pattern 11010111, then,
ch >> 1 would give 01101011 and ch >> 2 would give 00110101.
 Note that as the bits are shifted to the right, blanks are created on the
left. These blanks must be filled somehow. They are always filled with
zeros.
Right Shift Operator (>>)
 The left shift operator is represented by <<.
This is similar to the right shift operator, the only
difference being that the bits are shifted to the left,
and for each bit shifted, a 0 is added to the right of the
number.
Left Shift Operator (<<)
This operator is represented as &.
Remember it is different than &&, the logical AND
operator.
The & operator operates on two operands. While
operating upon these two operands they are compared
on a bit-by-bit basis. Hence both the operands must be
of the same type (either char or int).
The second operand is often called an AND mask. The
& operator operates on a pair of bits to yield a resultant
bit.
Bitwise AND Operator (&)
 The rules that decide the value of the resultant bit are
shown below:
mpared on a bit-by-bit basis. Hence both the operands mus
the same type (either char or int). The second operand is o
lled an AND mask. The & operator operates on a pair of bit
eld a resultant bit. The rules that decide the value of the resul
are shown below:
First bit Second bit First bit & Second bit
0 0 0
0 1 0
1 0 0
1 1 1
Bitwise AND Operator (&)
 The example given below shows more clearly what happens while
ANDing one operand with another.
1 0 1
Figure 14.8
The example given below shows more clearly what happens while
ANDing one operand with another. The rules given in the Figure
14.8 are applied to each pair of bits one by one.
7
this result
With this operand
yields
This operand when
ANDed bitwise
1 0237 6 5 4
1 0000001
1 0236 5 4
1 1000011
2 1 07 6 5 4
1 001
3
0101
Bitwise AND Operator (&)
 Probably, the best use of the AND operator is to check
whether a particular bit of an operand is ON or OFF. This is
explained in the following example.
 Suppose, from the bit pattern 10101101 of an operand, we
want to check whether bit number 3 is ON (1) or OFF (0).
completely independent of the operation performed on t
pairs.
Probably, the best use of the AND operator is to check w
particular bit of an operand is ON or OFF. This is explain
following example.
Suppose, from the bit pattern 10101101 of an operand, we
check whether bit number 3 is ON (1) or OFF (0). Since
to check the bit number 3, the second operand for th
operation should be 1 * 23
, which is equal to 8. This ope
be represented bitwise as 00001000.
Then the ANDing operation would be,
10101101 Original bit pattern
00001000 AND mask
--------------
00001000 Resulting bit pattern
Bitwise AND Operator (&)
 In every file entry present in the directory, there is an attribute byte. The
status of a file is governed by the value of individual bits in this attribute
byte. The AND operator can be used to check the status of the bits of this
attribute byte. The meaning of each bit in the attribute byte is shown in
Figure
Chapter 14: Operations On Bits 497
Bit numbers
7 6 5 4 3 2 1 0
Meaning
. . . . . . . 1 Read only
. . . . . . 1 . Hidden
. . . . . 1 . . System
. . . . 1 . . . Volume label entry
. . . 1 . . . . Sub-directory entry
. . 1 . . . . . Archive bit
. 1 . . . . . . Unused
1 . . . . . . . Unused
Bitwise AND Operator (&)
 Now, suppose we want to check whether a file is a hidden file or not. A
hidden file is one, which is never shown in the directory, even though it
exists on the disk.
 From the above bit classification of attribute byte, we only need to
check whether bit number 1 is ON or OFF.
 So, our first operand in this case becomes the attribute byte of the file
in question, whereas the second operand is the 1 * 21 = 2.
 Similarly, it can be checked whether the file is a system file or not,
whether the file is read-only file or not, and so on.
 The second important use of the AND operator is in changing the
status of the bit, or more precisely to switch OFF a particular bit.
Bitwise AND Operator (&)
Let’s summarize the uses of bitwise AND
operator:
①It is used to check whether a particular bit in a number
is ON or OFF.
②It is used to turn OFF a particular bit in a number.
Bit wise AND operator
 The XOR operator is represented as ^ and is also called an Exclusive
OR Operator.
 The OR operator returns 1, when any one of the two bits or both the bits
are 1, whereas XOR returns 1 only if one of the two bits is 1.
 XOR operator is used to toggle a bit ON or OFF.
 The truth table for the XOR operator is given below.
Bitwise XOR Operator (^)
Truth table for bit
wise operations
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
Bit wise Operators
Example program for bit
wise operators in C
Output:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
Summary Bit wise Operators
① To help manipulate hardware oriented data—individual bits rather than
bytes a set of bitwise operators are used.
② The bitwise operators include operators like one’s complement, right-
shift, left-shift, bitwise AND, OR, and XOR.
③ The one’s complement converts all zeros in its operand to 1s and all 1s
to 0s.
④ The right-shift and left-shift operators are useful in eliminating bits from a
number—either from the left or from the right.
⑤ The bitwise AND operators is useful in testing whether a bit is on/off and
in putting off a particular bit.
⑥ The bitwise OR operator is used to turn on a particular bit.
⑦ The XOR operator works almost same as the OR operator except one
minor variation.

More Related Content

What's hot

REAL TIME OPERATING SYSTEM
REAL TIME OPERATING SYSTEMREAL TIME OPERATING SYSTEM
REAL TIME OPERATING SYSTEMprakrutijsh
 
Computer architecture
Computer architectureComputer architecture
Computer architectureRishabha Garg
 
Introduction to Operating System
Introduction to Operating SystemIntroduction to Operating System
Introduction to Operating Systempriya_sinha02
 
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERSVTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERSvtunotesbysree
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationBadrul Alam
 
Types of operating system
Types of operating systemTypes of operating system
Types of operating systemAshikur Rahman
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set ArchitectureDilum Bandara
 
Functioning of computer
Functioning of computerFunctioning of computer
Functioning of computerSunipa Bera
 
VIM for (PHP) Programmers
VIM for (PHP) ProgrammersVIM for (PHP) Programmers
VIM for (PHP) ProgrammersZendCon
 
Unit 4 memory system
Unit 4   memory systemUnit 4   memory system
Unit 4 memory systemchidabdu
 

What's hot (20)

Operators in java
Operators in javaOperators in java
Operators in java
 
REAL TIME OPERATING SYSTEM
REAL TIME OPERATING SYSTEMREAL TIME OPERATING SYSTEM
REAL TIME OPERATING SYSTEM
 
C program
C programC program
C program
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
 
Manipulators
ManipulatorsManipulators
Manipulators
 
CPU
CPUCPU
CPU
 
Introduction to Operating System
Introduction to Operating SystemIntroduction to Operating System
Introduction to Operating System
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERSVTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
 
Types of computer
Types of computerTypes of computer
Types of computer
 
Linux - Introductions to Linux Operating System
Linux - Introductions to Linux Operating SystemLinux - Introductions to Linux Operating System
Linux - Introductions to Linux Operating System
 
Batch operating system
Batch operating system Batch operating system
Batch operating system
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Types of operating system
Types of operating systemTypes of operating system
Types of operating system
 
Operators in c++
Operators in c++Operators in c++
Operators in c++
 
Cgroups in android
Cgroups in androidCgroups in android
Cgroups in android
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set Architecture
 
Functioning of computer
Functioning of computerFunctioning of computer
Functioning of computer
 
VIM for (PHP) Programmers
VIM for (PHP) ProgrammersVIM for (PHP) Programmers
VIM for (PHP) Programmers
 
Unit 4 memory system
Unit 4   memory systemUnit 4   memory system
Unit 4 memory system
 

Similar to Cse lecture-4.2-c bit wise operators and expression

C bitwise operators
C bitwise operatorsC bitwise operators
C bitwise operatorsSuneel Dogra
 
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).pptLearnWithJCM
 
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.pptRithwikRanjan
 
data type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de ladata type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de laLEOFAKE
 
09. session 9 operators and statements
09. session 9   operators and statements09. session 9   operators and statements
09. session 9 operators and statementsPhúc Đỗ
 
Lecture 11 bitwise_operator
Lecture 11 bitwise_operatorLecture 11 bitwise_operator
Lecture 11 bitwise_operatoreShikshak
 
PE1 Module 2.ppt
PE1 Module 2.pptPE1 Module 2.ppt
PE1 Module 2.pptbalewayalew
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till nowAmAn Singh
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till nowAmAn Singh
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3Binay Kumar Ray
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of CSuchit Patel
 

Similar to Cse lecture-4.2-c bit wise operators and expression (20)

C bitwise operators
C bitwise operatorsC bitwise operators
C bitwise operators
 
Report on c
Report on cReport on c
Report on c
 
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
 
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
 
Sc
ScSc
Sc
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
data type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de ladata type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de la
 
09. session 9 operators and statements
09. session 9   operators and statements09. session 9   operators and statements
09. session 9 operators and statements
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
 
Lecture 11 bitwise_operator
Lecture 11 bitwise_operatorLecture 11 bitwise_operator
Lecture 11 bitwise_operator
 
PE1 Module 2.ppt
PE1 Module 2.pptPE1 Module 2.ppt
PE1 Module 2.ppt
 
C language
C languageC language
C language
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
 
Microoperations
MicrooperationsMicrooperations
Microoperations
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3
 
Java - Operators
Java - OperatorsJava - Operators
Java - Operators
 
Java 2
Java 2Java 2
Java 2
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
 

Recently uploaded

Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257subhasishdas79
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxhublikarsn
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessorAshwiniTodkar4
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelDrAjayKumarYadav4
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdfKamal Acharya
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptAfnanAhmad53
 

Recently uploaded (20)

Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 

Cse lecture-4.2-c bit wise operators and expression

  • 2. Bit wise Operators  One of C’s powerful features is a set of bit manipulation operators.  These permit the programmer to access and manipulate individual bits within a piece of data to perform bit operations.  Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.  These operators can operate upon ints and chars but not on floats and doubles. now delve inside the byte and see how it is constructed and how it can be manipulated effectively. So let us take apart the byte... bit by bit. Bitwise Operators One of C’s powerful features is a set of bit manipulation operators. These permit the programmer to access and manipulate individual bits within a piece of data. The various Bitwise Operators available in C are shown in Figure 14.1. Operator Meaning ~ One’s complement >> Right shift << Left shift & Bitwise AND | Bitwise OR ^ Bitwise XOR(Exclusive OR)
  • 3.  Let us first take a look at the bit numbering scheme in integers and characters. Bits are numbered from zero onwards, increasing from right to left as shown below: Chapter 14: Operations On Bits 483 us first take a look at the bit numbering scheme in integers and characters. Bits are numbered from zero onwards, increasing from right to left as shown below: 7 6 5 4 3 2 1 0 012312 11 10 9 8 7 6 5 4 Character 16-bit Integer 15 14 13 Chapter 14: Operations On Bits 483 us first take a look at the bit numbering scheme in integers and characters. Bits are numbered from zero onwards, increasing from right to left as shown below: 7 6 5 4 3 2 1 0 012312 11 10 9 8 7 6 5 4 Character 16-bit Integer 15 14 13 Bit wise Operators
  • 4.  One’s complement operator is represented by the symbol ~  On taking one’s complement of a number, all 1’s present in the number are changed to 0’s and all 0’s are changed to 1’s.  For example one’s complement of 1010 is 0101. Similarly, one’s complement of 1111 is 0000. Note that here when we talk of a number we are talking of binary equivalent of the number.  Thus, one’s complement of 65 means one’s complement of 0000 0000 0100 0001, which is binary equivalent of 65. One’s complement of 65 therefore would be, 1111 1111 1011 1110. One’s Complement Operator (~)
  • 5.  In real-world situations where could the one’s complement operator be useful?  Since it changes the original number beyond recognition, one potential place where it can be effectively used is in development of a file encryption utility Use of one’s complement operator
  • 6.  The right shift operator is represented by >>.  It needs two operands. It shifts each bit in its left operand to the right.  The number of places the bits are shifted depends on the number following the operator (i.e. its right operand).  Thus, ch >> 3 would shift all bits in ch three places to the right. Similarly, ch >> 5 would shift all bits 5 places to the right.  For example, if the variable ch contains the bit pattern 11010111, then, ch >> 1 would give 01101011 and ch >> 2 would give 00110101.  Note that as the bits are shifted to the right, blanks are created on the left. These blanks must be filled somehow. They are always filled with zeros. Right Shift Operator (>>)
  • 7.  The left shift operator is represented by <<. This is similar to the right shift operator, the only difference being that the bits are shifted to the left, and for each bit shifted, a 0 is added to the right of the number. Left Shift Operator (<<)
  • 8. This operator is represented as &. Remember it is different than &&, the logical AND operator. The & operator operates on two operands. While operating upon these two operands they are compared on a bit-by-bit basis. Hence both the operands must be of the same type (either char or int). The second operand is often called an AND mask. The & operator operates on a pair of bits to yield a resultant bit. Bitwise AND Operator (&)
  • 9.  The rules that decide the value of the resultant bit are shown below: mpared on a bit-by-bit basis. Hence both the operands mus the same type (either char or int). The second operand is o lled an AND mask. The & operator operates on a pair of bit eld a resultant bit. The rules that decide the value of the resul are shown below: First bit Second bit First bit & Second bit 0 0 0 0 1 0 1 0 0 1 1 1 Bitwise AND Operator (&)
  • 10.  The example given below shows more clearly what happens while ANDing one operand with another. 1 0 1 Figure 14.8 The example given below shows more clearly what happens while ANDing one operand with another. The rules given in the Figure 14.8 are applied to each pair of bits one by one. 7 this result With this operand yields This operand when ANDed bitwise 1 0237 6 5 4 1 0000001 1 0236 5 4 1 1000011 2 1 07 6 5 4 1 001 3 0101 Bitwise AND Operator (&)
  • 11.  Probably, the best use of the AND operator is to check whether a particular bit of an operand is ON or OFF. This is explained in the following example.  Suppose, from the bit pattern 10101101 of an operand, we want to check whether bit number 3 is ON (1) or OFF (0). completely independent of the operation performed on t pairs. Probably, the best use of the AND operator is to check w particular bit of an operand is ON or OFF. This is explain following example. Suppose, from the bit pattern 10101101 of an operand, we check whether bit number 3 is ON (1) or OFF (0). Since to check the bit number 3, the second operand for th operation should be 1 * 23 , which is equal to 8. This ope be represented bitwise as 00001000. Then the ANDing operation would be, 10101101 Original bit pattern 00001000 AND mask -------------- 00001000 Resulting bit pattern Bitwise AND Operator (&)
  • 12.  In every file entry present in the directory, there is an attribute byte. The status of a file is governed by the value of individual bits in this attribute byte. The AND operator can be used to check the status of the bits of this attribute byte. The meaning of each bit in the attribute byte is shown in Figure Chapter 14: Operations On Bits 497 Bit numbers 7 6 5 4 3 2 1 0 Meaning . . . . . . . 1 Read only . . . . . . 1 . Hidden . . . . . 1 . . System . . . . 1 . . . Volume label entry . . . 1 . . . . Sub-directory entry . . 1 . . . . . Archive bit . 1 . . . . . . Unused 1 . . . . . . . Unused Bitwise AND Operator (&)
  • 13.  Now, suppose we want to check whether a file is a hidden file or not. A hidden file is one, which is never shown in the directory, even though it exists on the disk.  From the above bit classification of attribute byte, we only need to check whether bit number 1 is ON or OFF.  So, our first operand in this case becomes the attribute byte of the file in question, whereas the second operand is the 1 * 21 = 2.  Similarly, it can be checked whether the file is a system file or not, whether the file is read-only file or not, and so on.  The second important use of the AND operator is in changing the status of the bit, or more precisely to switch OFF a particular bit. Bitwise AND Operator (&)
  • 14. Let’s summarize the uses of bitwise AND operator: ①It is used to check whether a particular bit in a number is ON or OFF. ②It is used to turn OFF a particular bit in a number. Bit wise AND operator
  • 15.  The XOR operator is represented as ^ and is also called an Exclusive OR Operator.  The OR operator returns 1, when any one of the two bits or both the bits are 1, whereas XOR returns 1 only if one of the two bits is 1.  XOR operator is used to toggle a bit ON or OFF.  The truth table for the XOR operator is given below. Bitwise XOR Operator (^)
  • 16. Truth table for bit wise operations
  • 17. Line 1 - Value of c is 12 Line 2 - Value of c is 61 Line 3 - Value of c is 49 Line 4 - Value of c is -61 Line 5 - Value of c is 240 Line 6 - Value of c is 15 Bit wise Operators
  • 18. Example program for bit wise operators in C Output: AND_opr value = 0 OR_opr value = 120 NOT_opr value = -41 XOR_opr value = 120 left_shift value = 80 right_shift value = 20
  • 19. Summary Bit wise Operators ① To help manipulate hardware oriented data—individual bits rather than bytes a set of bitwise operators are used. ② The bitwise operators include operators like one’s complement, right- shift, left-shift, bitwise AND, OR, and XOR. ③ The one’s complement converts all zeros in its operand to 1s and all 1s to 0s. ④ The right-shift and left-shift operators are useful in eliminating bits from a number—either from the left or from the right. ⑤ The bitwise AND operators is useful in testing whether a bit is on/off and in putting off a particular bit. ⑥ The bitwise OR operator is used to turn on a particular bit. ⑦ The XOR operator works almost same as the OR operator except one minor variation.