Code

#include <stdio.h>

int toggleFifthBit(int n) {
    // Create a mask with only the 5th bit set (0-based index)
    // 1 << 5 shifts the bit 1 five positions to the left, resulting in 00100000 (binary)
    int mask = 1 << 5; 
    
    // Perform a bitwise XOR operation with the mask
    // XORing with 1 toggles the bit (0 becomes 1, 1 becomes 0)
    // XORing with 0 leaves the bit unchanged
    return n ^ mask;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

8

Expected Output

40