Bitwise AND Operator (&)
The bitwise AND operator is denoted using a single ampersand symbol, i.e. &. The & operator takes two equal-length bit patterns as parameters. The two-bit integers are compared. If the bits in the compared positions of the bit patterns are 1, then the resulting bit is 1. If not, it is 0.
Example:
Take two bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 . Take Bitwise and of both X & y
Implementation of AND operator:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 7, b = 4;
int result = a & b;
cout << result << endl;
return 0;
}
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
int a = 7, b = 4;
int result = a & b;
System.out.println(result);
}
}
// This code is contributed by lokeshmvs21.
a = 7
b = 4
result = a & b
print(result)
# This code is contributed by akashish__
using System;
public class GFG{
static public void Main (){
int a = 7, b = 4;
int result = a & b;
Console.WriteLine(result);
}
}
// This code is contributed by akashish__
let a = 7, b = 4;
let result = a & b;
console.log(result);
// This code is contributed by akashish__
Output
4
Time Complexity: O(1)
Auxiliary Space: O(1)
Introduction to Bitwise Algorithms – Data Structures and Algorithms Tutorial
Bit stands for binary digit. A bit is the basic unit of information and can only have one of two possible values that is 0 or 1. In our world, we usually with numbers using the decimal base. In other words. we use the digit 0 to 9 However, there are other number representations that can be quite useful such as the binary number systems.
Unlike humans, computers have no concepts of words and numbers. They receive data encoded at the lowest level as a series of zeros and ones (0 and 1). These are called bits, and they are the basis for all the commands they receive. We’ll begin by learning about bits and then explore a few algorithms for manipulating bits. We’ll then explore a few algorithms for manipulating bits. The tutorial is meant to be an introduction to bit algorithms for programmers.
Table of Content
- What is Bitwise Algorithms?
- Bitwise Operators / Basics of Bit manipulation
- Bitwise AND Operator (&)
- Bitwise OR Operator (|)
- Bitwise XOR Operator (^)
- Bitwise NOT Operator (!~)
- Left-Shift (<<)
- Right-Shift (>>)
- Application of Bit Operators
- Important Practice Problems on Bitwise Algorithm
Contact Us