C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

3.4. Conditional, Bitwise and Special Operators


Conditional (Ternary) Operator (? :)

The operator?: is known as a conditional operator. Simple conditional operations can be carried out with conditional operators. An expression that makes use of the conditional operator is called a conditional expression. Such an expression can be written in place of a traditional if else statement. A conditional expression is written in the form (Syntax):

expression1? expression2: expression3

When evaluating a conditional expression, expression1 is evaluated first. If the result of expression1 is true, the value of expression2 is the value of conditional expression. If expression1 is false, the value of expression3 is the value of conditional expression. For example:

a= 10; b=15;
x = (a > b)? a: b ;
//In this example, x will be assigned the value of b.

Bitwise Operator

Bitwise operators in C are used for bitwise manipulation of integers at the binary level. These operators perform operations on individual bits of integers, allowing you to work with the binary representations of numbers. Following are the list of bitwise operators in c:

Bitwise AND (&):

Description: Performs a bitwise AND operation between corresponding bits of two integers. The result has a bit 1 only if the corresponding bits in both operands are 1 otherwise 0.

int result = 5 & 3;  // Result: 1 (binary: 101 & 011 = 001)

Bitwise OR (|):

Description: Performs a bitwise OR operation between corresponding bits of two integers. The result has a bit 1 if at least one of the corresponding bits in the operands is 1.

int result = 5 | 3;  // Result: 7 (binary: 101 | 011 = 111)

Bitwise XOR (^):

Description: Performs a bitwise XOR (exclusive OR) operation between corresponding bits of two integers. The result has a bit 1 if the corresponding bits in the operands are different.

int result = 5 ^ 3;  // Result: 6 (binary: 101 ^ 011 = 110)

Bitwise NOT (~):

Description: Inverts the bits of a single integer, turning each 0 bit to 1 and each 1 bit to 0.

int result = ~5;  // Result: -6 (binary: ~0000...0101 = 1111...1010)

Left Shift (<<):

Description: Shifts the bits of an integer to the left by a specified number of positions. Zeros are shifted in from the right.

int result = 5 << 2;  // Result: 20 (binary: 0000...0101 << 2 = 0000...010100)

Right Shift (>>):

Description: Shifts the bits of an integer to the right by a specified number of positions. The sign bit is shifted in from the left for signed integers, and zeros are shifted in for unsigned integers.

int result = 5 >> 1;  // Result: 2 (binary: 0000...0101 >> 1 = 0000...0010)

Special Operators


C supports some special operators such as comma operator, sizeof operator, pointer operator (* and &) and member selection operators.

Comma Operator (,):

Description: The comma operator is used to separate expressions within a statement. It evaluates each expression from left to right and returns the value of the rightmost expression.

Example:

#include <stdio.h>

int main() {
    int a = 5, b = 10, c;

    // Using the comma operator in a single statement
    c = (a++, b++, a + b);

    printf("a = %d, b = %d, c = %d\n", a, b, c);

    return 0;
}

 

In this example, the expressions a++, b++, and a + b are separated by commas. The result of the entire statement is the value of the rightmost expression (a + b), and the values of a and b are updated due to the post-increment operators.

sizeof Operator:

Description: The sizeof operator returns the size, in bytes, of a data type or an object. It is commonly used to determine the amount of memory occupied by a variable or a type.

Example:

#include <stdio.h>

int main() {
    int integerVar;
    double doubleVar;

    printf("Size of int: %lu bytes\n", sizeof(int));
    printf("Size of double: %lu bytes\n", sizeof(double));
    printf("Size of integerVar: %lu bytes\n", sizeof(integerVar));
    printf("Size of doubleVar: %lu bytes\n", sizeof(doubleVar));

    return 0;
}

In this example, the sizeof operator is used to determine the size of an int, a double, and the variables integerVar and doubleVar. The sizes are then printed using printf. The result may vary depending on the system architecture.