All submissions

Code

#include <stdio.h>
/*
Plan:
1. Move to the 5th position
2. Check if the bit is set or !set
3. Flip the bit
(n >> 5) & 1U -> give the 5th bit
if its 0 then n = n | (1U<<5)
if its 1 then n = n & ~(1U<<5)

*/
int toggleFifthBit(int n) {
    // Write your code here
    // return ((n>>5) & 1U) ? (n & ~(1U<<5)) : (n | (1U<<5)); // First solution
    return (n ^ (1U<<5)); // More optimized solution
}

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

Solving Approach

 

 

 

Loading...

Input

8

Expected Output

40