2. Bit Toggle

Discussions6
Log in to post comments and replies.
nvietanh28
nvietanh28
Sep 08 2026

int toggleFifthBit(int n) {

    return n ^ (1 << 5);

}

+1
Ramachandru
Ramachandru
Aug 15 2026
#include <stdio.h>

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

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

#include <stdio.h>


 

int toggleFifthBit(int n) {

    // Write your code here

    n= n^(1<<5);

    return n;

}


 

int main() {

    int n;

    scanf("%d", &n);

    printf("%d", toggleFifthBit(n));

    return 0;

}

0
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