SlideShare a Scribd company logo
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

Secondary storage management in os
Secondary storage management in osSecondary storage management in os
Secondary storage management in osSumant Diwakar
 
Context Switching
Context SwitchingContext Switching
Context Switchingfranksvalli
 
Operating systems Overview
Operating systems OverviewOperating systems Overview
Operating systems Overview
NAILBITER
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structures
Mukesh Chinta
 
Operating system overview concepts ppt
Operating system overview concepts pptOperating system overview concepts ppt
Operating system overview concepts ppt
RajendraPrasad Alladi
 
Fundamentals of C Programming Language
Fundamentals of C Programming LanguageFundamentals of C Programming Language
Fundamentals of C Programming Language
RamaBoya2
 
Pipelining
PipeliningPipelining
Pipelining
AJAL A J
 
Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9myrajendra
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
PreSolutions Softwares
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Computer Fundamental
Computer FundamentalComputer Fundamental
Computer Fundamental
FUNgaming7
 
Os solved question paper
Os solved question paperOs solved question paper
Os solved question paperAnkit Bhatnagar
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
Mukesh Chinta
 
Logical and shift micro operations
Logical and shift micro operationsLogical and shift micro operations
Logical and shift micro operations
Sanjeev Patel
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Operating System
Operating SystemOperating System
Operating System
Munazza-Mah-Jabeen
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in C
yndaravind
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
Huba Akhtar
 
introduction To Operating System
introduction To Operating Systemintroduction To Operating System
introduction To Operating SystemLuka M G
 

What's hot (20)

Secondary storage management in os
Secondary storage management in osSecondary storage management in os
Secondary storage management in os
 
Context Switching
Context SwitchingContext Switching
Context Switching
 
Operating systems Overview
Operating systems OverviewOperating systems Overview
Operating systems Overview
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structures
 
Operating system overview concepts ppt
Operating system overview concepts pptOperating system overview concepts ppt
Operating system overview concepts ppt
 
Fundamentals of C Programming Language
Fundamentals of C Programming LanguageFundamentals of C Programming Language
Fundamentals of C Programming Language
 
Pipelining
PipeliningPipelining
Pipelining
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Computer Fundamental
Computer FundamentalComputer Fundamental
Computer Fundamental
 
Os solved question paper
Os solved question paperOs solved question paper
Os solved question paper
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
 
Logical and shift micro operations
Logical and shift micro operationsLogical and shift micro operations
Logical and shift micro operations
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Operating System
Operating SystemOperating System
Operating System
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in C
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
introduction To Operating System
introduction To Operating Systemintroduction To Operating System
introduction To Operating 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).ppt
LearnWithJCM
 
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
RithwikRanjan
 
Sc
ScSc
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
Thesis Scientist Private Limited
 
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
LEOFAKE
 
09. session 9 operators and statements
09. session 9   operators and statements09. session 9   operators and statements
09. session 9 operators and statementsPhúc Đỗ
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
Asheesh kushwaha
 
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.ppt
balewayalew
 
C language
C languageC language
C language
Mohamed Bedair
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
Megha Sharma
 
Microoperations
MicrooperationsMicrooperations
Microoperations
Rakesh Pillai
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
AmAn Singh
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
AmAn Singh
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3
Binay Kumar Ray
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
Suchit 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

weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 

Recently uploaded (20)

weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 

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.