0% found this document useful (0 votes)
41 views6 pages

The Bitwise Logical Operators.: Operator Description

Three bitwise logical operators - AND, OR, and XOR - can manipulate individual bits in an integer. AND sets a bit to 1 only if the bits in both operands are 1, OR sets a bit to 1 if the bits in either operand are 1, and XOR sets a bit to 1 if the bits in the operands are different. These operators allow clearing or setting specific bits by ANDing or ORing with a value with the desired bits set to 0 or 1.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views6 pages

The Bitwise Logical Operators.: Operator Description

Three bitwise logical operators - AND, OR, and XOR - can manipulate individual bits in an integer. AND sets a bit to 1 only if the bits in both operands are 1, OR sets a bit to 1 if the bits in either operand are 1, and XOR sets a bit to 1 if the bits in the operands are different. These operators allow clearing or setting specific bits by ANDing or ORing with a value with the desired bits set to 0 or 1.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Three bitwise logical operators are used to manipulate individual bits in an integer data type,

as shown in Table below. These operators have names similar to the TRUE/FALE logical
operators, but their operations di!!er.
The bitwise logical operators.
Operator Description
& AND
| Inclusive OR
^ Exclusive OR
These are all binary operators, setting bits in the result to " or # depending on the bits in the
operands. They operate as !ollows$
%itwise A&' sets a bit in the result to " only i! the corresponding bits in both
operands are "( otherwise, the bit is set to #. The A&' operator is used to turn o!!, or
clear, one or more bits in a value.
%itwise inclusive )R sets a bit in the result to # only i! the corresponding bits in both
operands are #( otherwise, the bit is set to ". The )R operator is used to turn on, or
set, one or more bits in a value.
%itwise e*clusive )R sets a bit in the result to " i! the corresponding bits in the
operands are di!!erent +i! one is " and the other is #,( otherwise, the bit is set to #.
The !ollowing are e*amples o! how these operators wor-$

Operation Example
AND 11110000
& 01010101
----------------
01010000
Inclusive OR 11110000
| 01010101
----------------
11110101
Exclusive OR 11110000
^ 01010101
----------------
10100101
.ou /ust read that bitwise A&' and bitwise inclusive )R can be used to clear or set,
respectively, speci!ied bits in an integer value. 0ere1s what that means. uppose you have a
type char variable, and you want to ensure that the bits in positions # and 2 are cleared +that
is, e3ual to #, and that the other bits stay at their original values. 4! you A&' the variable with
a second value that has the binary value """#"""#, you1ll obtain the desired result. 0ere1s
how this wor-s$
4n each position where the second value has a ", the result will have the same value, # or ",
as was present in that position in the original variable$
0 & 1 == 0
1 & 1 == 1
4n each position where the second value has a #, the result will have a # regardless o! the
value that was present in that position in the original variable$
0 & 0 == 0
1 & 0 == 0
etting bits with )R wor-s in a similar way. 4n each position where the second value has a
", the result will have a ", and in each position where the second value has a #, the result
will be unchanged$
0 | 1 == 1
1 | 1 == 1
0 | 0 == 0
1 | 0 == 1
C Operator Precedence and Associativity
C Programming (COP-2220)
This page lists C operators in order of precedence (highest to lowest). Their associativity
indicates in what order operators of equal precedence in an expression are applied.
Operator Description Associativity
()
[]
.
->
++ --
Parentheses (function call) (see Note 1)
Brackets (array suscript)
!e"er selection #ia o$ect na"e
!e"er selection #ia pointer
Postfix incre"ent%decre"ent (see Note &)
left'to'right
++ --
+ -
! ~
(type)
*
&
sizeof
Prefix incre"ent%decre"ent
(nary plus%"inus
)ogical negation%itwise co"ple"ent
Cast (change type)
*ereference
+ddress
*eter"ine si,e in ytes
right'to'left
* / %
!ultiplication%di#ision%"odulus left'to'right
+ -
+ddition%sutraction left'to'right
<< >>
Bitwise shift left- Bitwise shift right left'to'right
< <=
> >=
.elational less than%less than or equal to
.elational greater than%greater than or equal to
left'to'right
== !=
.elational is equal to%is not equal to left'to'right
&
Bitwise +N* left'to'right
^
Bitwise exclusi#e /. left'to'right
|
Bitwise inclusi#e /. left'to'right
&&
)ogical +N* left'to'right
||
)ogical /. left'to'right
?:
Ternary conditional right'to'left
=
+= -=
*= /=
%= &=
^= |=
<<= >>=
+ssign"ent
+ddition%sutraction assign"ent
!ultiplication%di#ision assign"ent
!odulus%itwise +N* assign"ent
Bitwise exclusi#e%inclusi#e /. assign"ent
Bitwise shift left%right assign"ent
right'to'left
,
Co""a (separate expressions)
left'to'right
Note 1:
Parentheses are also used to group expressions to force a different order of
e#aluation0 such parenthetical expressions can e nested and are e#aluated fro"
inner to outer
Note 2:
Postfix incre"ent%decre"ent ha#e high precedence- ut the actual incre"ent or
decre"ent of the operand is delayed (to e acco"plished so"eti"e efore the
state"ent co"pletes execution). 1o in the state"ent y = x * z++; the current
#alue of z is used to e#aluate the expression (i.e., z++ e#aluates to z) and z only
incre"ented after all else is done. 1ee postinc.c for another exa"ple.
/* posi!"." -- 0#0$$% (s&')
(e)o p*e"e'e!"e of pos-i!"*e)e!
*/
+i!",-'e <s'io..>
i! )/i!()
0
i! i[] = 0%, 123
i! *p = i3
i! & = --*p++3

p*i!f(4& = %'5!5!4, &)3
s6se)(4p/-se4)3
*e-*! 03
2
C Language Operator Precedence Chart
/perator precedence descries the order in which C reads expressions. 2or exa"ple-
the expression /=7+8*$ contains two operations- an addition and a "ultiplication.
*oes the C co"piler e#aluate 7+8 first- then "ultiply the result y $- or does it
e#aluate 8*$ first- then add 7 to the result3 The operator precedence chart contains
the answers. /perators higher in the chart ha#e a higher precedence- "eaning that
the C co"piler e#aluates the" first. /perators on the sa"e line in the chart ha#e the
sa"e precedence- and the 4+ssociati#ity4 colu"n on the right gi#es their e#aluation
order.
Operator Precedence Chart
Operator Type Operator Associativity
Pri"ary 5xpression
/perators
() [] . -> expr++ expr--
left'to'right
(nary /perators
* & + - ! ~ ++expr --expr (typecast)
sizeof()
right'to'left
Binary /perators
* / %
left'to'right
+ -
>> <<
< > <= >=
== !=
&
^
|
&&
||
Ternary /perator
?:
right'to'left
+ssign"ent /perators
= += -= *= /= %= >>= <<= &= ^= |=
right'to'left
Co""a
,
left'to'right

You might also like