Bit Toggle

durgaramprasadtula
durgaramprasadtula

Code

#include <stdio.h>

int toggleFifthBit(int n) {
    return n ^ (1 << 5); // Toggle 5th bit (bit position 5)
}

int main() {
    int n;
    scanf("%d", &n);
    printf("%d", toggleFifthBit(n));
    return 0;
}

Solving Approach

  1. Input: Integer N (can be any non-negative value)
  2. Toggle the 5th bit using XOR operation:
    1. Create a mask for bit 5 → 1 << 5 → 0b00100000 → 32
    2. Use XOR: N ^ (1 << 5) → This flips bit 5
  3. Output the modified integer

 

Loading...

Input

8

Expected Output

40