All submissions

1 Line Solution

#include <stdio.h>

int toggleFifthBit(int n) {
    return n ^ 0x20;
}

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

Approach

From the XOR truth table, we can find that any bit that gets XORed with a 0 keeps its value, and any bit that gets XORed with 1 gets flipped.

ABA XOR B
0XX
1X!X

 

Therefore to toggle the Xth bit, all we have to do is to evaluate N XOR (1 << X) where N is our input.

Since X is always 5 in this problem, we can hard code (1 << 5) to 0x20 and then all we have to do is return N XOR 0x20.

Loading...

Input

8

Expected Output

40