C Operators
Dr.T.Abirami
Associate Professor
Department of Information Technology
Kongu Engineering College
C Operators
• An operator is simply a symbol that is used to perform operations.
types of operators to perform different types of operations in C language.
• Arithmetic Operators
• Relational Operators
• Shift Operators
• Logical Operators
• Bitwise Operators
• Ternary or Conditional Operators
• Assignment Operator
• Other Operators
Precedence of Operators in C
• The precedence of operator species that which operator will be
evaluated first and next.
• The associativity specifies the operator direction to be evaluated;
- it may be left to right or right to left.
Example:
int value=10+20*10;
• The value variable will contain 210 because * (multiplicative
operator) is evaluated before + (additive operator).
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right
C Arithmetic Operators
• An arithmetic operator performs mathematical operations such as
addition, subtraction, multiplication, division etc., on numerical
values (constants and variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
%
remainder after division
(modulo division)
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d n",c);
c = a-b;
printf("a-b = %d n",c);
c = a*b;
printf("a*b = %d n",c);
c = a/b;
printf("a/b = %d n",c);
c = a%b;
printf("Remainder when a divided by b = %d n",c);
return 0;
}
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
C Increment and Decrement Operators
• two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1.
• Increment ++ increases the value by 1
• whereas decrement -- decreases the value by 1.
• These two operators are unary operators, meaning they only operate on a
single operand.
++ and -- operator as prefix and postfix
• ++ operator as a prefix like: ++var, the value of var is incremented by
1; then it returns the value.
• ++ operator as a postfix like: var++, the original value of var is
returned first; then var is incremented by 1.
• The -- operator works in a similar way to the ++ operator except --
decreases the value by 1.
a = 5
++a; // a becomes 6
a++; // a becomes 7
--a; // a becomes 6
a--; // a becomes 5
#include <stdio.h>
int main() {
int var1 = 5, var2 = 5;
// 5 is displayed
// Then, var1 is increased to 6.
printf("%dn", var1++);
// var2 is increased to 6
// Then, it is displayed.
printf("%dn", ++var2);
return 0;
}
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d n", ++a);
printf("--b = %d n", --b);
printf("++c = %f n", ++c);
printf("--d = %f n", --d);
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
C Assignment Operators
• An assignment operator is used for assigning a value to a variable.
• The most common assignment operator is =
Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
// Working of assignment operators
#include <stdio.h>
int main()
{ int a = 5, c;
c = a; // c is 5
printf("c = %dn", c);
c += a; // c is 10
printf("c = %dn", c);
c -= a; // c is 5
printf("c = %dn", c);
c *= a; // c is 25
printf("c = %dn", c);
c /= a; // c is 5
printf("c = %dn", c);
c %= a; // c = 0
printf("c = %dn", c);
return 0;
}
Output
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
C Relational Operators
• A relational operator checks the relationship between two operands.
If the relation is true, it returns 1; if the relation is false, it returns
value 0.
• Relational operators are used in decision making and loops.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d n", a, b, a == b);
printf("%d == %d is %d n", a, c, a == c);
printf("%d > %d is %d n", a, b, a > b);
printf("%d > %d is %d n", a, c, a > c);
printf("%d < %d is %d n", a, b, a < b);
printf("%d < %d is %d n", a, c, a < c);
printf("%d != %d is %d n", a, b, a != b);
printf("%d != %d is %d n", a, c, a != c);
printf("%d >= %d is %d n", a, b, a >= b);
printf("%d >= %d is %d n", a, c, a >= c);
printf("%d <= %d is %d n", a, b, a <= b);
printf("%d <= %d is %d n", a, c, a <= c);
return 0;
}
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
• An expression containing logical operator returns either 0 or 1
depending upon whether expression results true or false.
• Logical operators are commonly used in decision making in C
programming.
Operator Meaning Example
&&
Logical AND. True only if
all operands are true
If c = 5 and d = 2 then,
expression ((c==5) &&
(d>5)) equals to 0.
||
Logical OR. True only if
either one operand is
true
If c = 5 and d = 2 then,
expression ((c==5) ||
(d>5)) equals to 1.
!
Logical NOT. True only if
the operand is 0
If c = 5 then, expression
!(c==5) equals to 0.
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d n", result);
result = !(a != b);
printf("!(a != b) is %d n", result);
result = !(a == b);
printf("!(a == b) is %d n", result);
return 0;
}
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program
(a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
(a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
(a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
(a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
!(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
!(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
C Bitwise Operators
• During computation, mathematical operations like: addition,
subtraction, multiplication, division, etc are converted to bit-level
which makes processing faster and saves power.
• Bitwise operators are used in C programming to perform bit-level
operations.
Bitwise Operators
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
truth table of the bitwise operators.
X Y X&Y X|Y X^Y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1
Bitwise AND operator
• It is denoted by the single ampersand sign (&).
• Two integer operands are written on both sides of the (&) operator. If the
corresponding bits of both the operands are 1, then the output of the bitwise AND
operation is 1; otherwise, the output would be 0.
For example,
We have two variables a and b.
a =6;
b=4;
The binary representation of the above two variables are given below:
a = 0110
b = 0100
When we apply the bitwise AND operation in the above two variables, i.e., a&
b, the output would be:
Result = 0100
#include <stdio.h>
int main()
{
int a=6, b=14; // variable declarations
printf("The output of the Bitwise AND operator a&b is %d",a&b);
return 0;
}
In the above code, we have created two variables, i.e., 'a' and 'b'. The values of 'a' and 'b' are 6 and 14 respectively. The
binary value of 'a' and 'b' are 0110 and 1110, respectively. When we apply the AND operator between these two variables,
a AND b = 0110 && 1110 = 0110
Bitwise OR operator
• The bitwise OR operator is represented by a single vertical sign (|).
• Two integer operands are written on both sides of the (|) symbol.
• If the bit value of any of the operand is 1, then the output would be 1,
otherwise 0.
We consider two variables,
a = 23;
b = 10;
The binary representation of the above two variables would be:
a = 0001 0111
b = 0000 1010
When we apply the bitwise OR operator in the above two variables, i.e., a|b , then the output would be:
Result = 0001 1111 As we can observe from the above result that the bits of both the operands are
compared one by one; if the value of either bit is 1, then the output would be 1
otherwise 0.
#include <stdio.h>
int main()
{
int a=23,b=10; // variable declarations
printf("The output of the Bitwise OR operator a|b is %d",a|b);
return 0;
}
Bitwise exclusive OR operator
• Bitwise exclusive OR operator is denoted by (^) symbol. Two operands are written on both sides of
the exclusive OR operator.
• If the corresponding bit of any of the operand is 1 then the output would be 1, otherwise 0.
For example,
We consider two variables a and b,
a = 12;
b = 10;
The binary representation of the above two variables would be:
a = 0000 1100
b = 0000 1010
When we apply the bitwise exclusive OR operator in the above two variables (a^b), then the
result would be:
Result = 0000 1110
Bitwise exclusive OR operator
• This operator is a binary operator, denoted by ‘^’. It returns bit by bit XOR of
input values, i.e, if corresponding bits are different, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise XOR Operation of 5 and 7
0101
^0111
________
0010 = 2 (In decimal)
#include <stdio.h>
int main()
{
int a=12,b=10; // variable declarations
printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b);
return 0;
}
Bitwise complement operator
• The bitwise complement operator is also known as one's complement
operator.
• It is represented by the symbol tilde (~).
• It takes only one operand or variable and performs complement
operation on an operand.
• When we apply the complement operation on any bits, then 0
becomes 1 and 1 becomes 0.
Bitwise complement operator
For example,
If we have a variable named 'a',
a = 8;
The binary representation of the above variable is given below:
a = 1000
When we apply the bitwise complement operator to the operand, then
the output would be:
Result = 0111
• This operator is a unary operator, denoted by ‘~’. It returns the one’s complement
representation of the input value, i.e, with all bits inverted, which means it makes
every 0 to 1, and every 1 to 0.
For example,
a = 5 = 0101 (In Binary)
• Bitwise Complement Operation of 5
~ 0101
____1___
0110 = 6 (In decimal)
• Note – Compiler will give 2’s complement of that number, i.e., 2’s complement of
10 will be -6.
• 2’s complement of a binary number is 1 added to the 1’s complement of
the binary number.
Examples:
Bitwise complement operator
~x = -x - 1
~ operators works like
negative integers called with
twos-complement
#include <stdio.h>
int main()
{
int a=8; // variable declarations
printf("The output of the Bitwise complement operator ~a is %d",~a);
return 0;
}
Bitwise shift operators
• Two types of bitwise shift operators exist in C programming.
• The bitwise shift operators will shift the bits either on the left-side or
right-side.
bitwise shift operator is divided into two categories:
• Left-shift operator
• Right-shift operator
Left-shift operator
• It is an operator that shifts the number of bits to the left-side.
Syntax of the left-shift operator
Operand << n (Ex: a<<2)
Where,
• Operand is an integer expression on which we apply the left-shift operation.
• n is the number of bits to be shifted.
• In the case of Left-shift operator, 'n' bits will be shifted on the left-side. The 'n'
bits on the left side will be popped out, and 'n' bits on the right-side are filled
with 0.
• Operand * 2n
• Let’s take a number 14.
• Binary representation of 14 is 00001110 (write it using 8 bit)
• 14 = (00001110) 2
• Then 14 << 1 will shift the binary sequence 1 position to the left side.
• Like,
If we shift 14 by 1 position
to the left, output will be
14 * 2 = 28.
If we shift 14 by 2 position
to the left, output will be
14 * 4 = 56.
14 << 1
= 14 * (21)
= 14 * 2
= 28.
For example,
Suppose we have a statement:
int a = 5;
The binary representation of 'a' is given below:
a = 0101
If we want to left-shift the above representation by 2, then the statement
would be:
a << 2;
0101<<2 = 00010100
#include <stdio.h>
int main()
{
int a=5; // variable initialization
printf("The value of a<<2 is : %d ", a<<2);
return 0;
}
Right-shift operator
• Bitwise Right shift operator >> is used to shift the binary sequence to right
side by specified position.
Example
• Let’s take a number 14.
• Binary representation of 14 is 00001110 (using 8 bit)
14 = (00001110) 2
• Then 14 >> 1 will shift the binary sequence by 1 position to the right side.
= 14 >> 1
= 14/ (21)
= 7
#include<stdio.h>
int main()
{
int var = 128;
printf("var/2 =%d n",var>>1); //1 position to right
printf("var/4 =%d n",var>>2); //2 position to right
printf("var/8 =%d n",var>>3); //3 position to right
printf("var/16 =%d n",var>>4); //4 position to right
printf("var/32 =%d n",var>>5); //5 position to right
return 0;
}
var/2 =64
var/4 =32
var/8 =16
var/16 =8
var/32 =4
#include <stdio.h>
int main()
{
int a=7; // variable initialization
printf("The value of a>>2 is : %d ", a>>2);
return 0;
}
Conditional Operator
• The conditional operator is also known as a ternary operator.
• The conditional statements are the decision-making statements which
depends upon the output of the expression.
• It is represented by two symbols, i.e., '?' and ':'.
• As conditional operator works on three operands, so it is also known
as the ternary operator.
Syntax of a conditional operator
(Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
example
age=19;
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));
#include <stdio.h>
int main()
{
int x=10,y=2;
printf( x > y ? "x is greatern" : "y is greater");
return 0;
}
x is greater
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %dn", x);
printf("y value is %d", y);
}
OUTPUT:
x value is 1
y value is 2
Other Operators
Comma Operator
• Comma operators are used to link related expressions together.
For example:
• int a, c = 5, d;
The sizeof operator
The sizeof is a unary operator that returns the size of data (constants, variables, array, structure,
etc).
Example
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytesn",sizeof(a));
printf("Size of float=%lu bytesn",sizeof(b));
printf("Size of double=%lu bytesn",sizeof(c));
printf("Size of char=%lu byten",sizeof(d));
return 0;
}
Other Operators
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Other operators
•reference operator &,
•dereference operator * and
•member selection operator ->

C operators

  • 1.
    C Operators Dr.T.Abirami Associate Professor Departmentof Information Technology Kongu Engineering College
  • 2.
    C Operators • Anoperator is simply a symbol that is used to perform operations. types of operators to perform different types of operations in C language. • Arithmetic Operators • Relational Operators • Shift Operators • Logical Operators • Bitwise Operators • Ternary or Conditional Operators • Assignment Operator • Other Operators
  • 3.
    Precedence of Operatorsin C • The precedence of operator species that which operator will be evaluated first and next. • The associativity specifies the operator direction to be evaluated; - it may be left to right or right to left. Example: int value=10+20*10; • The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).
  • 4.
    Category Operator Associativity Postfix() [] -> . ++ - - Left to right Unary + - ! ~ ++ - - (type)* & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift << >> Left to right Relational < <= > >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left Comma , Left to right
  • 5.
    C Arithmetic Operators •An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc., on numerical values (constants and variables). Operator Meaning of Operator + addition or unary plus - subtraction or unary minus * multiplication / division % remainder after division (modulo division)
  • 6.
    // Working ofarithmetic operators #include <stdio.h> int main() { int a = 9,b = 4, c; c = a+b; printf("a+b = %d n",c); c = a-b; printf("a-b = %d n",c); c = a*b; printf("a*b = %d n",c); c = a/b; printf("a/b = %d n",c); c = a%b; printf("Remainder when a divided by b = %d n",c); return 0; } Output a+b = 13 a-b = 5 a*b = 36 a/b = 2 Remainder when a divided by b=1
  • 7.
    C Increment andDecrement Operators • two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. • Increment ++ increases the value by 1 • whereas decrement -- decreases the value by 1. • These two operators are unary operators, meaning they only operate on a single operand.
  • 8.
    ++ and --operator as prefix and postfix • ++ operator as a prefix like: ++var, the value of var is incremented by 1; then it returns the value. • ++ operator as a postfix like: var++, the original value of var is returned first; then var is incremented by 1. • The -- operator works in a similar way to the ++ operator except -- decreases the value by 1. a = 5 ++a; // a becomes 6 a++; // a becomes 7 --a; // a becomes 6 a--; // a becomes 5
  • 9.
    #include <stdio.h> int main(){ int var1 = 5, var2 = 5; // 5 is displayed // Then, var1 is increased to 6. printf("%dn", var1++); // var2 is increased to 6 // Then, it is displayed. printf("%dn", ++var2); return 0; }
  • 10.
    // Working ofincrement and decrement operators #include <stdio.h> int main() { int a = 10, b = 100; float c = 10.5, d = 100.5; printf("++a = %d n", ++a); printf("--b = %d n", --b); printf("++c = %f n", ++c); printf("--d = %f n", --d); return 0; } Output ++a = 11 --b = 99 ++c = 11.500000 --d = 99.500000
  • 11.
    C Assignment Operators •An assignment operator is used for assigning a value to a variable. • The most common assignment operator is = Operator Example Same as = a = b a = b += a += b a = a+b -= a -= b a = a-b *= a *= b a = a*b /= a /= b a = a/b %= a %= b a = a%b
  • 12.
    // Working ofassignment operators #include <stdio.h> int main() { int a = 5, c; c = a; // c is 5 printf("c = %dn", c); c += a; // c is 10 printf("c = %dn", c); c -= a; // c is 5 printf("c = %dn", c); c *= a; // c is 25 printf("c = %dn", c); c /= a; // c is 5 printf("c = %dn", c); c %= a; // c = 0 printf("c = %dn", c); return 0; } Output c = 5 c = 10 c = 5 c = 25 c = 5 c = 0
  • 13.
    C Relational Operators •A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. • Relational operators are used in decision making and loops. Operator Meaning of Operator Example == Equal to 5 == 3 is evaluated to 0 > Greater than 5 > 3 is evaluated to 1 < Less than 5 < 3 is evaluated to 0 != Not equal to 5 != 3 is evaluated to 1 >= Greater than or equal to 5 >= 3 is evaluated to 1 <= Less than or equal to 5 <= 3 is evaluated to 0
  • 14.
    // Working ofrelational operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10; printf("%d == %d is %d n", a, b, a == b); printf("%d == %d is %d n", a, c, a == c); printf("%d > %d is %d n", a, b, a > b); printf("%d > %d is %d n", a, c, a > c); printf("%d < %d is %d n", a, b, a < b); printf("%d < %d is %d n", a, c, a < c); printf("%d != %d is %d n", a, b, a != b); printf("%d != %d is %d n", a, c, a != c); printf("%d >= %d is %d n", a, b, a >= b); printf("%d >= %d is %d n", a, c, a >= c); printf("%d <= %d is %d n", a, b, a <= b); printf("%d <= %d is %d n", a, c, a <= c); return 0; } Output 5 == 5 is 1 5 == 10 is 0 5 > 5 is 0 5 > 10 is 0 5 < 5 is 0 5 < 10 is 1 5 != 5 is 0 5 != 10 is 1 5 >= 5 is 1 5 >= 10 is 0 5 <= 5 is 1 5 <= 10 is 1
  • 15.
    C Logical Operators •An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. • Logical operators are commonly used in decision making in C programming. Operator Meaning Example && Logical AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0. || Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1. ! Logical NOT. True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0.
  • 16.
    // Working oflogical operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10, result; result = (a == b) && (c > b); printf("(a == b) && (c > b) is %d n", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d n", result); result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d n", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d n", result); result = !(a != b); printf("!(a != b) is %d n", result); result = !(a == b); printf("!(a == b) is %d n", result); return 0; } Output (a == b) && (c > b) is 1 (a == b) && (c < b) is 0 (a == b) || (c < b) is 1 (a != b) || (c < b) is 0 !(a != b) is 1 !(a == b) is 0
  • 17.
    Explanation of logicaloperator program (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true). (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false). (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true). (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false). !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true). !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
  • 18.
    C Bitwise Operators •During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power. • Bitwise operators are used in C programming to perform bit-level operations.
  • 19.
    Bitwise Operators Operators Meaningof operators & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR ~ Bitwise complement << Shift left >> Shift right
  • 20.
    truth table ofthe bitwise operators. X Y X&Y X|Y X^Y 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 1
  • 21.
    Bitwise AND operator •It is denoted by the single ampersand sign (&). • Two integer operands are written on both sides of the (&) operator. If the corresponding bits of both the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the output would be 0. For example, We have two variables a and b. a =6; b=4; The binary representation of the above two variables are given below: a = 0110 b = 0100 When we apply the bitwise AND operation in the above two variables, i.e., a& b, the output would be: Result = 0100
  • 22.
    #include <stdio.h> int main() { inta=6, b=14; // variable declarations printf("The output of the Bitwise AND operator a&b is %d",a&b); return 0; } In the above code, we have created two variables, i.e., 'a' and 'b'. The values of 'a' and 'b' are 6 and 14 respectively. The binary value of 'a' and 'b' are 0110 and 1110, respectively. When we apply the AND operator between these two variables, a AND b = 0110 && 1110 = 0110
  • 24.
    Bitwise OR operator •The bitwise OR operator is represented by a single vertical sign (|). • Two integer operands are written on both sides of the (|) symbol. • If the bit value of any of the operand is 1, then the output would be 1, otherwise 0. We consider two variables, a = 23; b = 10; The binary representation of the above two variables would be: a = 0001 0111 b = 0000 1010 When we apply the bitwise OR operator in the above two variables, i.e., a|b , then the output would be: Result = 0001 1111 As we can observe from the above result that the bits of both the operands are compared one by one; if the value of either bit is 1, then the output would be 1 otherwise 0.
  • 25.
    #include <stdio.h> int main() { inta=23,b=10; // variable declarations printf("The output of the Bitwise OR operator a|b is %d",a|b); return 0; }
  • 26.
    Bitwise exclusive ORoperator • Bitwise exclusive OR operator is denoted by (^) symbol. Two operands are written on both sides of the exclusive OR operator. • If the corresponding bit of any of the operand is 1 then the output would be 1, otherwise 0. For example, We consider two variables a and b, a = 12; b = 10; The binary representation of the above two variables would be: a = 0000 1100 b = 0000 1010 When we apply the bitwise exclusive OR operator in the above two variables (a^b), then the result would be: Result = 0000 1110
  • 27.
    Bitwise exclusive ORoperator • This operator is a binary operator, denoted by ‘^’. It returns bit by bit XOR of input values, i.e, if corresponding bits are different, it gives 1, else it gives 0. For example, a = 5 = 0101 (In Binary) b = 7 = 0111 (In Binary) Bitwise XOR Operation of 5 and 7 0101 ^0111 ________ 0010 = 2 (In decimal)
  • 28.
    #include <stdio.h> int main() { inta=12,b=10; // variable declarations printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b); return 0; }
  • 29.
    Bitwise complement operator •The bitwise complement operator is also known as one's complement operator. • It is represented by the symbol tilde (~). • It takes only one operand or variable and performs complement operation on an operand. • When we apply the complement operation on any bits, then 0 becomes 1 and 1 becomes 0.
  • 30.
    Bitwise complement operator Forexample, If we have a variable named 'a', a = 8; The binary representation of the above variable is given below: a = 1000 When we apply the bitwise complement operator to the operand, then the output would be: Result = 0111
  • 31.
    • This operatoris a unary operator, denoted by ‘~’. It returns the one’s complement representation of the input value, i.e, with all bits inverted, which means it makes every 0 to 1, and every 1 to 0. For example, a = 5 = 0101 (In Binary) • Bitwise Complement Operation of 5 ~ 0101 ____1___ 0110 = 6 (In decimal) • Note – Compiler will give 2’s complement of that number, i.e., 2’s complement of 10 will be -6. • 2’s complement of a binary number is 1 added to the 1’s complement of the binary number. Examples: Bitwise complement operator ~x = -x - 1 ~ operators works like negative integers called with twos-complement
  • 32.
    #include <stdio.h> int main() { inta=8; // variable declarations printf("The output of the Bitwise complement operator ~a is %d",~a); return 0; }
  • 33.
    Bitwise shift operators •Two types of bitwise shift operators exist in C programming. • The bitwise shift operators will shift the bits either on the left-side or right-side. bitwise shift operator is divided into two categories: • Left-shift operator • Right-shift operator
  • 34.
    Left-shift operator • Itis an operator that shifts the number of bits to the left-side. Syntax of the left-shift operator Operand << n (Ex: a<<2) Where, • Operand is an integer expression on which we apply the left-shift operation. • n is the number of bits to be shifted. • In the case of Left-shift operator, 'n' bits will be shifted on the left-side. The 'n' bits on the left side will be popped out, and 'n' bits on the right-side are filled with 0. • Operand * 2n
  • 35.
    • Let’s takea number 14. • Binary representation of 14 is 00001110 (write it using 8 bit) • 14 = (00001110) 2 • Then 14 << 1 will shift the binary sequence 1 position to the left side. • Like, If we shift 14 by 1 position to the left, output will be 14 * 2 = 28. If we shift 14 by 2 position to the left, output will be 14 * 4 = 56. 14 << 1 = 14 * (21) = 14 * 2 = 28.
  • 36.
    For example, Suppose wehave a statement: int a = 5; The binary representation of 'a' is given below: a = 0101 If we want to left-shift the above representation by 2, then the statement would be: a << 2; 0101<<2 = 00010100
  • 37.
    #include <stdio.h> int main() { inta=5; // variable initialization printf("The value of a<<2 is : %d ", a<<2); return 0; }
  • 38.
    Right-shift operator • BitwiseRight shift operator >> is used to shift the binary sequence to right side by specified position. Example • Let’s take a number 14. • Binary representation of 14 is 00001110 (using 8 bit) 14 = (00001110) 2 • Then 14 >> 1 will shift the binary sequence by 1 position to the right side. = 14 >> 1 = 14/ (21) = 7
  • 40.
    #include<stdio.h> int main() { int var= 128; printf("var/2 =%d n",var>>1); //1 position to right printf("var/4 =%d n",var>>2); //2 position to right printf("var/8 =%d n",var>>3); //3 position to right printf("var/16 =%d n",var>>4); //4 position to right printf("var/32 =%d n",var>>5); //5 position to right return 0; } var/2 =64 var/4 =32 var/8 =16 var/16 =8 var/32 =4
  • 41.
    #include <stdio.h> int main() { inta=7; // variable initialization printf("The value of a>>2 is : %d ", a>>2); return 0; }
  • 42.
    Conditional Operator • Theconditional operator is also known as a ternary operator. • The conditional statements are the decision-making statements which depends upon the output of the expression. • It is represented by two symbols, i.e., '?' and ':'. • As conditional operator works on three operands, so it is also known as the ternary operator. Syntax of a conditional operator (Condition? true_value: false_value); Example : (A > 100 ? 0 : 1);
  • 43.
    example age=19; (age>=18)? (printf("eligible forvoting")) : (printf("not eligible for voting"));
  • 44.
    #include <stdio.h> int main() { intx=10,y=2; printf( x > y ? "x is greatern" : "y is greater"); return 0; } x is greater
  • 45.
    #include <stdio.h> int main() { intx=1, y ; y = ( x ==1 ? 2 : 0 ) ; printf("x value is %dn", x); printf("y value is %d", y); } OUTPUT: x value is 1 y value is 2
  • 46.
    Other Operators Comma Operator •Comma operators are used to link related expressions together. For example: • int a, c = 5, d;
  • 47.
    The sizeof operator Thesizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc). Example #include <stdio.h> int main() { int a; float b; double c; char d; printf("Size of int=%lu bytesn",sizeof(a)); printf("Size of float=%lu bytesn",sizeof(b)); printf("Size of double=%lu bytesn",sizeof(c)); printf("Size of char=%lu byten",sizeof(d)); return 0; } Other Operators Output Size of int = 4 bytes Size of float = 4 bytes Size of double = 8 bytes Size of char = 1 byte
  • 48.
    Other operators •reference operator&, •dereference operator * and •member selection operator ->