Toggle the Bit in an 8-bit Register

Code

#include <stdio.h>

// Function to toggle a bit at a given position
unsigned char toggleBit(unsigned char reg, int pos) {
    return reg ^ (1 << pos);  // Toggle the bit without affecting other bits
    }

    int main() {
        unsigned char reg;
            int pos;

                scanf("%hhu %d", &reg, &pos);

                    unsigned char updatedReg = toggleBit(reg, pos);
                        printf("%u\n", updatedReg);  // Print the updated register value

                            return 0;
                            }

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

6 1

Expected Output

4