Skip to content

Commit

Permalink
Merge pull request #1995 from T-Fathima/branch_1
Browse files Browse the repository at this point in the history
Added toggle-bit-at-position in bit manipulation
  • Loading branch information
ajay-dhangar authored Nov 10, 2024
2 parents a115162 + 864c505 commit 0442636
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions docs/bit-manipulation/toggle-bit-at-position.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
id: toggle-bit-at-position
title: Toggle a Bit at a Given Position
sidebar_label: Bit Toggle Operation
sidebar_position: 4
Description: The Bit Toggle Operation is a bit manipulation technique to flip a specific bit at a given position in the binary representation of an integer. This is useful in applications where selective bit modification is required, such as graphics, cryptography, and low-level hardware operations.

Tags: [dsa, bit manipulation, algorithm, toggle bit, binary operations]
---
# Toggling a Bit at a Specified Position Using Bit Manipulation

## Introduction

The **Bit Toggle Operation** allows flipping a bit at a given position in the binary form of an integer. This technique is efficient and widely used in fields requiring precise control over binary data, such as in graphics processing, low-level systems programming, and cryptographic algorithms.

## How it Works

The idea behind toggling a bit is to use the XOR operation. By XORing a number with a bitmask where only the target bit is set, the bit at that position flips its state. This technique is efficient and performs in constant time.

### Steps in the Algorithm:
1. Generate a mask by shifting 1 to the left by `pos` positions (where `pos` is the bit position to toggle).
2. XOR the number `n` with the mask.
3. The result is the number `n` with the bit at position `pos` toggled.

### Example Walkthrough

Let's take an example where `n = 10` (binary `1010`) and `pos = 1`.

- **Binary representation of n**:
`n = 1010`
- Generate the mask:
`1 << 1 = 0010`
- XOR operation:
`n ^ (1 << 1) = 1010 ^ 0010 = 1000`

The result is `8` (binary `1000`), where the bit at position `1` has been toggled.

### Edge Case Example

Consider toggling the bit at position `0` in `n = 5` (binary `0101`):

- **Binary representation of n**:
`n = 0101`
- Generate the mask:
`1 << 0 = 0001`
- XOR operation:
`n ^ (1 << 0) = 0101 ^ 0001 = 0100`

The result is `4` (binary `0100`), where the bit at position `0` has been toggled.

### C++ Implementation

```cpp
#include <iostream>
using namespace std;

// Function to toggle the bit at a given position
int toggleBit(int n, int pos) {
return n ^ (1 << pos);
}

int main() {
// Testing the function
int n = 10, pos = 1;
cout << "Original number: " << n << endl;
int toggled = toggleBit(n, pos);
cout << "Number after toggling bit at position " << pos << ": " << toggled << endl;
return 0;
}
```
### Python Implementation
```python
def toggle_bit(n, pos):
return n ^ (1 << pos)
# Testing the function
n = 10
pos = 1
print("Original number:", n)
toggled = toggle_bit(n, pos)
print(f"Number after toggling bit at position {pos}: {toggled}")
```

# Time Complexity
The time complexity of this operation is O(1) since it involves a single bitwise operation.

# Why It's Efficient
Toggling a bit using XOR is highly efficient because it allows us to change the bit state without needing conditional checks or additional operations. This method is commonly used in scenarios where quick bit modification is required.

# Conclusion
The bit toggle operation is a simple yet powerful technique in bit manipulation, enabling rapid state changes in binary data. This approach is essential for efficient handling of binary data in various applications, from hardware programming to data encryption.

0 comments on commit 0442636

Please sign in to comment.