C Programming Session 7
C Programming Session 7
The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else'
statement is also a decision-making statement.
o In the above syntax, the expression1 is a Boolean condition that can be either
true or false value.
o If the expression1 results into a true value, then the expression2 will execute.
o If the expression1 returns false value then the expression3 will execute.
In the above code, we are taking input as the 'age' of the user. After taking input, we
have applied the condition by using a conditional operator. In this condition, we are
checking the age of the user. If the age of the user is greater than or equal to 18, then
the statement1 will execute, i.e., (printf("eligible for voting")) otherwise, statement2
will execute, i.e., (printf("not eligible for voting")).
If we provide the age of user below 18, then the output would be:
If we provide the age of user above 18, then the output would be:
As we can observe from the above two outputs that if the condition is true, then the
statement1 is executed; otherwise, statement2 will be executed.
Till now, we have observed that how conditional operator checks the condition and
based on condition, it executes the statements. Now, we will see how a conditional
operator is used to assign the value to a variable.
1. #include <stdio.h>
2. int main()
3. {
4. int a=5,b; // variable declaration
5. b=((a==5)?(3):(2)); // conditional operator
6. printf("The value of 'b' variable is : %d",b);
7. return 0;
8. }
In the above code, we have declared two variables, i.e., 'a' and 'b', and assign 5 value
to the 'a' variable. After the declaration, we are assigning value to the 'b' variable by
using the conditional operator. If the value of 'a' is equal to 5 then 'b' is assigned with a
3 value otherwise 2.
Output
The above output shows that the value of 'b' variable is 3 because the value of 'a'
variable is equal to 5.
As we know that the behavior of conditional operator and 'if-else' is similar but they
have some differences. Let's look at their differences.
o A conditional operator can also be used for assigning a value to the variable,
whereas the 'if-else' statement cannot be used for the assignment purpose.
o It is not useful for executing the statements when the statements are multiple,
whereas the 'if-else' statement proves more suitable when executing multiple
statements.
o The nested ternary operator is more complex and cannot be easily debugged,
while the nested 'if-else' statement is easy to read and maintain.
Bitwise Operator in C
The bitwise operators are the operators used to perform the operations on the data at
the bit-level. When we perform the bitwise operations, then it is also known as bit-level
programming. It consists of two digits, either 0 or 1. It is mainly used in numerical
computations to make the calculations faster.
| Bitwise OR operator
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1
For example,
As we can observe from the above result that bits of both the variables are compared
one by one. If the bit of both the variables is 1 then the output would be 1, otherwise
0.
1. #include <stdio.h>
2. int main()
3. {
4. int a=6, b=14; // variable declarations
5. printf("The output of the Bitwise AND operator a&b is %d",a&b);
6. return 0;
7. }
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,
Output
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.
For example,
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.
1. #include <stdio.h>
2. int main()
3. {
4. int a=23,b=10; // variable declarations
5. printf("The output of the Bitwise OR operator a|b is %d",a|b);
6. return 0;
7. }
Output
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,
As we can observe from the above result that the bits of both the operands are
compared one by one; if the corresponding bit value of any of the operand is 1, then
the output would be 1 otherwise 0.
1. #include <stdio.h>
2. int main()
3. {
4. int a=12,b=10; // variable declarations
5. printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b);
6. return 0;
7. }
Output
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.
For example,
As we can observe from the above result that if the bit is 1, then it gets changed to 0
else 1.
1. #include <stdio.h>
2. int main()
3. {
4. int a=8; // variable declarations
5. printf("The output of the Bitwise complement operator ~a is %d",~a);
6. return 0;
7. }
Output
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. Therefore, we can say that the
bitwise shift operator is divided into two categories:
o Left-shift operator
o Right-shift operator
Left-shift operator
1. Operand << n
Where,
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.
For example,
1. #include <stdio.h>
2. int main()
3. {
4. int a=5; // variable initialization
5. printf("The value of a<<2 is : %d ", a<<2);
6. return 0;
7. }
Output
Right-shift operator
1. Operand >> n;
Where,
In the case of the right-shift operator, 'n' bits will be shifted on the right-side. The 'n'
bits on the right-side will be popped out, and 'n' bits on the left-side are filled with 0.
For example,
1. #include <stdio.h>
2. int main()
3. {
4. int a=7; // variable initialization
5. printf("The value of a>>2 is : %d ", a>>2);
6. return 0;
7. }
Output