Skip to content

Commit b212430

Browse files
committed
bitwise algorithm added
1 parent 55a9358 commit b212430

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

docs/dsa/algorithms/bitwise.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
id: bitwise-in-dsa
3+
title: Bitwise Algorithm in Data Structures and Algorithms
4+
sidebar_label: Bitwise Algorithm
5+
sidebar_position: 1
6+
description: ""
7+
tags:
8+
[dsa,data-algorithms , bitwise
9+
]
10+
---
11+
12+
Bitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits within an integer's binary representation to solve problems efficiently. These algorithms use bitwise operators like AND, OR, XOR, NOT, LEFT SHIFT, and RIGHT SHIFT. They are fundamental in low-level programming, encryption, compression, and optimization tasks due to their direct manipulation of data at the bit level.
13+
![bitwise](image-7.png)
14+
15+
### Explanation
16+
Bitwise algorithms operate directly on the binary representation of numbers. For example, the bitwise AND operator (&) compares each bit of two numbers and returns a new number whose bits are set to 1 only if both compared bits are 1. Other operators work similarly, manipulating bits to achieve various outcomes.
17+
18+
## Advantages
19+
- Efficiency: Bitwise operations are extremely fast, as they are directly supported by the processor.
20+
- Low-level control: They provide granular control over data, allowing for optimizations not possible with higher-level abstractions.
21+
- Space-saving: Bitwise operations can be used to compactly store and manipulate data, reducing memory usage.
22+
23+
## Disadvantages
24+
- Readability: Code using bitwise operations can be hard to read and understand, especially for those not familiar with these operations.
25+
- Error-prone: Small mistakes in bitwise operations can lead to significant bugs, as the operations are very low-level.
26+
- Limited applicability: Not all problems benefit from bitwise optimizations; their use is somewhat niche.
27+
28+
## Application in a Problem
29+
Consider the problem of finding the unique number in an array where all numbers except one are repeated twice. A bitwise XOR operation can solve this efficiently.
30+
31+
**Example**
32+
Given an array: [4, 1, 2, 1, 2], find the unique number.
33+
34+
## Step-by-step:
35+
36+
- XOR all the numbers in the array.
37+
- Repeated numbers will cancel each other out because n XOR n = 0.
38+
- The result will be the unique number, as n XOR 0 = n.
39+
40+
## Implementation:
41+
```python
42+
def findUnique(arr):
43+
unique = 0
44+
for num in arr:
45+
unique ^= num
46+
return unique
47+
48+
arr = [4, 1, 2, 1, 2]
49+
print(findUnique(arr)) # Output: 4
50+
```
51+
52+
In this example, the XOR operation is used to find the unique number efficiently. This showcases the power of bitwise operations in solving specific types of problems with minimal code and high performance.
53+
54+
55+
Bitwise algorithms leverage the fundamental operations of bitwise logic to perform calculations directly on the binary representations of numbers. This approach offers a unique blend of efficiency, precision, and low-level data manipulation capabilities that are unmatched by higher-level algorithmic strategies. While they excel in specific domains such as cryptography, data compression, and systems programming, their utility is somewhat specialized, and they require a good understanding of binary arithmetic to be used effectively.

docs/dsa/algorithms/image-7.png

37.7 KB
Loading

0 commit comments

Comments
 (0)