Bitwise Operators
Bitwise operators in C are used to perform operations on individual bits of integer operands. They allow you to manipulate and test the bits of an integer value. C provides six bitwise operators: bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>).
-
The bitwise AND operator (&) performs a bitwise AND operation on each corresponding bit of two operands. The result is 1 if both bits are 1; otherwise, it is 0.
-
The bitwise OR operator (|) performs a bitwise OR operation on each corresponding bit of two operands. The result is 1 if at least one of the bits is 1; otherwise, it is 0.
-
The bitwise XOR operator (^) performs a bitwise XOR (exclusive OR) operation on each corresponding bit of two operands. The result is 1 if the bits are different; otherwise, it is 0.
-
The bitwise NOT operator (~) is a unary operator that inverts all the bits of an operand. It converts each 0 to 1 and each 1 to 0.
-
The left shift operator (<<) shifts the bits of the first operand to the left by the number of positions specified by the second operand. The vacated bits on the right are filled with 0s.
- The right shift operator (>>) shifts the bits of the first operand to the right by the number of positions specified by the second operand. The behavior of the right shift operator depends on the sign of the first operand.
Example
C
#include <stdio.h>
int main() {
unsigned int a = 12; // binary: 1100
unsigned int b = 10; // binary: 1010
unsigned int result;
// Bitwise AND
result = a & b; // result = 8 (binary: 1000)
printf("Bitwise AND of %u and %u gives %u\n", a, b, result);
// Bitwise OR
result = a | b; // result = 14 (binary: 1110)
printf("Bitwise OR of %u and %u gives %u\n", a, b, result);
// Bitwise XOR
result = a ^ b; // result = 6 (binary: 0110)
printf("Bitwise XOR of %u and %u gives %u\n", a, b, result);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Bitwise AND of 12 and 10 gives 8
Bitwise OR of 12 and 10 gives 14
Bitwise XOR of 12 and 10 gives 6
Note : Bitwise operators are commonly used in low-level programming, embedded systems, and optimization techniques. They allow you to perform operations at the bit level, manipulate individual bits, and perform tasks such as setting, clearing, or toggling specific bits in an integer value.
Comma Operator
The comma operator (,) is a special operator in C that allows you to combine multiple expressions into a single expression. It evaluates each expression from left to right and returns the value of the last expression.
The comma operator has the lowest precedence among all the operators in C. It is often used to write compact code and perform multiple operations in a single line.
The syntax of the comma operator is
expression1, expression2, ..., expressionN
Here's how it works:
-
The expressions are evaluated from left to right.
-
The value of each expression, except for the last one, is discarded.
- The value of the last expression is returned as the result of the entire comma-separated expression.
Example
int a = 10, b = 20, c;
c = (a++, b++, a + b);
printf("a = %d, b = %d, c = %d\n", a, b, c);
In this example, the comma operator is used to combine multiple expressions in the assignment statement for variable c. The expressions a++, b++, and a + b are evaluated from left to right. The value of a++ and b++ are discarded, and the value of a + b is assigned to c.
After the assignment, the values of a, b, and c are printed using printf(). The output will be:
a = 11, b = 21, c = 32
The comma operator is commonly used in for loops to perform multiple initializations or updates in a single statement. For example:
for (int i = 0, j = 0; i < 10; i++, j += 2) {
// Loop body
}
In this for loop, the comma operator is used to initialize both i and j in the initialization part and update both i and j in the update part of the loop.
The comma operator can also be used to write compact expressions and perform multiple operations in a single line, such as:
int result = (x = 5, y = 10, x + y);
Note : However, it's important to use the comma operator smartly or cautiously and ensure that the code remains readable and maintainable. Overusing the comma operator can make the code harder to understand and debug.
Frequently Asked Questions
Can the ternary operator be nested in C?
Yes, ternary operators can be nested. However, it's important to manage this carefully to keep the code readable. Nesting allows for more complex conditions to be evaluated in a concise manner.
Do bitwise operators work on floating-point numbers?
No, bitwise operators only work on integer types. Applying them to floating-point numbers requires converting the floats to integers, as bitwise operations are meant for direct binary manipulation.
Is the comma operator commonly used in professional C programming?
The comma operator is less common than other operators but is useful in loops and macro definitions where multiple expressions need to be evaluated sequentially in a single line of code.
Conclusion
In this article, we have learned about three special operators in C: the ternary operator, bitwise operators, and the comma operator. We learned that ternary operator provides a concise way to write simple conditional statements, bitwise operators allow manipulation of individual bits, and the comma operator combines multiple expressions into a single expression. Knowledge of these special operators enhances your C programming skills and enables you to write more compact and efficient code. This will be very helpful when we will work on more complex and large piece of codes.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.