Bitwise AND Operator: Usage and Examples in C

Understanding how data is manipulated at the bit level is a key part of becoming a skilled C programmer. One of the fundamental tools in this domain is the Bitwise AND operator (&). It’s simple in concept but powerful in practice especially when used in systems programming, embedded devices, and performance-critical applications.

Bitwise AND Operator

This blog will explain what the Bitwise AND operator is, how it works, where it’s useful, and how to implement it in real-world C programs.

Want to explore more C programming fundamentals and advanced techniques with hands-on practice?


Check out Uncodemy’s C Programming course and take your learning further.

What is the Bitwise AND Operator in C?

The Bitwise AND operator is denoted by &. It operates on bits and performs a logical AND operation on corresponding bits of two integers.

In simpler terms:

  • 1 & 1 = 1
  • 1 & 0 = 0
  • 0 & 1 = 0
  • 0 & 0 = 0

Only if both bits are 1, the result will be 1. Otherwise, it’s 0.

Syntax

result = operand1 & operand2;

Both operands must be integers. The result will be an integer representing the bitwise AND of the two.

Basic Example: Bitwise AND of Two Numbers

Copy Code

#include <stdio.h>

int main() {

    int a = 12;  // Binary: 1100

    int b = 10;  // Binary: 1010

    int result = a & b;

   printf("Bitwise AND of %d and %d is %d\n", a, b, result);

    return 0;

}

Output:

Bitwise AND of 12 and 10 is 8

Why?

   1100

&  1010

=  1000  → Decimal = 8

Real-World Use Cases of Bitwise AND in C

The operator may seem technical, but it has many everyday uses in programming. Here are a few practical examples:

1. Checking If a Number is Even or Odd

Copy Code

int num = 7;

if (num & 1)

   printf("Odd\n");

else

   printf("Even\n");

2. Clearing Specific Bits (Bit Masking)

Copy Code

int num = 0b1111;   // 15 in decimal

int mask = 0b1100;  // Only preserve the last 2 bits

int result = num & mask; // Result is 0b1100 → 12

3. Feature Flags in Embedded Systems

Copy Code

#define FEATURE_A 0x01

#define FEATURE_B 0x02

#define FEATURE_C 0x04

int system_flags = 0x03; // Binary: 0011 → A and B enabled

if (system_flags & FEATURE_A)

   printf("Feature A is ON\n");

if (system_flags & FEATURE_C)

   printf("Feature C is ON\n");

4. IP Filtering or Masking

Copy Code

int ip = 192;

int subnet = 255;

int network = ip & subnet;

When Should You Use Bitwise AND?

Use it when:

  • You need speed (bitwise operations are faster than arithmetic).
  • You're working with embedded systems or low-level drivers.
  • You're manipulating binary data.
  • You want to pack multiple boolean values into a single integer.

Caution While Using Bitwise AND

  • Operands must be integers (not floats or strings).
  • Be careful with signed numbers bitwise operations can yield unexpected results due to two's complement representation.
  • Always comment your code for readability, especially in teams or production projects.

Bitwise AND vs Logical AND

Don’t confuse & with &&.

OperatorNameWorks OnReturns
&Bitwise ANDBitsInteger result
&&Logical ANDConditions0 (false) or 1 (true)

Copy Code

int a = 0, b = 1;

printf("%d\n", a & b);   // Output: 0

printf("%d\n", a && b);  // Output: 0 (false condition)

Comparison Table: Bitwise Operators in C

OperatorDescription
&Bitwise AND
``
^Bitwise XOR
~Bitwise NOT
<<Left Shift
>>Right Shift

 

FAQs: Bitwise AND Operator

Q1. What is the primary use of the Bitwise AND operator?
It’s used to manipulate bits directly. Common use cases include masking, setting/clearing bits, and low-level hardware control.

Q2. How is it different from Logical AND?
Bitwise AND works on individual bits. Logical AND (&&) works on boolean expressions.

Q3. Can I use Bitwise AND with negative numbers?
Yes, but be aware of how negative numbers are represented in memory (two’s complement).

Q4. Is bitwise AND faster than modulo or division?
Yes. Bitwise operations are significantly faster since they’re handled at the hardware level.

Q5. Can I use Bitwise AND on floating point numbers?
No. Bitwise operations are only valid for integer types in C.

Conclusion

The Bitwise AND operator in C may seem intimidating at first, but it's a valuable tool in any systems programmer’s toolkit. From simple checks like determining even or odd numbers to complex tasks like permissions, data compression, and hardware interaction—it has a wide range of applications.

If you're preparing for technical interviews, competitive programming, or embedded projects, make sure you’re comfortable with bitwise logic.

Ready to master bitwise operations and level up your C skills?
Explore hands-on projects, challenges, and more at Uncodemy’s blog and training platform.

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses