C
Operators in C - Bitwise and Conditional Operators
Operators in C

Operators in C

Operators tell C to perform a calculation, comparison, logical decision, assignment, or bit-level operation.

In the expression total = a + b;, which symbol performs addition?
Connect this lesson with variables: operators act on values stored in variables and produce a result.
operator_example.cExpression
int a = 7; int b = 5; int total = a + b; printf("%d", total);
Expression anatomy

Operands, operator and result

7Left operand
+
5Right operand
=
12Result
1

Unary operator

Works with one operand.

-a ++a !flag
2

Binary operator

Works with two operands.

a + b a > b a & b
3

Ternary operator

Uses three expressions.

condition ? x : y
Emphasize that unary, binary, and ternary describe the number of operands, not the type of result.
Classification

Major operator groups

Arithmetic
+ - * / %

Perform numeric calculations.

Relational
< <= > >= == !=

Compare two values.

Logical
&& || !

Combine or reverse conditions.

Assignment
= += -= *= /= %=

Store or update a value.

Increment
++ --

Increase or decrease by one.

Bitwise
& | ^ ~ << >>

Operate on individual bits.

Conditional
? :

Select one of two expressions.

Other
sizeof , & *

Special purposes such as size and addresses.

Arithmetic operators

Calculate with two integer values

17 + 5
22

Integer division matters

17 / 5 → 3 17 % 5 → 2

When both operands are integers, the fractional part of division is discarded.

The remainder operator % is used with integer operands.
Ask students to predict both 17 / 5 and 17 % 5 before revealing the result.
Relational operators

Comparisons produce true or false

ExpressionC resultMeaning

Important distinction

a = b; // assignment a == b; // equality test
In C, false is represented by 0. A true relational result is represented by 1.
Logical operators

Combining conditions

ABA && BA || B
0000
0101
1001
1111

Admission condition

age >= 18 && marks >= 60
Combined result
1
Both conditions are true.
Explain short-circuit behavior briefly: with &&, the second operand is not evaluated when the first is false; with ||, it is not evaluated when the first is true.
Assignment operators

Store and update values

=

Simple assignment

Stores the right-hand value in the left-hand variable.

score = 20;
+=

Compound assignment

Combines an operation with assignment.

score += 5; // same as score = score + 5;
%=

Other forms

Several arithmetic and bitwise operators have assignment forms.

-= *= /= %= &= |= ^= <<= >>=
int
score
10
score is initialized with 10.
Increment and decrement

Prefix and postfix are not always identical

Prefix: y = ++x;

x = 5; y = ++x;
x and y
?

Postfix: y = x++;

x = 5; y = x++;
x and y
?
Prefix changes the variable before its value is used. Postfix uses the old value first, then changes the variable.
Expression evaluation

Precedence controls which operator acts first

Compare the two expressions

2 + (3 * 4)
14
High()Parentheses
++ -- ! ~Unary
* / %Multiplicative
+ -Additive
<< >>Shift
< <= > >= == !=Comparison
& ^ | && ||Bitwise and logical
Low?: =Conditional and assignment
Recommend parentheses whenever an expression could be misread, even when the precedence rule is known.
& | ^

Bitwise Operators

Bitwise operators inspect or modify individual binary digits. Use unsigned values for the clearest introductory examples.

Bit-level logic

How AND, OR and XOR work on one bit

ABA & BA | BA ^ B
00000
01011
10011
11110
&

AND

Result bit is 1 only when both input bits are 1.

|

OR

Result bit is 1 when at least one input bit is 1.

^

XOR

Result bit is 1 when the input bits are different.

Interactive 8-bit view

Toggle the bits and apply an operator

Value A170 decimal
1286432168421
Value B204 decimal
1286432168421
OperationLow 8-bit teaching view
10101010 & 11001100
10001000
Result = 136 decimal
The complement demo shows only the low 8 bits: (~A) & 0xFFu. Actual unsigned integer width depends on the C implementation.
Use values A = 170 (10101010) and B = 204 (11001100) to make alternating bit patterns easy to compare.
Shift operators

Move bits left or right

Left shift <<

00001101 = 13 00011010 = 26 13 << 1 = 26

For an unsigned value when no significant bit is lost, shifting left by one corresponds to multiplication by 2.

Right shift >>

00011010 = 26 00001101 = 13 26 >> 1 = 13

For a non-negative unsigned value, shifting right by one corresponds to integer division by 2.

Do not shift by a negative count or by a count equal to or greater than the width of the promoted left operand.
Do not confuse them

Logical operators versus bitwise operators

Logical Conditions

a && b a || b !a
  • Treats zero as false and non-zero as true
  • Produces 0 or 1
  • && and || short-circuit

Bitwise Individual bits

a & b a | b a ^ b ~a
  • Processes every corresponding bit
  • Produces a bit pattern
  • Does not short-circuit
& is not a replacement for &&, and | is not a replacement for ||.
? :

Conditional Operator

The conditional operator evaluates one condition and selects one of two expressions.

Ternary operator

condition ? true-expression : false-expression

Condition
marks >= 40
?
If true
"Pass"
:
If false
"Fail"
conditional_operator.cOne selected expression
const char *result = (marks >= 40) ? "Pass" : "Fail";
Clarify that only one of the second or third operands is evaluated after the condition is tested.
Interactive examples

Use the conditional operator for a simple choice

Pass or fail

marks >= 40 ? "Pass" : "Fail"
Selected result
Pass

Maximum of two values

a > b ? a : b
Maximum
22
When to use it

Conditional operator and if-else

Conditional operator

max = (a > b) ? a : b;

Useful when choosing one of two values in a short, readable expression.

if-else statement

if (a > b) max = a; else max = b;

Preferable when each branch performs multiple statements or needs clearer control flow.

Avoid deeply nested conditional operators because they quickly become difficult to read.
Integrated implementation

Arithmetic, logical, bitwise and conditional operators

operators_demo.cCombined example
#include <stdio.h> int main(void) { int a = 12, b = 5; unsigned int flags = 10u; // 1010 in binary int sum = a + b; int greater = (a > b); unsigned int lowBits = flags & 3u; int maximum = (a > b) ? a : b; printf("Sum = %d\n", sum); printf("a > b = %d\n", greater); printf("Low two bits = %u\n", lowBits); printf("Maximum = %d\n", maximum); return 0; }
Common mistakes

Similar symbols can have very different meanings

if (a = b)
if (a == b)
a & b for conditions
a && b
17 / 5 = 3.4
17 / 5 = 3
x = x++;
x++; // update x directly
a > b ? a;
a > b ? a : b
In C, avoid modifying the same scalar object more than once without proper sequencing. Expressions such as i = i++; are not a safe way to increment a variable.
Quick assessment

Check your understanding

Question 1 of 10
Ask students to answer before selecting an option on screen.
Final recap

Five ideas to remember

+

Arithmetic

Calculates numeric results.

>

Comparison

Produces 0 or 1.

&&

Logical

Combines conditions.

&

Bitwise

Processes individual bits.

?:

Conditional

Selects one of two expressions.

()

Parentheses

Make evaluation order clear.