Program for Bitwise Operators in C, C++, Java, Python, C# & JavaScript
Last Updated :
23 Jul, 2025
Bitwise operators are fundamental operators used in computer programming for manipulating individual data bits. They operate at the binary level, performing operations on binary data representations. These operators are commonly used in low-level programming, such as device drivers, embedded systems, and cryptography, where manipulation at the bit level is required. They can also be used for optimizing certain algorithms and operations where bitwise manipulation can provide faster execution compared to conventional arithmetic operations.
What are Bitwise Operators?
Bitwise operators in programming perform operations at the bit level, manipulating individual bits of binary representations of numbers. These operators are often used in low-level programming, such as embedded systems and device drivers.
Types of Bitwise Operators:
- Bitwise AND (&): Compares corresponding bits of two operands. If both bits are 1, the result is 1; otherwise, it's 0.
- Bitwise OR (|): Compares corresponding bits of two operands. If either bit is 1, the result is 1; otherwise, it's 0.
- Bitwise XOR (^): Compares corresponding bits of two operands. If the bits are different, the result is 1; if they are the same, the result is 0.
- Bitwise NOT (~): Flips the bits of its operand. If a bit is 0, it becomes 1, and if it's 1, it becomes 0.
- Left Shift (<<): Shifts the bits of its first operand to the left by a number of positions specified by the second operand.
- Right Shift (>>): Shifts the bits of its first operand to the right by a number of positions specified by the second operand.
Below is a table summarizing common bitwise operators along with their symbols, description:
Operator | Description |
---|
& | Bitwise AND operator |
| | Bitwise OR operator |
^ | Bitwise XOR (exclusive OR) operator |
~ | Bitwise NOT (complement) operator |
<< | Bitwise left shift operator |
>> | Bitwise right shift operator |
Bitwise Operator in C:
Here are the implementation of Bitwise Operator in C language:
C
#include <stdio.h>
int main() {
int num1 = 10; // Binary representation: 1010
int num2 = 5; // Binary representation: 0101
// Bitwise AND (&)
int result_and = num1 & num2; // Result: 0000 (decimal 0)
printf("Bitwise AND: %d\n", result_and);
// Bitwise OR (|)
int result_or = num1 | num2; // Result: 1111 (decimal 15)
printf("Bitwise OR: %d\n", result_or);
// Bitwise XOR (^)
int result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
printf("Bitwise XOR: %d\n", result_xor);
// Bitwise NOT (~) - Unary operator
int result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
printf("Bitwise NOT (num1): %d\n", result_not1);
int result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
printf("Bitwise NOT (num2): %d\n", result_not2);
// Bitwise Left Shift (<<)
int result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
printf("Bitwise Left Shift: %d\n", result_left_shift);
// Bitwise Right Shift (>>)
int result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
printf("Bitwise Right Shift: %d\n", result_right_shift);
return 0;
}
OutputBitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5
Bitwise Operator in C++:
Here are the implementation of Bitwise Operator in C++ language:
C++
#include <iostream>
using namespace std;
int main() {
int num1 = 10; // Binary representation: 1010
int num2 = 5; // Binary representation: 0101
// Bitwise AND (&)
int result_and = num1 & num2; // Result: 0000 (decimal 0)
cout << "Bitwise AND: " << result_and << endl;
// Bitwise OR (|)
int result_or = num1 | num2; // Result: 1111 (decimal 15)
cout << "Bitwise OR: " << result_or << endl;
// Bitwise XOR (^)
int result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
cout << "Bitwise XOR: " << result_xor << endl;
// Bitwise NOT (~) - Unary operator
int result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
cout << "Bitwise NOT (num1): " << result_not1 << endl;
int result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
cout << "Bitwise NOT (num2): " << result_not2 << endl;
// Bitwise Left Shift (<<)
int result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
cout << "Bitwise Left Shift: " << result_left_shift << endl;
// Bitwise Right Shift (>>)
int result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
cout << "Bitwise Right Shift: " << result_right_shift << endl;
return 0;
}
OutputBitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5
Bitwise Operator in Java:
Here are the implementation of Bitwise Operator in Java language:
Java
public class Main {
public static void main(String[] args) {
int num1 = 10; // Binary representation: 1010
int num2 = 5; // Binary representation: 0101
// Bitwise AND (&)
int result_and = num1 & num2; // Result: 0000 (decimal 0)
System.out.println("Bitwise AND: " + result_and);
// Bitwise OR (|)
int result_or = num1 | num2; // Result: 1111 (decimal 15)
System.out.println("Bitwise OR: " + result_or);
// Bitwise XOR (^)
int result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
System.out.println("Bitwise XOR: " + result_xor);
// Bitwise NOT (~) - Unary operator
int result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
System.out.println("Bitwise NOT (num1): " + result_not1);
int result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
System.out.println("Bitwise NOT (num2): " + result_not2);
// Bitwise Left Shift (<<)
int result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
System.out.println("Bitwise Left Shift: " + result_left_shift);
// Bitwise Right Shift (>>)
int result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
System.out.println("Bitwise Right Shift: " + result_right_shift);
}
}
OutputBitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5
Bitwise Operator in Python:
Here are the implementation of Bitwise Operator in Python language:
Python
num1 = 10 # Binary representation: 1010
num2 = 5 # Binary representation: 0101
# Bitwise AND (&)
result_and = num1 & num2 # Result: 0000 (decimal 0)
print("Bitwise AND:", result_and)
# Bitwise OR (|)
result_or = num1 | num2 # Result: 1111 (decimal 15)
print("Bitwise OR:", result_or)
# Bitwise XOR (^)
result_xor = num1 ^ num2 # Result: 1111 (decimal 15)
print("Bitwise XOR:", result_xor)
# Bitwise NOT (~) - Unary operator
result_not1 = ~num1 # Result: 1111 0101 (decimal -11 in 2's complement)
print("Bitwise NOT (num1):", result_not1)
result_not2 = ~num2 # Result: 1111 1010 (decimal -6 in 2's complement)
print("Bitwise NOT (num2):", result_not2)
# Bitwise Left Shift (<<)
result_left_shift = num1 << 1 # Result: 10100 (decimal 20)
print("Bitwise Left Shift:", result_left_shift)
# Bitwise Right Shift (>>)
result_right_shift = num1 >> 1 # Result: 0101 (decimal 5)
print("Bitwise Right Shift:", result_right_shift)
Output('Bitwise AND:', 0)
('Bitwise OR:', 15)
('Bitwise XOR:', 15)
('Bitwise NOT (num1):', -11)
('Bitwise NOT (num2):', -6)
('Bitwise Left Shift:', 20)
('Bitwise Right Shift:', 5)
Bitwise Operator in C#:
Here are the implementation of Bitwise Operator in C# language:
C#
using System;
class MainClass {
public static void Main (string[] args) {
int num1 = 10; // Binary representation: 1010
int num2 = 5; // Binary representation: 0101
// Bitwise AND (&)
int result_and = num1 & num2; // Result: 0000 (decimal 0)
Console.WriteLine("Bitwise AND: " + result_and);
// Bitwise OR (|)
int result_or = num1 | num2; // Result: 1111 (decimal 15)
Console.WriteLine("Bitwise OR: " + result_or);
// Bitwise XOR (^)
int result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
Console.WriteLine("Bitwise XOR: " + result_xor);
// Bitwise NOT (~) - Unary operator
int result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
Console.WriteLine("Bitwise NOT (num1): " + result_not1);
int result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
Console.WriteLine("Bitwise NOT (num2): " + result_not2);
// Bitwise Left Shift (<<)
int result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
Console.WriteLine("Bitwise Left Shift: " + result_left_shift);
// Bitwise Right Shift (>>)
int result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
Console.WriteLine("Bitwise Right Shift: " + result_right_shift);
}
}
OutputBitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5
Bitwise Operator in JavaScript:
Here are the implementation of Bitwise Operator in Javascript language:
JavaScript
let num1 = 10; // Binary representation: 1010
let num2 = 5; // Binary representation: 0101
// Bitwise AND (&)
let result_and = num1 & num2; // Result: 0000 (decimal 0)
console.log("Bitwise AND:", result_and);
// Bitwise OR (|)
let result_or = num1 | num2; // Result: 1111 (decimal 15)
console.log("Bitwise OR:", result_or);
// Bitwise XOR (^)
let result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
console.log("Bitwise XOR:", result_xor);
// Bitwise NOT (~) - Unary operator
let result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
console.log("Bitwise NOT (num1):", result_not1);
let result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
console.log("Bitwise NOT (num2):", result_not2);
// Bitwise Left Shift (<<)
let result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
console.log("Bitwise Left Shift:", result_left_shift);
// Bitwise Right Shift (>>)
let result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
console.log("Bitwise Right Shift:", result_right_shift);
OutputBitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5
Application of Bitwise Operators:
Bitwise operators are commonly used in various scenarios, including:
- Setting or clearing specific bits within binary data.
- Checking the status of flags or bit fields.
- Extracting or packing values from or into binary data structures.
- Performing arithmetic operations in a more optimized way when dealing with low-level programming, such as manipulating hardware registers or implementing cryptographic algorithms.
Important points about Bitwise Operators:
- Bitwise operators work at the binary level, which is the lowest level of data representation in computers.
- They are typically used in scenarios where fine-grained control over individual bits is necessary, such as low-level programming, cryptography, and hardware interfacing.
- Understanding bitwise operations is crucial for writing efficient code and implementing certain algorithms.
Conclusion:
Bitwise operators are commonly used in low-level programming, such as device drivers, embedded systems, and cryptography, where manipulation at the bit level is required. They can also be used for optimizing certain algorithms and operations where bitwise manipulation can provide faster execution compared to conventional arithmetic operations.
Similar Reads
Computer Fundamentals Tutorial This Computer Fundamentals Tutorial covers everything from basic to advanced concepts, including computer hardware, software, operating systems, peripherals, etc. Whether you're a beginner or an experienced professional, this tutorial will enhance your computer skills and take them to the next level
4 min read
Fundamental
Computer HardwareComputer hardware refers to the physical components of a computer that you can see and touch. These components work together to process input and deliver output based on user instructions. In this article, weâll explore the different types of computer hardware, their functions, and how they interact
10 min read
What is a Computer Software?Computer Software serves as the backbone of all digital devices and systems. It is an integral part of modern technology. Unlike hardware which comprises physical components, software is intangible and exists as a code written in programming language. This article focuses on discussing computer soft
8 min read
Central Processing Unit (CPU)The Central Processing Unit (CPU) is like the brain of a computer. Itâs the part that does most of the thinking, calculating, and decision-making to make your computer work. Whether youâre playing a game, typing a school assignment, or watching a video, the CPU is busy handling all the instructions
6 min read
Input DevicesInput devices are important parts of a computer that help us communicate with the system. These devices let us send data or commands to the computer, allowing it to process information and perform tasks. Simply put, an input device is any tool we use to give the computer instructions, whether it's t
11 min read
Output DevicesOutput devices are hardware that display or produce the results of a computer's processing. They convert digital data into formats we can see, hear, or touch. The output device may produce audio, video, printed paper or any other form of output. Output devices convert the computer data to human unde
9 min read
Memory
Computer MemoryMemory is the electronic storage space where a computer keeps the instructions and data it needs to access quickly. It's the place where information is stored for immediate use. Memory is an important component of a computer, as without it, the system wouldnât operate correctly. The computerâs opera
9 min read
What is a Storage Device? Definition, Types, ExamplesThe storage unit is a part of the computer system which is employed to store the information and instructions to be processed. A storage device is an integral part of the computer hardware which stores information/data to process the result of any computational work. Without a storage device, a comp
11 min read
Primary MemoryPrimary storage or memory is also known as the main memory, which is the part of the computer that stores current data, programs, and instructions. Primary storage is stored in the motherboard which results in the data from and to primary storage can be read and written at a very good pace.Need of P
4 min read
Secondary MemorySecondary memory, also known as secondary storage, refers to the storage devices and systems used to store data persistently, even when the computer is powered off. Unlike primary memory (RAM), which is fast and temporary, secondary memory is slower but offers much larger storage capacities. Some Ex
7 min read
Hard Disk Drive (HDD) Secondary MemoryPrimary memory, like RAM, is limited and volatile, losing data when power is off. Secondary memory solves this by providing large, permanent storage for data and programs.A hard disk drive (HDD) is a fixed storage device inside a computer that is used for long-term data storage. Unlike RAM, HDDs ret
11 min read
Application Software
MS Word Tutorial - Learn How to Use Microsoft Word (2025 Updated)Microsoft Word remains one of the most powerful word processing program in the world. First released in 1983, this word processing software has grown to serve approximately 750 million people every month. Also, MS Word occupies 4.1% of the market share for productivity software.With features like re
9 min read
MS Excel Tutorial - Learn Excel Online FreeExcel, one of the powerful spreadsheet programs for managing large datasets, performing calculations, and creating visualizations for data analysis. Developed and introduced by Microsoft in 1985, Excel is mostly used in analysis, data entry, accounting, and many more data-driven tasks.Now, if you ar
11 min read
What is a Web Browser and How does it Work?The web browser is an application software used to explore the World Wide Web (WWW). It acts as a platform that allows users to access information from the Internet by serving as an interface between the client (user) and the server. The browser sends requests to servers for web documents and servic
4 min read
What is a Excel SpreadsheetExcel works like other spreadsheet programs but offers more features. Each Excel file is called a workbook, which contains one or more worksheets. You can start with a blank workbook or use a template.A worksheet is a grid of 1,048,576 rows and 16,384 columns, over 17 billion cells, for entering and
7 min read
System Software
Programming Languages
C Programming Language TutorialC is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Python Tutorial - Learn Python Programming LanguagePython is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Java TutorialJava is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
JavaScript TutorialJavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read