From 864c505dafeadd989c367ba7cad8669b1507dfe1 Mon Sep 17 00:00:00 2001 From: Tatheer Fathima <148070120+T-Fathima@users.noreply.github.com> Date: Sun, 10 Nov 2024 03:10:28 +0000 Subject: [PATCH] Added toggle-bit-at-position in bit manipulation --- .../toggle-bit-at-position.md | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 docs/bit-manipulation/toggle-bit-at-position.md diff --git a/docs/bit-manipulation/toggle-bit-at-position.md b/docs/bit-manipulation/toggle-bit-at-position.md new file mode 100644 index 000000000..ea2bfce6c --- /dev/null +++ b/docs/bit-manipulation/toggle-bit-at-position.md @@ -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 +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. \ No newline at end of file