Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering Bitwise Operators in C: AND, OR, XOR, Shifts & Complement

What are Bitwise Operators?

Bitwise operators manipulate individual bits of integer values. Unlike arithmetic operators that work on whole numbers, bitwise operators act on the binary representation, making them ideal for performance‑critical tasks such as flags, bit masks, and low‑level device control.

In C, the following bitwise operators are available:

OperatorMeaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR (exclusive OR)
~Bitwise NOT (one’s complement)
<<Left shift
>>Right shift

Bitwise operators are defined only for integral types; they are not applicable to floating‑point types like float or double.

The operations start at the least significant bit (LSB) and proceed towards the most significant bit (MSB). The following truth table summarizes the binary results:

Operand xOperand yx & yx | yx ^ y
00000
01011
10011
11110

In the following sections we’ll dive deeper into each operator with examples.

Bitwise AND (&)

The AND operator returns 1 only when both corresponding bits are 1; otherwise the result is 0. It is commonly used to mask out unwanted bits.

Op1 = 0000 1101
Op2 = 0001 1001
Result = 0000 1001

Bitwise OR (|)

The OR operator returns 1 if at least one of the bits is 1. It is useful for setting specific bits.

Op1 = 0000 1101
Op2 = 0001 1001
Result = 0001 1101

Bitwise XOR (^)

The XOR operator returns 1 only when the bits differ. It’s handy for toggling bits.

Op1 = 0000 1101
Op2 = 0001 1001
Result = 0001 0100

Example program:

#include <stdio.h>
int main() {
    int a = 20;   /* 20 = 010100 */
    int b = 21;   /* 21 = 010101 */
    int c = 0;

    c = a & b;          /* 20 = 010100 */
    printf("AND   - c = %d\n", c);

    c = a | b;          /* 21 = 010101 */
    printf("OR    - c = %d\n", c);

    c = a ^ b;          /* 1  = 000001 */
    printf("XOR   - c = %d\n", c);
    return 0;
}

Output:

AND   - c = 20
OR    - c = 21
XOR   - c = 1

Bitwise Shift Operators (<< and >>)

Shift operators move bits left or right by a specified count.

Example with a 4‑bit number 1111:

x << 2  → 1100
x >> 2  → 0011

Program illustration:

#include <stdio.h>
int main() {
    int a = 20;   /* 20 = 010100 */
    int c = 0;

    c = a << 2;   /* 80 = 101000 */
    printf("Left shift  - c = %d\n", c);

    c = a >> 2;   /* 5  = 000101 */
    printf("Right shift - c = %d\n", c);
    return 0;
}

Output:

Left shift  - c = 80
Right shift - c = 5

Bitwise Complement (~)

The complement operator flips every bit: 1 becomes 0 and vice versa. It is a unary operator applied to a single operand.

0000 1111  → 1111 0000

Example program:

#include <stdio.h>
int main() {
    int a = 10;   /* 10 = 001010 */
    int c = ~a;   /* -11 in two’s complement */
    printf("Complement - c = %d\n", c);
    return 0;
}

Output:

Complement - c = -11

All Operators Together

Below is a concise program that demonstrates every bitwise operator discussed.

#include <stdio.h>
int main() {
    unsigned int x = 48;  /* 0011 0000 */
    unsigned int y = 13;  /* 0000 1101 */
    int z = 0;

    z = x & y;  /* 0  = 0000 0000 */
    printf("AND  - x & y = %d\n", z);

    z = x | y;  /* 61 = 0011 1101 */
    printf("OR   - x | y = %d\n", z);

    z = x ^ y;  /* 61 = 0011 1101 */
    printf("XOR  - x ^ y = %d\n", z);

    z = ~x;     /* -49 = 1111 0100 (two’s complement) */
    printf("NOT  - ~x = %d\n", z);

    z = x << 2; /* 192 = 1100 0000 */
    printf("Left shift - x << 2 = %d\n", z);

    z = x >> 2; /* 12  = 0000 1100 */
    printf("Right shift - x >> 2 = %d\n", z);
    return 0;
}

Result:

AND  - x & y = 0
OR   - x | y = 61
XOR  - x ^ y = 61
NOT  - ~x = -49
Left shift - x << 2 = 192
Right shift - x >> 2 = 12

Key Takeaways

C Language

  1. C# Operators – Comprehensive Guide to Operators in C#
  2. Mastering C# Bitwise & Bit Shift Operators: A Comprehensive Guide
  3. Mastering C++ Operators: A Complete Guide with Practical Examples
  4. Mastering C Programming Operators: A Comprehensive Guide
  5. GaN Power Amplifiers: Driving Sub‑6 GHz 5G Massive MIMO Beyond LDMOS
  6. IoT & Blockchain: A Paradigm Shift in Data Trust and Efficiency
  7. C++ Operators Explained: Types, Examples, and Sample Programs
  8. Comprehensive Guide to C Operators: Types, Functions & Examples
  9. Mastering C# Operators: A Comprehensive Guide
  10. Valve Types: Function, Port Configuration & Operator Styles