Lesson 20 of 60 – Bitwise Operators in C
33%

Bitwise Operators in C

Bitwise operators are used to perform operations directly on the individual bits of integer values. They are useful in low-level programming, embedded systems, device programming, flags, masks, and other situations where individual bits need to be manipulated.

Note: The main bitwise operators in C are &, |, ^, ~, <<, and >>.

1. What are Bitwise Operators?

Bitwise operators work on the binary representation of integer operands. Instead of treating a number only as a complete value, they operate on individual bits.

unsigned int a = 5;
unsigned int b = 3;

printf("%u", a & b);

Here, the & operator performs a bitwise AND operation.

2. Binary Representation

Computers represent integer values using binary digits called bits. Each bit can contain either 0 or 1.

Decimal 5 = Binary 0101
Decimal 3 = Binary 0011

Bitwise operators work with these binary bits.

3. Bitwise Operators in C

Operator Name
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift

4. Bitwise AND Operator &

The bitwise AND operator compares corresponding bits. A result bit is 1 only when both corresponding bits are 1.

5 = 0101
3 = 0011
    ----
    0001

Therefore:

5 & 3 = 1

5. Bitwise AND Example

#include <stdio.h>

int main()
{
    unsigned int a = 5;
    unsigned int b = 3;

    printf("%u", a & b);

    return 0;
}

Output:

1

6. AND Truth Table

Bit A Bit B A & B
0 0 0
0 1 0
1 0 0
1 1 1

Only 1 & 1 produces 1.

7. Bitwise OR Operator |

The bitwise OR operator produces a 1 when at least one of the corresponding bits is 1.

5 = 0101
3 = 0011
    ----
    0111

Therefore:

5 | 3 = 7

8. Bitwise OR Example

#include <stdio.h>

int main()
{
    unsigned int a = 5;
    unsigned int b = 3;

    printf("%u", a | b);

    return 0;
}

Output:

7

9. OR Truth Table

Bit A Bit B A | B
0 0 0
0 1 1
1 0 1
1 1 1

Only 0 | 0 produces 0.

10. Bitwise XOR Operator ^

The bitwise XOR operator produces 1 when the two corresponding bits are different.

5 = 0101
3 = 0011
    ----
    0110

Therefore:

5 ^ 3 = 6

11. XOR Example

#include <stdio.h>

int main()
{
    unsigned int a = 5;
    unsigned int b = 3;

    printf("%u", a ^ b);

    return 0;
}

Output:

6

12. XOR Truth Table

Bit A Bit B A ^ B
0 0 0
0 1 1
1 0 1
1 1 0

XOR produces 1 when the bits are different.

13. Bitwise NOT Operator ~

The bitwise NOT operator ~ reverses every bit of its operand. Each 0 becomes 1, and each 1 becomes 0.

Binary:
00000101

After ~:
11111010

The exact decimal result depends on the type and representation of the operand.

14. NOT Example with Unsigned Integer

#include <stdio.h>

int main()
{
    unsigned int value = 5;

    printf("%u", ~value);

    return 0;
}

For an unsigned integer, the result is the bitwise complement of the value across the width of the type.

15. Left Shift Operator <<

The left shift operator moves the bits of its left operand toward the left by the number of positions specified by the right operand.

unsigned int number = 5;

number << 1

For this positive value, the binary representation is shifted one position to the left, producing 10.

5  = 0101
5 << 1 = 1010

Result = 10

16. Left Shift Example

#include <stdio.h>

int main()
{
    unsigned int number = 5;

    printf("%u", number << 1);

    return 0;
}

Output:

10

17. Right Shift Operator >>

The right shift operator moves the bits of its left operand toward the right by the number of positions specified by the right operand.

unsigned int number = 8;

number >> 1

For this unsigned value:

8       = 1000
8 >> 1  = 0100

Result = 4

18. Right Shift Example

#include <stdio.h>

int main()
{
    unsigned int number = 8;

    printf("%u", number >> 1);

    return 0;
}

Output:

4

19. Bitwise AND for Checking Bits

Bitwise AND is often used with a mask to check whether a particular bit is set.

unsigned int value = 5;

if (value & 1)
{
    printf("Last bit is 1");
}

The expression checks the least significant bit of the value.

20. Checking Even and Odd Numbers

For non-negative integers, the least significant bit can be used to check whether a number is even or odd.

unsigned int number = 7;

if (number & 1)
{
    printf("Odd");
}
else
{
    printf("Even");
}

Output:

Odd

21. Bitwise AND Assignment &=

The &= operator performs bitwise AND and assigns the result back to the left operand.

unsigned int a = 7;
unsigned int b = 3;

a &= b;

printf("%u", a);

Output:

3

It is equivalent to a = a & b;.

22. Bitwise OR Assignment |=

unsigned int a = 4;
unsigned int b = 2;

a |= b;

printf("%u", a);

Output:

6

It is equivalent to a = a | b;.

23. Bitwise XOR Assignment ^=

unsigned int a = 7;
unsigned int b = 3;

a ^= b;

printf("%u", a);

Output:

4

It is equivalent to a = a ^ b;.

24. Left Shift Assignment <<=

unsigned int number = 4;

number <<= 1;

printf("%u", number);

Output:

8

The statement is equivalent to number = number << 1;.

25. Right Shift Assignment >>=

unsigned int number = 8;

number >>= 1;

printf("%u", number);

Output:

4

The statement is equivalent to number = number >> 1;.

26. Bit Masking

A bit mask is a value used with bitwise operators to select or manipulate specific bits.

unsigned int value = 13;
unsigned int mask = 4;

if (value & mask)
{
    printf("Bit is set");
}

The mask selects the bit represented by the value 4.

27. Common Bitwise Operator Mistakes

  • Confusing bitwise AND & with logical AND &&.
  • Confusing bitwise OR | with logical OR ||.
  • Using bitwise operators without understanding binary representation.
  • Ignoring the type of the integer operand.
  • Using an invalid or inappropriate shift count.
  • Assuming right-shifting a signed negative value always behaves like right-shifting an unsigned value.
  • Forgetting that bitwise NOT operates on all bits of the operand's type.

28. Complete Bitwise Example

#include <stdio.h>

int main()
{
    unsigned int a = 5;
    unsigned int b = 3;

    printf("AND = %u\n", a & b);
    printf("OR = %u\n", a | b);
    printf("XOR = %u\n", a ^ b);
    printf("Left Shift = %u\n", a << 1);
    printf("Right Shift = %u\n", a >> 1);

    return 0;
}

Output:

AND = 1
OR = 7
XOR = 6
Left Shift = 10
Right Shift = 2

29. Bitwise Operators with User Input

#include <stdio.h>

int main()
{
    unsigned int a, b;

    printf("Enter two positive integers: ");
    scanf("%u %u", &a, &b);

    printf("AND = %u\n", a & b);
    printf("OR = %u\n", a | b);
    printf("XOR = %u\n", a ^ b);

    return 0;
}

This program accepts two unsigned integer values and performs three common bitwise operations.

30. Practice with Bitwise Operators

Practice the following programs:

  1. Perform bitwise AND on two numbers.
  2. Perform bitwise OR on two numbers.
  3. Perform bitwise XOR on two numbers.
  4. Use bitwise NOT on an unsigned integer.
  5. Perform a left shift.
  6. Perform a right shift.
  7. Check whether a number is even or odd using & 1.
  8. Use a mask to check a particular bit.
  9. Use &=, |=, and ^=.
  10. Write a program that demonstrates all major bitwise operators.

Practice binary representations along with these operators to understand how individual bits are manipulated.

📌 Key Points

  • Bitwise operators work on individual bits of integer operands.
  • & performs bitwise AND.
  • | performs bitwise OR.
  • ^ performs bitwise XOR.
  • ~ performs bitwise NOT.
  • << shifts bits to the left.
  • >> shifts bits to the right.
  • Bitwise operators are different from logical operators.
  • Bit masks can be used to check or manipulate individual bits.
  • Unsigned integer types are often convenient when demonstrating bit-level operations.

🧠 Quick Quiz

Question: Which operator performs bitwise AND in C?