Pogram to demonstrate bitwise operators
- Write a program to demonstrate bitwise operators in C
- Write a program to demonstrate bitwise operators in C++
- Write a program to demonstrate bitwise operators in Python
- Write a program to demonstrate bitwise operators in PHP
- Write a program to demonstrate bitwise operators in Java
- Write a program to demonstrate bitwise operators in Java Script
- Write a program to demonstrate bitwise operators in C#
Explanation:
Bitwise operators manipulate binary representations of numbers at the bit level. These operators are essential to low-level programming, optimization, and hardware engineering because they allow for direct bit manipulation.
Operator | Name | Description | Example (a = 5, b = 3) |
& | AND | Performs a bitwise AND between two numbers. | 5 & 3 = 1 |
| | OR | OR | Performs a bitwise OR between two numbers. |
^ | XOR | Performs a bitwise exclusive OR between two numbers. | 5 ^ 3 = 6 |
~ | NOT | Flips all the bits (1 becomes 0, 0 becomes 1). | ~5 = -6 |
<< | Left Shift | Shifts the bits of the number to the left by a given amount. | 5 << 1 = 10 |
>> | Left Right | Shifts the bits of the number to the right by a given amount. | 5 >> 1 = 2 |
Pogram to demonstrate bitwise operators
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> int main() { int a = 5, b = 3; // 5 = 0101, 3 = 0011 printf("AND (a & b): %d\n", a & b); printf("OR (a | b): %d\n", a | b); printf("XOR (a ^ b): %d\n", a ^ b); printf("NOT (~a): %d\n", ~a); printf("Left Shift (a << 1): %d\n", a << 1); printf("Right Shift (a >> 1): %d\n", a >> 1); return 0; }
#include <iostream> int main() { int a = 5, b = 3; // 5 = 0101, 3 = 0011 std::cout << "AND (a & b): " << (a & b) << std::endl; std::cout << "OR (a | b): " << (a | b) << std::endl; std::cout << "XOR (a ^ b): " << (a ^ b) << std::endl; std::cout << "NOT (~a): " << (~a) << std::endl; std::cout << "Left Shift (a << 1): " << (a << 1) << std::endl; std::cout << "Right Shift (a >> 1): " << (a >> 1) << std::endl; return 0; }
a, b = 5, 3 # 5 = 0b0101, 3 = 0b0011 print("AND (a & b):", a & b) print("OR (a | b):", a | b) print("XOR (a ^ b):", a ^ b) print("NOT (~a):", ~a) print("Left Shift (a << 1):", a << 1) print("Right Shift (a >> 1):", a >> 1)
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><?php $a = 5; // 0101 $b = 3; // 0011 echo "AND (a & b): " . ($a & $b) . "\n"; echo "OR (a | b): " . ($a | $b) . "\n"; echo "XOR (a ^ b): " . ($a ^ $b) . "\n"; echo "NOT (~a): " . (~$a) . "\n"; echo "Left Shift (a << 1): " . ($a << 1) . "\n"; echo "Right Shift (a >> 1): " . ($a >> 1) . "\n"; ?>
public class BitwiseOperators { public static void main(String[] args) { int a = 5, b = 3; // 5 = 0101, 3 = 0011 System.out.println("AND (a & b): " + (a & b)); System.out.println("OR (a | b): " + (a | b)); System.out.println("XOR (a ^ b): " + (a ^ b)); System.out.println("NOT (~a): " + (~a)); System.out.println("Left Shift (a << 1): " + (a << 1)); System.out.println("Right Shift (a >> 1): " + (a >> 1)); } }
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-javascript">let a = 5; // 0101 let b = 3; // 0011 console.log("AND (a & b):", a & b); console.log("OR (a | b):", a | b); console.log("XOR (a ^ b):", a ^ b); console.log("NOT (~a):", ~a); console.log("Left Shift (a << 1):", a << 1); console.log("Right Shift (a >> 1):", a >> 1);
using System; class BitwiseOperators { static void Main() { int a = 5, b = 3; // 5 = 0101, 3 = 0011 Console.WriteLine("AND (a & b): " + (a & b)); Console.WriteLine("OR (a | b): " + (a | b)); Console.WriteLine("XOR (a ^ b): " + (a ^ b)); Console.WriteLine("NOT (~a): " + (~a)); Console.WriteLine("Left Shift (a << 1): " + (a << 1)); Console.WriteLine("Right Shift (a >> 1): " + (a >> 1)); } }