Skip to content
StudyFame

Bitwise Operators in C

Inside the computer, every number is stored as bits: zeros and ones. 5 is 101, 3 is 011. Bitwise operators work directly on these bits.

They look difficult, but with small numbers and a truth table they become easy. In this lesson you will learn all six bitwise operators, with every answer worked out in binary.

What You Will Learn

  • The 6 bitwise operators: &, |, ^, ~, <<, >>.
  • How to solve them step by step in binary.
  • Why left shift multiplies by 2 and right shift divides by 2.
  • Checking even/odd with n & 1.
  • Swapping two numbers with XOR.

Bitwise Operators in C Explained

Exam definition: Bitwise operators perform operations on the individual bits of integer values. C has six: & (AND), | (OR), ^ (XOR), ~ (NOT / one's complement), << (left shift) and >> (right shift). They work only on integer types.

OperatorNameRule for each bitExample (5 = 0101, 3 = 0011)Result
&AND1 only if both bits are 15 & 3 = 00011
|OR1 if any bit is 15 | 3 = 01117
^XOR1 if the bits are different5 ^ 3 = 01106
~NOTFlips every bit~5−6
<<Left shiftMoves bits left, adds 0s on the right5 << 1 = 101010
>>Right shiftMoves bits right, drops the last bit5 >> 1 = 00102

Truth table for one bit

aba & ba | ba ^ b
00000
01011
10011
11110

Memory trick: AND = both must agree to say 1. OR = anyone can say 1. XOR = "eXclusive": 1 only when they are different (like a light with two switches). Left shift = × 2, right shift = ÷ 2.

Why is ~5 equal to −6?

~ flips every bit of the whole int (all 32 bits), including the sign bit. In the two's complement system used by computers, ~x is always equal to -(x + 1). So ~5 = −6 and ~0 = −1.

Syntax of Bitwise Operators

a & b     a | b     a ^ b     ~a
a << n    /* shift a left by n places  = a × 2ⁿ (for non-negative a, if it fits) */
a >> n    /* shift a right by n places = a ÷ 2ⁿ (for non-negative a)            */

Example 1: All Bitwise Operators

#include <stdio.h>

int main(void)
{
    int a = 5, b = 3;     /* a = 0101, b = 0011 */

    printf("a & b  = %d\n", a & b);
    printf("a | b  = %d\n", a | b);
    printf("a ^ b  = %d\n", a ^ b);
    printf("~a     = %d\n", ~a);
    printf("a << 1 = %d\n", a << 1);
    printf("a >> 1 = %d\n", a >> 1);
    printf("1 << 4 = %d\n", 1 << 4);

    return 0;
}

Output

a & b  = 1
a | b  = 7
a ^ b  = 6
~a     = -6
a << 1 = 10
a >> 1 = 2
1 << 4 = 16

Code Explanation (Solved in Binary)

  0101  (5)        0101  (5)        0101  (5)
& 0011  (3)      | 0011  (3)      ^ 0011  (3)
  ----             ----             ----
  0001  = 1        0111  = 7        0110  = 6
  1. AND: only the last column has 1 in both → 0001 = 1.
  2. OR: every column with at least one 1 → 0111 = 7.
  3. XOR: columns where the bits differ → 0110 = 6.
  4. 5 << 1: 0101 → 1010 = 10 (doubled).
  5. 5 >> 1: 0101 → 0010 = 2 (halved, the last 1 is dropped).
  6. 1 << 4: 1 moved 4 places = 10000 = 16 = 2⁴.

How Shifting Works

  • Each place to the left doubles a binary number, just as each place to the left multiplies by 10 in decimal.
  • So x << n = x × 2ⁿ, and x >> n = x ÷ 2ⁿ (dropping the remainder), for non-negative numbers.
  • Shifting by a negative number, or by the width of the type or more (32 for int), is undefined behaviour. Left-shifting a negative number is also undefined.

Example 2: Even/Odd with & and Swap with XOR

#include <stdio.h>

int main(void)
{
    int n = 13;
    printf("%d is %s\n", n, (n & 1) ? "odd" : "even");

    int x = 10, y = 20;
    printf("Before swap: x = %d, y = %d\n", x, y);

    x = x ^ y;
    y = x ^ y;   /* (x ^ y) ^ y = x */
    x = x ^ y;   /* (x ^ y) ^ x = y */

    printf("After swap : x = %d, y = %d\n", x, y);
    return 0;
}

Output

13 is odd
Before swap: x = 10, y = 20
After swap : x = 20, y = 10

n & 1 keeps only the last bit. Odd numbers end in 1 in binary, even numbers end in 0. The XOR swap works because a ^ b ^ b = a (XOR with the same value twice cancels out).

Important Points to Remember

  • 6 bitwise operators: & | ^ ~ << >>.
  • They work only on integers.
  • XOR gives 1 when bits are different.
  • ~x = −(x + 1).
  • << 1 = × 2; >> 1 = ÷ 2.

Common Mistakes Beginners Make

MistakeWhat happensFix
Using ^ for power: 2^3Gives 1 (XOR), not 8pow(2, 3) or 1 << 3
Using & instead of && in conditionsBitwise result, no short-circuitUse && for logic
n & 1 == 0 for even== runs first: n & (1 == 0) = 0(n & 1) == 0 with brackets
Bitwise on a floatCompile errorUse integers only

Bitwise Operators Practice Questions

  1. Name the six bitwise operators.
  2. Find 12 & 10, 12 | 10 and 12 ^ 10.
  3. What is ~10?
  4. What is 3 << 2 and 20 >> 2?
  5. How do you check if a number is odd using a bitwise operator?
Show answers
  1. &, |, ^, ~, <<, >>.
  2. 12 = 1100, 10 = 1010. AND = 1000 = 8; OR = 1110 = 14; XOR = 0110 = 6.
  3. −11 (since ~x = −(x + 1)).
  4. 3 << 2 = 12 (3 × 4); 20 >> 2 = 5 (20 ÷ 4).
  5. if (n & 1) is true for odd numbers.

Coding Exercise

Read a number and print it multiplied by 8 and divided by 4 using only shift operators.

Show solution
#include <stdio.h>

int main(void)
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    printf("n x 8 = %d\n", n << 3);
    printf("n / 4 = %d\n", n >> 2);
    return 0;
}
Enter a number: 40
n x 8 = 320
n / 4 = 10

Quick Quiz: Bitwise Operators in C (MCQ)

  1. 6 & 3 equals:

    A) 2   B) 7   C) 5   D) 1

  2. 6 | 3 equals:

    A) 2   B) 7   C) 5   D) 9

  3. 6 ^ 3 equals:

    A) 2   B) 7   C) 5   D) 216

  4. ~0 equals:

    A) 0   B) 1   C) −1   D) 255

  5. 1 << 5 equals:

    A) 5   B) 10   C) 16   D) 32

Show answers
  1. A: 110 & 011 = 010 = 2.
  2. B: 110 | 011 = 111 = 7.
  3. C: 110 ^ 011 = 101 = 5.
  4. C: −1.
  5. D: 32.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Operators& | ^ ~ << >>
5 & 3 / 5 | 3 / 5 ^ 31 / 7 / 6
~x−(x + 1); ~5 = −6
x << nx × 2ⁿ
x >> nx ÷ 2ⁿ
Odd checkn & 1
Works onIntegers only

Frequently asked questions

What is the difference between & and &&?
& works on each bit; && works on whole conditions (true/false) with short-circuit.
How do you swap two numbers without a third variable?
With XOR: a = a ^ b; b = a ^ b; a = a ^ b;
Why is left shift by 1 the same as multiplying by 2?
Each binary place is worth twice the one to its right, so moving bits left doubles the value.
Is ^ the power operator?
No. ^ is XOR; use pow() for powers.

Remember

  • XOR = different → 1.
  • Left × 2, right ÷ 2; ~x = −(x + 1).

Next Topic

Next lesson: Ternary Operator in C. You will learn a one-line way to choose between two values.