2. Bit Toggle

Discussions3
Log in to post comments and replies.
You
Loading editor...
MartzZ
MartzZ
Jul 22 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

+2
MohanKilari
MohanKilari
Jul 31 2026
#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
MATHESHVARMAJ
Jul 30 2026

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

+7
GauthamShankar
GauthamShankar
Jul 29 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

+2