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 GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING

Bitwise Operators

Bitwise operators are used to perform operations on values or variables, one bit at a time.

What is a Bitwise Operator?

A bitwise operator is a symbol or keyword that tells the computer what operation to perform, bit by bit, on values or variables.

See this page for an overview of other types of operators.

The most common bitwise operators are:

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise NOT)
  • << (Left shift)
  • >> (Right shift)

All data in the computer is stored as sequences of 0s and 1s. This makes it possible to use bitwise operators to manipulate the data.

Use the simulation below to click around and see the result of different bitwise operators:

Bitwise AND

result = a & b

a

00001011

11

operator

&

b

00000111

7

result

00000000

0

In the simulation above, the rightmost column shows the decimal values of the variables a, b, and result. The decimal numbers are displayed to give a better understanding of what happens if you do the operation 11 & 7 (using decimal numbers), and why the result is 3.

Left shift <<, and right shift >> operators shift the bits of variable a to the left or right. The number of positions to shift is specified by second variable b, that is why variable b is limited to the first 2 bits in the simulation above.

In the example below, we use the bitwise OR operator | to combine two variables stored in binary format:


Python
JavaScript
Java
C++
a = 0b1010
b = 0b0110
result = a | b
print(bin(result))
const a = 0b1010;
const b = 0b0110;
const result = a | b;
console.log(result.toString(2));
int a = 0b1010;
int b = 0b0110;
int result = a | b;
System.out.println(Integer.toBinaryString(result));
int a = 0b1010;
int b = 0b0110;
int result = a | b;
cout << bitset<4>(result);
Run Example »


×

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.