Skip to content

[Add] Bit Manipulation Folder | [GSSoC'23] #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Bit Manipulation/Bitwise AND.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Bitwise AND Operation
The bitwise AND operation is a fundamental operation in computer programming that works on individual bits of binary numbers.

In this operation, if both input bits are 1, the resulting bit will be 1. Otherwise, if either or both of the input bits are 0, the corresponding resulting bit will be 0. Consequently, the bitwise AND operation can be seen as a way to extract common 1-bits from two binary numbers while setting all other bits to 0.

Truth table for **Bitwise AND** Operation: <br>
<p align="center">
<img src="https://github.com/Ashutosh0120/dsa_competitive-coding-GSSOC-2023/assets/24804042/50b90107-8753-4fb2-b012-cc7394673329" alt="Image">
</p>

Example: <br>
If A=10100, B=10101, then
<p align="center">
<img src="https://github.com/Ashutosh0120/dsa_competitive-coding-GSSOC-2023/assets/24804042/96ad669c-477f-40e5-a0cb-dae0816cf899" alt="Image">
</p>

## Code:
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int a=5, b=4;
int c=a&b;
cout<<c<<endl; //AND of a and b
return 0;
}
```
## Output:
```cpp
4 //Since 5&4=4
```
## Complexity:
```
Time Complexity: O(1)
Auxiliary Space: O(1)
```
34 changes: 34 additions & 0 deletions Bit Manipulation/Bitwise NOT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
### Bitwise NOT Operation
The bitwise NOT operation is a unary operation that works on individual bits of a binary number.
Also known as bitwise complement, it flips each bit of the input number, changing 0s to 1s and 1s to 0s. The result is a new binary number with the complement of each bit from the original number.

Truth table for **Bitwise NOT** Operation: <br>
<p align="center">
<img src="https://github.com/Ashutosh0120/dsa_competitive-coding-GSSOC-2023/assets/24804042/ebde6110-7050-4b19-89b5-6484103fa633" alt="Image">
</p>

Example: <br>
If A=10100, then
<p align="center">
<img src="https://github.com/Ashutosh0120/dsa_competitive-coding-GSSOC-2023/assets/24804042/64fbf3b2-e1e6-43b2-987b-97ac591f8d9a" alt="Image">
</p>

## Code:
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int a=5, b=~a;
cout<<b<<endl; //NOT of a
return 0;
}
```
## Output:
```cpp
1 //Since ~5=1
```
## Complexity:
```
Time Complexity: O(1)
Auxiliary Space: O(1)
```
36 changes: 36 additions & 0 deletions Bit Manipulation/Bitwise OR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Bitwise OR Operation
The bitwise OR operation is a fundamental operation in computer programming that works on individual bits of binary numbers.

In this operation, if at least one of the input bits is 1, the resulting bit will be 1. Only when both input bits are 0 will the corresponding resulting bit be 0. Consequently, the bitwise OR operation can be seen as a way to combine the 1-bits from two binary numbers, while setting all other bits to 0 if they are both 0.

Truth table for **Bitwise OR** Operation: <br>
<p align="center">
<img src="https://github.com/Ashutosh0120/dsa_competitive-coding-GSSOC-2023/assets/24804042/2dfb1b8c-b0dc-4e06-ac15-d8ebab65b3d4" alt="Image">
</p>

Example: <br>
If A=10100, B=10101, then
<p align="center">
<img src="https://github.com/Ashutosh0120/dsa_competitive-coding-GSSOC-2023/assets/24804042/8176525d-5180-4d70-8bf1-42e72db27b8d" alt="Image">
</p>

## Code:
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int a=5, b=4;
int c=a|b;
cout<<c<<endl; //OR of a and b
return 0;
}
```
## Output:
```cpp
5 //Since 5|4=5
```
## Complexity:
```
Time Complexity: O(1)
Auxiliary Space: O(1)
```
46 changes: 46 additions & 0 deletions Bit Manipulation/Bitwise Shift Operation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
### Bitwise Shift Operation
The bitwise shift operations are used to move the individual bits of a binary number left or right. There are two types of bitwise shift operations: bitwise left shift (<<) and bitwise right shift (>>). These operations are often used in low-level programming to perform efficient multiplication and division by powers of 2, as well as other bit manipulation tasks.

1. **Bitwise Left Shift (<<)**:
The bitwise left shift operation moves all the bits of a binary number to the left by a specified number of positions. The leftmost bits that are shifted out are discarded, and the rightmost bits are filled with zeros.

For example, if we perform a left shift of 2 positions on the binary number 00101110, the result will be:
```cpp
Original binary number: 00101110
Left shift by 2: 10111000
```

2. **Bitwise Right Shift (>>)**:
The bitwise right shift operation moves all the bits of a binary number to the right by a specified number of positions. The rightmost bits that are shifted out are discarded, and the leftmost bits are filled based on the sign bit (for signed integers) or with zeros (for unsigned integers).

For example, if we perform a right shift of 3 positions on the binary number 11011010, the result will be:
```cpp
Original binary number: 11011010
Right shift by 3: 00011011
```

## Code:
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int a=5;
a=a>>1;
int b=7;
b=b<<1;
cout<<a<<endl;
cout<<b<<endl;
return 0;
}
```
## Output:
```cpp
2 // 5>>1= 2
14 // 7<<1= 14

```
## Complexity:
```
Time Complexity: O(1)
Auxiliary Space: O(1)
```
31 changes: 31 additions & 0 deletions Bit Manipulation/Check kth bit set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
### Check kth bit set or not
In order to check whether the kth bit is set or not, we use below 2 methods: <br>
1. **Right Shift Operation** : To check the kth bit, you can left-shift the number by (k-1) positions and then perform a bitwise AND operation with 1. <br>
If the result is non-zero, it means the kth bit is set (1); otherwise, the kth bit is not set (0).<br>
```cpp
#include<bits/stdc++.h>
using namespace std;
int main() {
int n = 5, k = 3;
while(k--){
n=n>>1;
}
if ((n & 1) != 0) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
```

2. **Bitwise Mask** : Alternatively, you can create a bitmask with only the kth bit set (1), and then perform a bitwise AND operation with the number. If the result is non-zero, the kth bit is set (1); otherwise, it is not set (0). <br>

```cpp
#include<bits/stdc++.h>
using namespace std;
int main() {
int n = 5, k = 3;
int m = 1 << (k - 1);
return (n & m) != 0;
}
```