The logical operators and ternary operator (conditional operator) in C language are very common and fundamental operators, used for boolean logic operations and simplifying conditional expressions. By using these operators appropriately, the code can be made more concise and clear. This article will focus on logical operators, the ternary operator, and bitwise logic operations.
1. Logical Operators: Used for Boolean Value Judgement
Logical operators are mainly used for boolean value operations, returning a logical value (<span>0</span> or <span>1</span>), commonly used in conditional statements and control flow statements.
1.1 Logical AND <span>&&</span> (AND)
<span>&&</span> operator is used to determine if both conditions are true; the result is true (1) only when both conditions are true.
int a = 5, b = 10;
if (a > 0 && b > 0) {
printf("Both are positive.\n");
}
- • Usage:
<span>expr1 && expr2</span>, if<span>expr1</span>and<span>expr2</span>are both true (non-zero), it returns<span>true</span>(1), otherwise it returns<span>false</span>(0).
1.2 Logical OR <span>||</span> (OR)
<span>||</span> operator is used to determine if at least one of the conditions is true; the result is true (1) if at least one condition is true.
int a = -1, b = 10;
if (a > 0 || b > 0) {
printf("At least one is positive.\n");
}
- • Usage:
<span>expr1 || expr2</span>, if<span>expr1</span>or<span>expr2</span>is true (non-zero), it returns<span>true</span>(1), otherwise it returns<span>false</span>(0).
1.3 Logical NOT <span>!</span> (NOT)
<span>!</span> operator is used to invert the value of a condition; non-zero values become <span>0</span>, and <span>0</span> becomes <span>1</span>.
int a = 0;
if (!a) {
printf("a is false.\n");
}
- • Usage:
<span>!expr</span>, if<span>expr</span>is true (non-zero), it returns<span>false</span>(0); if<span>expr</span>is false (0), it returns<span>true</span>(1).
2. Ternary Operator: Concise Conditional Judgement
The ternary operator is a simplified way of conditional judgement, with the syntax:
condition ? expr1 : expr2;
If <span>condition</span> is true (non-zero), it returns <span>expr1</span>; otherwise, it returns <span>expr2</span>.
2.1 Basic Usage of Ternary Operator
int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("The larger number is %d\n", max);
In the above example, <span>a > b</span> is the conditional expression; if the condition is true, it returns <span>a</span>, otherwise it returns <span>b</span>. The result is <span>20</span>, because <span>b</span> is larger.
2.2 Nested Ternary Operators
The ternary operator can be nested to handle complex conditional judgements.
int a = 10, b = 20, c = 30;
int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("The largest number is %d\n", max);
Here, <span>max</span> will return the maximum value, as the ternary operator checks for the maximum among the three numbers through nesting.
2.3 Advantages and Disadvantages of Ternary Operator
| Advantages | Disadvantages |
| Concise and readable code | Poor readability, especially when nested |
| Can be used directly in expressions | Difficult to debug, especially with complex conditions |
- • The ternary operator is very effective when the conditional logic is simple; however, when the conditions are complex, the readability of the ternary operator decreases, and it is recommended to use standard
<span>if-else</span>statements.
3. Bitwise Logic Operators: Bit-Level Operations
Bitwise operators are used to operate on the binary bits of numbers, mainly used in low-level hardware programming, encryption, data compression, and other scenarios.
3.1 Bitwise AND <span>&</span>
The bitwise AND operator <span>&</span> compares the corresponding bits of two operands; the result bit is <span>1</span> only when both bits are <span>1</span>, otherwise it is <span>0</span>.
int a = 5, b = 3; // Binary: a=0101, b=0011
int result = a & b; // Result: 0001
printf("Result of a & b: %d\n", result); // Output 1
- • Usage:
<span>expr1 & expr2</span>, the result is 1 only when a certain bit of<span>expr1</span>and<span>expr2</span>is 1.
3.2 Bitwise OR <span>|</span>
The bitwise OR operator <span>|</span> compares the corresponding bits of two operands; the result bit is <span>0</span> only when both bits are <span>0</span>, otherwise it is <span>1</span>.
int a = 5, b = 3; // Binary: a=0101, b=0011
int result = a | b; // Result: 0111
printf("Result of a | b: %d\n", result); // Output 7
- • Usage:
<span>expr1 | expr2</span>, if any bit of<span>expr1</span>or<span>expr2</span>is 1, the result is 1.
3.3 Bitwise XOR <span>^</span>
The bitwise XOR operator <span>^</span> compares the corresponding bits of two operands; the result bit is <span>1</span> only when the two bits are different (one is 1, the other is 0), otherwise it is <span>0</span>.
int a = 5, b = 3; // Binary: a=0101, b=0011
int result = a ^ b; // Result: 0110
printf("Result of a ^ b: %d\n", result); // Output 6
- • Usage:
<span>expr1 ^ expr2</span>, when a certain bit of<span>expr1</span>and<span>expr2</span>is different, the result is 1.
3.4 Bitwise NOT <span>~</span>
The bitwise NOT operator <span>~</span> inverts all binary bits of a number, turning all <span>0</span> into <span>1</span> and all <span>1</span> into <span>0</span>.
int a = 5; // Binary: a=0101
int result = ~a; // Result: 1010
printf("Result of ~a: %d\n", result); // Output -6
- • Usage:
<span>~expr</span>, inverts each bit of<span>expr</span>.
3.5 Bitwise Shift Operations <span><<</span> and <span>>></span>
Bitwise left shift <span><<</span> and right shift <span>>></span> move the binary bits of a number left or right by a specified number of positions. When shifting left, high bits are filled with zeros, and when shifting right, low bits are filled with zeros (for unsigned numbers).
int a = 5; // Binary: a=0101
int left = a << 1; // Left shift: 1010
int right = a >> 1; // Right shift: 0010
printf("Result of a << 1: %d\n", left); // Output 10
printf("Result of a >> 1: %d\n", right); // Output 2
- • Usage:
- • Left shift:
<span>expr << n</span>, shifts the binary bits of<span>expr</span>left by<span>n</span>positions; - • Right shift:
<span>expr >> n</span>, shifts the binary bits of<span>expr</span>right by<span>n</span>positions.
4. Comparison of Logical and Bitwise Operators
| Operator | Function | Usage |
<span>&&</span> |
Logical AND | Used for boolean condition judgement |
| || | Logical OR | Used for boolean condition judgement |
<span>!</span> |
Logical NOT | Inverts boolean value |
<span>&</span> |
Bitwise AND | Performs AND operation on binary bits |
| | | Bitwise OR | Performs OR operation on binary bits |
<span>^</span> |
Bitwise XOR | Performs XOR operation on binary bits |
<span>~</span> |
Bitwise NOT | Inverts binary bits |
<span><<</span> |
Left Shift | Shifts binary bits left by specified positions |
<span>>></span> |
Right Shift | Shifts binary bits right by specified positions |
5. Notes and Recommendations
| Issue | Description |
| Priority of Logical Operators | Logical operators <span>&&</span> and || |
| Bitwise Operators and Integers | Bitwise operators are commonly used with integer types and are not suitable for non-integer types like floating-point numbers |
| Nesting of Ternary Operators | Ternary operators can be nested, but care should be taken with readability to avoid excessive complexity |
| Left and Right Shift Operations | Shifting left or right may cause sign extension issues, especially when right-shifting negative numbers |
6. Summary Overview
| Operator | Function Description | Example |
<span>&&</span> |
Logical AND | <span>a > 0 && b > 0</span> |
| || | Logical OR | a > 0 || b > 0 |
<span>!</span> |
Logical NOT | <span>!a</span> |
<span>&</span> |
Bitwise AND | <span>a & b</span> |
| | | Bitwise OR | <span>a | b</span> |
<span>^</span> |
Bitwise XOR | <span>a ^ b</span> |
<span>~</span> |
Bitwise NOT | <span>~a</span> |
<span><<</span> |
Left Shift | <span>a << 1</span> |
<span>>></span> |
Right Shift | <span>a >> 1</span> |
<span>?:</span> |
Ternary Operator (Conditional Operator) | <span>a > b ? a : b</span> |
7. Conclusion
- • Logical operators and the ternary operator are frequently used in daily programming, simplifying conditional judgement logic;
- • Bitwise operators are commonly used for low-level operations, especially in embedded programming, hardware control, and other fields;
- • When using these operators, pay attention to priority, expression readability, and choose operators wisely.