Code

#include <stdio.h>

int toggleFifthBit(int n) {
    // Write your code here
    int bit5 = n & (1<<5); // basically triggers if 0 or 1 for 5th bit
    // originally i had  int bit5 = n & (00100000); which doesnt work because c doesnt understand 00100000 to be binary...
    if (bit5 == 0) {    
        n |= (1<<5); //set 5th bit to 1        
    }
    else { 
        n &= ~(1<<5);} //clear 5th bit
    return n;
    
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

8

Expected Output

40