Set or Clear a Specific Bit in a Register

Code

#include <stdio.h>

int main() {
    unsigned char reg;  // 8-bit register (0-255)
        int bit_pos, mode;

            scanf("%hhu %d %d", &reg, &bit_pos, &mode);

                if (mode == 1)
                        reg |= (1 << bit_pos);     // Set the bit
                            else
                                    reg &= ~(1 << bit_pos);    // Clear the bit

                                        printf("%u\n", reg);
                                            return 0;
                                            }

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

10 3 1

Expected Output

10