Operator Taxonomy
| Category | Symbols |
|---|---|
| Arithmetic | + - * / % |
| Shift | << >> |
| Bitwise | `& |
| Assignment | `= += -= *= /= %= <<= >>= &= |
| Unary | ! ++ -- & * + - ~ sizeof |
| Relational | < > <= >= == != |
| Logical | && || |
| Ternary | ? : |
| Sequence | , |
| Subscript | [] |
| Call | () |
| Member | . -> |
Binary Representations
Radix Conversion
- Any base → decimal: positional expansion
e.g.0b1011 = 1·2³ + 0·2² + 1·2¹ + 1·2⁰ = 11 - Decimal → binary (integer part): repeated division by 2, collect remainders in reverse order.
- Binary ↔ octal: group bits in 3-tuples (421 weights).
0b110'010 = 0o62 - Binary ↔ hex: group bits in 4-tuples (8421 weights).
0b1100'1010 = 0xCA
Signed Integer Encodings
For n-bit signed integers:
- Sign-and-magnitude: MSB is sign, rest are magnitude.
- One’s complement: negative value is bitwise NOT of positive.
- Two’s complement: negative value is one’s complement + 1.
Modern C uses two’s complement exclusive.
Shift Operators
| Operator | Direction | Vacated Bits | Effect |
|---|---|---|---|
<< |
left | filled with 0 | x << n equals x * 2ⁿ |
>> |
right | 0 (logical) or sign-bit (arithmetic) | x >> n equals x / 2ⁿ (implementation-defined for negative) |
#include <stdio.h>
int main(void) {
unsigned int v = 6; /* 0b110 */
printf("%u %u\n", v << 2, v >> 1); /* 24 3 */
}
Bitwise Operators
| Op | Name | Rule |
|---|---|---|
& |
AND | 1 only if both bits are 1 |
| |
OR | 1 if either bit is 1 |
^ |
XOR | 1 if bits differ |
~ |
NOT | flip every bit |
Swap without temporary:
int x = 5, y = 3;
x ^= y;
y ^= x; /* y == 5 */
x ^= y; /* x == 3 */
Unary Operators
!x– logical NOT (0 → 1, non-zeero → 0)++x/x++– pre/post increment--x/x--– pre/post decrement&x– address-of*p– dereference+x,-x– unary plus/minus~x– bitwise complementsizeof (type)– size in bytes
Comma Operator
(e1, e2, …, eN) evaluates left-to-right; the whole expression yields eN.
int a = (printf("first\n"), 2 + 3); /* prints, then a == 5 */
Subscript and Function Call
arr[i]is exactly*(arr + i); both operands can be any intergal expression.f(arg1, arg2)invokesf; the first operand is the function designator, the rest are arguments.
Structure Member Access
typedef struct {
double real;
double imag;
} complex;
complex z = { 3.0, 4.0 };
complex *zp = &z;
z.real = 5.0; /* direct access */
zp->imag = 6.0; /* indirect access */
Precedence and Associativity
Use parentheses to enforce clarity; consult the standard table when in doubt. Examples:
int r = a + b * c; /* * has higher precedence */
int s = a & b == 0; /* == evaluated before & unless parenthesized */