โž• C Operators
Estimated reading: 3 minutes 276 views

C Bitwise Operators โ€“ Working with Bits in C Programming

Introduction โ€“ What Are Bitwise Operators in C?

Bitwise operators in C allow direct manipulation of individual bits within integers. They are highly efficient for low-level programming, especially in embedded systems, cryptography, drivers, and performance-critical applications where memory and processing time matter.

In this guide, youโ€™ll learn:

  • The list and syntax of bitwise operators
  • How each bitwise operator functions
  • Common use cases and examples
  • Important tips and common pitfalls

List of Bitwise Operators in C

OperatorNameDescriptionExample (a = 5, b = 3)
&Bitwise ANDSets each bit to 1 if both bits are 1a & b โ†’ 1 (0101 & 0011 = 0001)
``Bitwise ORSets each bit to 1 if either bit is 1
^Bitwise XORSets each bit to 1 if only one bit is 1 (exclusive OR)a ^ b โ†’ 6 (0101 ^ 0011 = 0110)
~Bitwise NOTInverts all the bits (1โ€™s complement)~a โ†’ -6 (~0101 = 1010)
<<Left ShiftShifts bits to the left and fills with 0sa << 1 โ†’ 10 (0101 << 1 = 1010)
>>Right ShiftShifts bits to the righta >> 1 โ†’ 2 (0101 >> 1 = 0010)

Examples of Bitwise Operators

#include <stdio.h>

int main() {
    int a = 5, b = 3;
    
    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);
    return 0;
}

Output:

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

Binary Representation (Visualization)

DecimalBinary
50101
30011
a & b0001 โ†’ 1
`ab`
a ^ b0110 โ†’ 6

Common Use Cases

  • Setting, clearing, and toggling specific bits
  • Checking if a number is odd/even (num & 1)
  • Efficient multiplication/division by powers of 2 using shift (<<, >>)
  • Permissions and flags (like chmod in Linux)
  • CRCs, hashing, cryptography

Tips and Pitfalls

  • Bitwise NOT (~) may return negative values due to 2โ€™s complement representation
  • Bitwise operators only work on integral types (int, char, long)
  • Left shifting too far may result in undefined behavior
  • Use unsigned types for consistent shifting behavior

Summary โ€“ Recap & Next Steps

Bitwise operators provide direct access to bits for fast and memory-efficient programming. They are powerful tools in low-level programming but require a solid understanding of binary logic.

Key Takeaways:

  • Use &, |, ^, ~, <<, and >> to manipulate bits
  • Operate only on integers and charsโ€”not floats or doubles
  • Bit shifts are useful for fast multiplications/divisions by 2
  • Bitwise operations are core to embedded systems and bitmasking

Real-World Relevance:

Bitwise operators are widely used in hardware interfaces, microcontrollers, graphics engines, encryption algorithms, and any situation where you need precise, low-level control.


Frequently Asked Questions (FAQ)

What is the difference between & and &&?

& is a bitwise AND operator (compares individual bits).
&& is a logical AND operator (evaluates overall Boolean expressions).


Can bitwise operators be used on float or double?

No. Bitwise operators can only be applied to integral types like int, char, short, or long.


What does the ~ operator do?

The ~ operator performs a bitwise NOT (1โ€™s complement), inverting each bit of the operand.


How do I check if a number is even or odd using bitwise?

Use num & 1:

if (num & 1)  // odd
else          // even

What does a << 2 mean?

Left shift a by 2 bits. Equivalent to multiplying a by 2^2 = 4.

int a = 3;
int result = a << 2;  // 3 * 4 = 12

Share Now :
Share

๐Ÿงฎ C Bitwise Operators

Or Copy Link

CONTENTS
Scroll to Top