2. Bit Toggle

Discussions3
Log in to post comments and replies.
You
MartzZ
MartzZApr 09 2026

Bit toggle of 31 isn't 95, it's 63. 
The example with input of 31 is wrong, toggling the 6th bit. That got me stuck in a figure it out loop for a good while lol

Please fix this

0
MohanKilari
MohanKilariDec 05 2025
#include <stdio.h>

int toggleFifthBit(int n) {
    // Write your code here
    n ^= (1 << 5);
    return n;
}

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

When i use uint8_t instead of int.. the expected output is failed ... when the input is 31... what is the reason

+4
GauthamShankar
GauthamShankarMar 04 2026

uint8_t has only 8 bits, but bit index 31 is out of range, so 1U << 31 is 32-bit, assigning back to 8-bit truncates and hence the toggling fails

0