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

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types 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 Scope C Function Declaration C Recursion C Math Functions

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Nested Structures C Structs & Pointers C Unions C typedef

C Enums

C Enums

C Memory

C Memory Management

C Errors

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

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed Width Integers

C Projects

C Projects

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 Compiler C Syllabus C Study Plan C Certificate

C Bitwise Operators


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 a small reference table showing how decimal numbers look in binary (16-bit format):

 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).


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. This is the same as multiplying by powers of 2.

Example

int a = 3; // 00000011

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

Try it Yourself »


Right Shift (>>)

The >> operator shifts bits to the right. This is the same as dividing by powers of 2. On signed integers, the sign bit may be preserved depending on the system.

Example

int a = 12; // 00001100

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

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.


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.



×

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, cookie and privacy policy.

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