Professional Documents
Culture Documents
Muhammad Rafay-MCT-242L CP (Fall2022) - Lab 05 - Operators in C Language
Muhammad Rafay-MCT-242L CP (Fall2022) - Lab 05 - Operators in C Language
OBJECTIVE:
This lab will introduce different operators available in C Language. At the end of this lab, you should be
able to:
APPARATUS:
Laptop\PC with following tools installed
o Visual Studio Code with C/C++ and Code Runner Extensions
o C/C++ mingw-w64 tools for Windows 10
OPERATORS:1
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C
language is rich in built-in operators and provides the following types of operators:
Arithmetic Operators Logical Operators Assignment Operators
Relational Operators Bitwise Operators Miscellaneous Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language. Assume
variable A holds 10 and variable B holds 20 then:
1
https://www.tutorialspoint.com/cprogramming/c_operators.htm
#include <stdio.h>
int main()
{
int A = 17, B = 6;
printf("The value of A is %d\n", A);
printf("The value of B is %d\n\n", B);
printf("Value of A+B = %d+%d is %d\n", A, B, A+B );
printf("Value of A-B = %d-%d is %d\n", A, B, A-B );
printf("Value of A*B = %d*%d is %d\n", A, B, A*B );
printf("Value of A/B = %d/%d is %d\n", A, B, A/B );
printf("Value of A%B = %d%%%d is %d\n", A, B, A%B );
return 0;
}
// end of program
The value of A is 17
The value of B is 6
Relational Operators
The following table shows all the relational operators supported by the C language. Assume
variable A holds 10 and variable B holds 20 then:
The relational operators can be used to compare integers and floating-point numbers, with operands of
mixed type allowed. Thus, 1 < 2.5 has the value 1, while 4.3 > 7 has the value 0.
The precedence of the relational operators is lower than that of the arithmetic operators; for example, i+j<k-
1 means (i+j)<(k-1). The relational operators are left associative.
The expression i < j < k is legal in C but doesn’t have the meaning that you might expect. Since < operator is
left-associative, this expression is equivalent to (i<j)<k. In other words, the expression first tests whether i is
less than j; the 1 or 0 produced by this comparison in then compared to k. The expression does not test
whether j lies between i and k. For that we use i<j&&j<k.
#include <stdio.h>
int main()
{
int A = 10, B =20;
printf("The value of A is %d\n", A);
printf("The value of B is %d\n\n", B);
printf("Value of A==B = %d==%d is %d\n", A, B, A==B );
printf("Value of A!=B = %d!=%d is %d\n", A, B, A!=B );
printf("Value of A>B = %d>%d is %d\n", A, B, A>B );
printf("Value of A<B = %d<%d is %d\n", A, B, A<B );
printf("Value of A>=B = %d>=%d is %d\n", A, B, A>=B );
printf("Value of A<=B = %d<=%d is %d\n", A, B, A<=B );
return 0;
}
// end of program
The value of A is 10
The value of B is 20
Logical Operators
The following table shows all the logical operators supported by the C language. Assume variable A holds 1
and variable B holds 0 then:
#include <stdio.h>
int main()
{
int A = 10, B =0;
printf("The value of A is %d\n", A);
printf("The value of B is %d\n", B);
printf("Value of A&&B = %d&%d is %d\n", A, B, A&&B);
printf("Value of A||B = %d|%d is %d\n", A, B, A||B);
printf("Value of !A =!%d is %d\n", A, !A);
return 0;
}
// end of program
The value of A is 10
The value of B is 0
Program Output
Value of A&&B = 10&0 is 0
Value of A||B = 10|0 is 1
Value of !A =!10 is 0
Bitwise Operators
The byte is the lowest level at which we can access data; there is no “bit” type, and we can't ask for an
individual bit. In fact, we cannot even perform operations on a single bit -every bit wise operator will be
applied to, at a minimum, an entire byte at a time. This means we will be considering the whole
representation of a number whenever we talk about applying a bitwise operator. Table below summarizes
the bitwise operators available in C.
The following table lists the bitwise operators supported by C language. Assume variable 'A' holds 0xC3
(1100 0011) and variable 'B' holds 0x9F (1001 1111), then:
the number of bits specified by the right operand. i.e., 0000 1111
#include <stdio.h>
int main()
{
int A = 0xC3; // 1100 0011
int B = 0x9F; // 1001 1111
printf("The value of A is 0x%X (1100 0011)\n", A);
printf("The value of B is 0x%X (1001 1111)\n\n", B);
printf("Value of A&B = 0x%X & 0x%X is 0x%X\n", A, B, A&B );
printf("Value of A|B = 0x%X | 0x%X is 0x%X\n", A, B, A|B );
printf("Value of A^B = 0x%X ^ 0x%X is 0x%X\n", A, B, A^B );
printf("Value of ~A = ~0x%X is 0x%X\n", A, ~A );
printf("Value of A>>2 = 0x%X>>2 is 0x%X\n", A, A>>2 );
printf("Value of A<<2 = 0x%X<<2 is 0x%X\n", A, A<<2 );
return 0;
}
// end of program
The value of A is 0xC3 (1100 0011)
The value of B is 0x9F (1001 1111)
Bit Masking
Bitwise operators treat every bit in a word as a Boolean (two-valued) variable, apply a column-wise Boolean
operator, and generate a result. Unlike binary math, there is no carry or borrow and every column is
independent.
Bit-masking is using the bits in one word to “mask off” or select part of the range of bits in another word,
using the bitwise Boolean AND operator. The 1 bit in the “mask” select which bits we want to keep in the
word, and zero bits in the mask turn all the other corresponding bits to zeros. In other words, the one bit are
the “holes” in the mask that let the corresponding bits in the other word flow through to the result.
Turning On Bits
The opposite of masking (turning off bits) is “ORing in” bits, where we use the bitwise Boolean OR to “turn
ON” one or more bits in a word. We select a value that, when ORed with some other value, “turn ON”
selected bits and leaves the other bits unchanged.
Toggling Bits
Sometimes it does not really matter what the value is, but it must be made the opposite of what it currently
is. This can be achieved using the XOR (Exclusive OR) operation. XOR returns 1 if and only if an odd
number of bits are 1. Therefore, if two corresponding bits are one, the result will be a 0, but if only one of
them is 1, the result will be one. Therefore, inversion of the value of bits is done by XORing them with a 1.
If the original bid was 1, it returns 1 XOR 1 = 0. If the original bit was 0 it returns 0 XOR 1 = 1. Also note
that XOR masking is bit-safe, meaning that it will not affect unmasked bit because Y XOR 0 = Y, just like
an OR.
#include<stdio.h>
int main (){
// Turning Off Bits
printf("Turning Off Bits: Bitwise AND Operator\n");
int byte = 0xC3; // 1100 0011 (single byte register value)
printf("Register Value (1100 0011): 0x%X\n", byte);
int bitMask = 0xFC; // 1111 1100 (bit mask for bit 0 and 1)
byte &= bitMask; // Bitwise AND and Assignment Operator
printf("Bit Mask for AND (1111 1100): 0x%X\n", bitMask);
printf("Bitwise AND Value(1100 0000): 0x%X\n\n", byte);
// Turning On Bits
printf("Turning On Bits: Bitwise OR Operator\n");
byte = 0xC3; // 1100 0011 (single byte register value)
printf("Register Value (1100 0011): 0x%X\n", byte);
bitMask = 0x0C; // 0000 1100 (bit mask for bit 2 and 3)
byte |= bitMask; // Bitwise OR and Assignment Operator
printf("Bit Mask for OR (0000 1100): 0x0%X\n", bitMask);
printf("Bitwise OR Value (1100 1111): 0x%X\n\n", byte);
// Toggling Bits
printf("Toggling of Bits: Bitwise XOR Operator\n");
byte = 0xC3; // 1100 0011 (single byte register value)
printf("Register Value (1100 0011): 0x%X\n", byte);
bitMask = 0x0F; // 0000 1111 (bit mask for bit 0 through 3)
byte ^= bitMask; // Bitwise XOR and Assignment Operator
printf("Bit Mask for XOR (0000 1111): 0x0%X\n", bitMask);
printf("Bitwise XOR Value(1100 1100): 0x%X\n\n", byte);
return 0;
}
// end of program
Turning Off Bits: Bitwise AND Operator
Register Value (1100 0011): 0xC3
Bit Mask for AND (1111 1100): 0xFC
Bitwise AND Value(1100 0000): 0xC0
Assignment Operators
The following table shows all the assignment operators supported by the C language.
#include <stdio.h>
int main()
{
int A = 15, B =10, C = 2;
printf("The value of A is %d\n", A);
printf("The value of B is %d\n", B);
printf("The value of C is %d\n\n", C);
printf("Value of C = A+B => C = %d+%d is %d\n", A, B, C=A+B);
printf("Value of C += A => C += %d is %d\n", A, C+=A);
printf("Value of C -= B => C -= %d is %d\n", B, C-=B);
printf("Value of C *= A => C *= %d is %d\n", A, C*=A);
printf("Value of C /= B => C /= %d is %d\n", B, C/=B);
printf("Value of C %= B => C %= %d is %d\n", B, C%=B);
printf("Value of C <<= 3 is 0x%X\n", C<<=3);
printf("Value of C >>= 2 is 0x%X\n", C>>=2);
printf("Value of C &= 0x6 is 0x%X\n", C&=0x6);
printf("Value of C |= 0xC is 0x%X\n", C|=0xC);
printf("Value of C ^= 0xF is 0x%X\n", C^=0xF);
return 0;
}
// end of program
The value of A is 15
The value of B is 10
The value of C is 2
Miscellaneous Operators
The following table shows the miscellaneous operators supported by the C language.
#include <stdio.h>
int main()
{
int A = 10, B =20;
printf("The value of A is %d\n", A);
printf("The value of B is %d\n\n", B);
printf("Value of sizeof(A) is %d (in bytes)\n", sizeof(A));
printf("Value of &A is 0x%X (32-bit memory location)\n", &A);
printf("Value of A>B?1:0 is %d\n", A>B);
printf("Value of A<B?1:0 is %d\n", A<B);
return 0;
}
// end of program
The value of A is 10
The value of B is 20
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an expression is
evaluated. Certain operators have higher precedence than others; for example, the multiplication operator
has a higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not
20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into
7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at
the bottom. Within an expression, higher precedence operators will be evaluated first.
Precedence Category Description Associativity
1 () [] . -> left-to-right
2 ++ -- ! ~ (type) * & sizeof Unary Operator right-to-left
3 */% Arithmetic Operator left-to-right
4 +- Arithmetic Operator left-to-right
5 << >> Shift Operator left-to-right
6 < <= > >= Relational Operator left-to-right
7 == != Relational Operator left-to-right
8 & Bitwise AND Operator left-to-right
9 ^ Bitwise XOR Operator left-to-right
10 | Bitwise OR Operator left-to-right
11 && Logical AND Operator left-to-right
int i = 7, j = 8, k = 9; Output: 1
2
printf("%d", (i + 10) % k / j);
Justification:
Again brackets are supposed to be solved first, then since both / and % have same order of precedence, the
operators are carried in order of left to right.
int i = 7, j = 8; Output: 63 8
3 i *= j + 1;
printf("%d %d", i, j);
Justification:
First of all Arithmetic operator is solved and then Assignment operator. As assignment operator is near the last
precedence so it will be solved later.
int i = 1, j = 1, k = 1; Output: 3 2 1
4 i += j += k;
printf("%d %d %d", i, j, k);
Justification:
As only assignment operator is used and they have associativity of right to left. So rightward operator is
performed first and then moving to left.
int i = 5; Output: 7 13 19
int j = i++ * 3 - 2;
6
int k = ++i * 3 - 2;
printf("%d %d %d", i, j, k);
Justification:
The value of i in the start is 5 when j is being calculated and after that i is incremented and become 6, but for k
(i=7) i is incremented once again before k being calculated.
So final values become i=7, j=13 and k=19