C Bitwise Operators

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Bitwise Operators?

Bitwise operators act on the binary representation of integers. & keeps bits set in both, | keeps bits set in either, ^ keeps bits that differ, ~ flips every bit, and << / >> shift bits left or right. Prefer unsigned types so shifts and ~ behave predictably.

Remember
a & b    /* AND   */   a | b    /* OR  */
a ^ b    /* XOR   */   ~a       /* NOT */
a << n   /* shift left: multiply by 2^n */
a >> n   /* shift right: divide by 2^n  */

Cheat Sheet

Keep these facts handy while you work with Bitwise Operators in C.

TopicDetail
AND1100 & 1010 = 1000
OR1100 | 1010 = 1110
XOR1100 ^ 1010 = 0110
Shift3 << 2 is 12

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Bit Manipulation Techniques.

Example 1 — Bitwise operators on 12 and 10

12 is 1100 in binary and 10 is 1010.

C
#include <stdio.h>

int main(void) {
    unsigned a = 12;   /* 1100 */
    unsigned b = 10;   /* 1010 */

    printf("a & b  = %u\n", a & b);
    printf("a | b  = %u\n", a | b);
    printf("a ^ b  = %u\n", a ^ b);
    printf("a << 2 = %u\n", a << 2);
    printf("a >> 2 = %u\n", a >> 2);
    printf("~a & 0xF = %u\n", ~a & 0xFu);
    return 0;
}

How It Works

  1. 1100 & 1010 keeps only the shared bit: 1000 = 8.
  2. ^ keeps bits that differ: 0110 = 6.
  3. Shifting left by 2 multiplies by 4; right by 2 divides by 4.
  4. ~a flips all 32 bits, so mask with 0xF to see the low four bits (0011).

Edge Cases & Pitfalls

Check these before relying on Bitwise Operators in real programs.

shift

Shifting too far

Shifting by a negative amount or by the type width or more is undefined. Keep the count below 32 for a 32-bit int.

signed

Shifting signed values

Right shift of a negative signed number is implementation-defined. Use unsigned types for bit work.

Key Takeaways

  • Unsigned: safest for bit work.
  • Shift: x << n multiplies by 2^n.
  • Mask: AND with a mask extracts bits.

One line: Bitwise operators act on the binary representation of integers.

Frequently Asked Questions

& combines each pair of bits and returns an integer. && is a logical AND that returns 0 or 1 and short-circuits.
Signed shifts and bitwise NOT can produce implementation-defined or undefined results. Unsigned types have well-defined wraparound and shifting.
Continue with "C Bit Manipulation Techniques" next. Set, clear, toggle, and test bits, build bitmasks, and count set bits. The C Operators hub lists every topic in order.

Next: Bit Manipulation Techniques

Set, clear, toggle, and test bits, build bitmasks, and count set bits.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful