0% found this document useful (0 votes)
4 views5 pages

Lecture-6(Operators and Expressions)

The document explains assignment operators in C, detailing their functions such as +=, -=, *=, /=, and %= for performing arithmetic operations on variables. It also covers bitwise operators like &, |, ^, ~, <<, and >>, which operate on binary representations of integers. Additionally, it introduces miscellaneous operators including sizeof, pointer operators, and the conditional operator, highlighting the differences between & and &&.

Uploaded by

mfiuk9148
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Lecture-6(Operators and Expressions)

The document explains assignment operators in C, detailing their functions such as +=, -=, *=, /=, and %= for performing arithmetic operations on variables. It also covers bitwise operators like &, |, ^, ~, <<, and >>, which operate on binary representations of integers. Additionally, it introduces miscellaneous operators including sizeof, pointer operators, and the conditional operator, highlighting the differences between & and &&.

Uploaded by

mfiuk9148
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

4.

Assignment Operators
It is used to assign a particular value to a variable. = (Assignment)- Used to assign a value from
right side operand to left side operand.

1. += (Addition Assignment)- To store the sum of both the operands to the left side operand.
2. -= (Subtraction Assignment) – To store the difference of both the operands to the left side
operand.
3. *= (Multiplication Assignment) – To store the product of both the operands to the left side
operand.
4. /= (Division Assignment) – To store the division of both the operands to the left side
operand.
5. %= (Remainder Assignment) – To store the remainder of both the operands to the left side
operand.

Example of Assignment Operators in C


#include<stdio.h>
int main()
{
int number = 10, result;
result = number;
printf("result = %d \n", result);
result += number; //Same as result = result + a
printf("result = %d \n", result);
result -= number; //Same as result = result - a
printf("result = %d \n", result);
result *= number; //Same as result = result * a
printf("result = %d \n", result);
result /= number; //Same as result = result / a
printf("result = %d \n", result);
result %= number; //Same as result = result % a
printf("result = %d \n", result);
return 0;
}
Output
result = 10

result = 20

result = 10

result = 100
result = 10

result = 0

Table for Assignment Operators in C


Operan
Operator Operation Elucidation
d

Used to assign a value from right side operand to left side


= a, b a=b
operand

+= a, b a+=b a=a+b: The value of a+b is stored in a

-= a, b a-=b a=a-b: The value of a-b is stored in a

*= a, b a*=b a=a*b: The value of a*b is stored in a

/= a, b a/=b a=a/b: The value of a/b is stored in a

%= a, b a%=b a=a %b: The value of a%b is stored in a

5. Bitwise Operators
It is based on the principle of performing operations bit by bit which is based on boolean
algebra. It increases the processing speed and hence the efficiency of the program.

The Bitwise Operators in C/C++ Includes –


1. & (Bitwise AND) – Converts the value of both the operands into binary form and performs
AND operation bit by bit.
2. | (Bitwise OR) – Converts the value of both the operands into binary form and performs
OR operation bit by bit.
3. ^ (Bitwise exclusive OR) – Converts the value of both the operands into binary form and
performs EXCLUSIVE OR operation bit by bit.
4. ~ (One’s complement operator): Converts the operand into its complementary form.
5. << – Left shift
6. >> – Right shift

In order to clearly understand bitwise operators, let us see the truth table for various bitwise
operations and understand how it is associated with boolean algebra.
Since there are 2 variables, namely, a and b, there are 22 combinations for values a and b can
take simultaneously.
AND – Both the operands should have boolean value 1 for the result to be 1.

OR – At least one operand should have boolean value 1 for the result to be 1.

XOR (EXCLUSIVE OR) – Either the first operand should have boolean value 1 or the second
operand should have boolean value 1. Both cannot have the boolean value 1 simultaneously.

One Complement: bitwise and, or, xor and not truth table values as follows :

a b a&b a|b a^b ~a


0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
Example

Let us take an example each of performing bitwise AND, OR, EXCLUSIVE OR

Consider two operands, a and b with values:

a = 26 and b=14

Therefore, a & b is computed as follows:

1. Find the binary equivalent of a and b:


2. Perform boolean AND/OR/EXCLUSIVE OR operation bit by btw
3. Convert the answer into its corresponding decimal form.

Bitwise AND

a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a & b = 0 1 0 1 0 which is equal to 10

Bitwise OR

a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a | b = 1 1 1 1 0 which is equal to 30
Bitwise XOR

a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a | b = 1 0 1 0 0 which is equal to 20

Example of Bitwise Operators in C

#include <stdio.h>
int main()
{
int a = 26, b = 14;
printf(" Bitwise AND operation %d & %d : %d\n",a,b,a&b);
printf(" Bitwise OR operation %d | %d : %d\n",a,b,a|b);
printf(" Bitwise XOR operation %d ^ %d : %d\n",a,b,a^b);
return 0;
}
Output
Bitwise AND operation 26 & 14 : 10
Bitwise OR operation 26 | 14 : 30
Bitwise XOR operation 26 ^ 14 : 20

Table for Bitwise Operators in C

Operator Operand Operation Elucidation

Bitwise AND: Converts the value of


both the operands into binary form
& a, b (a&b)
and performs AND- operation bit by
bit

Bitwise OR:
Converts the value of both the
| a, b (a|b)
operands into binary form and
performs OR- operation bit by bit

^ a, b (a^b) Bitwise exclusive OR: Converts the


value of both the operands into
binary form and performs EXCLUSIVE
OR operation bit by bit

One’s complement operator:


~ a (~a) Converts the operand into its
complementary form

<< a a<< Left shift

>> a a>> Right shift

6. Miscellaneous Operators
Apart from the above-discussed operators, there are certain operators which fall under this
category which include sizeof and ternary (conditional) operators.

Here is a table which illustrates the use of these operators:

1. sizeof – It returns the memory occupied by the particular data type of the operand
2. & (Pointer) – It refers to the address (memory location) in which the operand is stored.
3. * (Pointer) – It is a pointer operator
4. ? (Condition) – It is an alternative for if-else condition

Difference between & and && operators

1. && is a boolean operator, while & is a bitwise operator.


2. & is an and operation on two integers. Example: 1100 & 1001 = 1101, so 12 & 9 = 13.
3. && only checks if both (left and right) values are TRUE (i.e. non-zero).
4. 1 & 2 for example is 0, because a binary and of 1 and 2 is 0. Example: 01 & 10 = 00
5. While 1 && 2 is like TRUE && TRUE, which also equals true.

You might also like