All submissions

Code

#include <stdio.h>

int toggleFifthBit(int n) {
    // Write your code here
    int k= n & 0x20;
    // if k is 0 means bit 5 was 0
    switch (k) {
    case 0:
        return n | 0x20; // bit 5 was zero, set it

    default:
        return n & 0xFFFFFFDF; // bit 5 was set, clear it
    }
}

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

Solving Approach

find out what bit 5 was by  bitwise AND with 0x20, (value of bit 5)

if it was cleared, set it by bitwise AND with 0x20

if it was set, clear it by bitwise AND with the ones complemenet of 0x20

 

 

Loading...

Input

8

Expected Output

40