Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Pass By Reference C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Arrays of Structs C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example C Memory Mistakes

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C Problem Solving

C Problem Solving C Build the Logic C Edge Cases & Errors C Test Cases

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers C Command-Line Args

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Hash Tables C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C Bitwise Operators


Share

C Bitwise Operators

Note: This is a more advanced topic in C. If you are new to programming, don't worry if it feels tricky at first - bitwise operators are mainly used in special cases like system programming, hardware control, or performance optimizations.

In C, bitwise operators let you work directly with the bits (the 1s and 0s) that make up numbers in binary form.

Every integer in a computer is stored in binary, which means it is represented using bits (binary digits) that are either 0 or 1. Bitwise operators allow you to compare, combine, shift, or flip these bits.

Note: Bitwise operations only work on integer types (such as int, char, or long).

Tip: If you are not yet familiar with binary numbers, check out these pages first:


List of Bitwise Operators

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if at least one of the bits is 1
^XORSets each bit to 1 if only one of the bits is 1
~NOTInverts all the bits
<<Left ShiftShifts bits to the left (multiplies by powers of 2)
>>Right ShiftShifts bits to the right (divides by powers of 2)

Binary Example

Let's start with two integers:

int a = 6;  // 0110 in binary
int b = 3;  // 0011 in binary

Here's how different bitwise operators work on these values:

OperationBinary ResultDecimal Result
a & b00102
a | b01117
a ^ b01015
~a...1001-7 (on most systems)
a << 1110012
a >> 100113

Decimal and Binary Values

Here is how the decimal numbers 0 to 12 look in binary (with 16 bits):

 0 = 0000000000000000
 1 = 0000000000000001
 2 = 0000000000000010
 3 = 0000000000000011
 4 = 0000000000000100
 5 = 0000000000000101
 6 = 0000000000000110
 7 = 0000000000000111
 8 = 0000000000001000
 9 = 0000000000001001
10 = 0000000000001010
11 = 0000000000001011
12 = 0000000000001100

Tip: Each step to the left doubles the value. For example, 0000000000000100 is 4 because the third bit from the right is set (22 = 4).


Visualize Bitwise Operators

Choose an operator below to see how it works on a = 6 and b = 3, bit by bit (only the last 8 bits are shown):

Code


Bit by bit

    Output

    Explanation



    Bitwise AND (&)

    The & operator compares each bit and returns 1 only if both bits are 1.

    Example

    int a = 6;   // 0110
    int b = 3;   // 0011
    
    int result = a & b;
    printf("Result: %d\n", result); // 2 (0010)

    Try it Yourself »


    Bitwise OR (|)

    The | operator sets a bit to 1 if either bit is 1.

    Example

    int a = 6;   // 0110
    int b = 3;   // 0011
    
    int result = a | b;
    printf("Result: %d\n", result); // 7 (0111)

    Try it Yourself »


    Bitwise XOR (^)

    The ^ operator returns 1 only when the bits are different.

    Example

    int a = 6;   // 0110
    int b = 3;   // 0011
    
    int result = a ^ b;
    printf("Result: %d\n", result); // 5 (0101)

    Try it Yourself »


    Bitwise NOT (~)

    The ~ operator inverts each bit (0 becomes 1 and 1 becomes 0).

    Example

    int a = 5; // 00000101
    
    int result = ~a;
    printf("Result: %d\n", result); // -6 on most systems

    Try it Yourself »

    Note: The result of ~ depends on how negative numbers are stored (usually two's complement). For example, 5 (00000101) becomes 11111010, which is interpreted as -6.


    Left Shift (<<)

    The << operator shifts bits to the left and fills in 0s on the right. Each shift to the left multiplies the number by 2, so a << 2 multiplies it by 4.

    Example

    int a = 3; // 00000011
    
    int result = a << 2;
    printf("Result: %d\n", result); // 12 (3 * 4)

    Try it Yourself »


    Right Shift (>>)

    The >> operator shifts bits to the right. Each shift to the right divides the number by 2, so a >> 2 divides it by 4. For negative numbers, the result can be different on different systems.

    Example

    int a = 12; // 00001100
    
    int result = a >> 2;
    printf("Result: %d\n", result); // 3 (12 / 4)

    Try it Yourself »


    Real-Life Example: Flags and Permissions

    Bitwise operators are often used to store multiple options in a single integer, using flags.

    Example

    #define READ  1  // 0001
    #define WRITE 2  // 0010
    #define EXEC  4  // 0100
    
    int permissions = READ | WRITE;  // user can read and write
    
    if (permissions & READ) {
      printf("Read allowed\n");
    }
    if (permissions & WRITE) {
      printf("Write allowed\n");
    }
    if (permissions & EXEC) {
      printf("Execute allowed\n");
    }

    Try it Yourself »

    In this example, the user has both READ and WRITE permissions, but not EXEC.

    permissions is 0011. permissions & READ is 0011 & 0001, which gives 0001. That is not 0, so the condition is true. permissions & EXEC is 0011 & 0100, which gives 0000 (false), so "Execute allowed" is not printed.


    Summary

    • Bitwise operators work on individual bits of integers
    • &: AND - both bits must be 1
    • |: OR - either bit can be 1
    • ^: XOR - only one bit is 1
    • ~: NOT - flips all bits
    • <<: Left shift - multiplies by powers of 2
    • >>: Right shift - divides by powers of 2

    Tip: Bitwise operations are useful for optimization, low-level hardware access, flags, and masks.



    Free account

    Track your progress

    XP earned 0 Day streak 0 Spaces 0 League —

    New

    Learn in Adventure Mode W3Schools Adventure App

    Coding fundamentals as bite-sized lessons and challenges.

    ×

    Contact Sales

    If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
    sales@w3schools.com

    Report Error

    If you want to report an error, or if you want to make a suggestion, send us an e-mail:
    help@w3schools.com

    W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

    Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.