All submissions

Set or Clear a Specific Bit in a Register

Chill Code

#include <stdio.h>

unsigned char modifyBit(unsigned char reg, int pos, int mode) {
    return ((mode)? (reg | (1 << pos)) : (reg & ~(1 << pos)));
    // got same logic but implemented same as yash bhai (he knows that)
}

int main() {
    unsigned char reg;
    int pos, mode;
    scanf("%hhu %d %d", &reg, &pos, &mode);
    printf("%d", modifyBit(reg, pos, mode));
    return 0;
}

Solving Approach

  • Ternary Operator (condition ? expr1 : expr2) is often marginally faster because it's a single expression and can be optimized more aggressively by compilers/interpreters.
  • if-else Statement might involve slightly more overhead, especially in interpreted languages like Python or JavaScript, because it involves block-level control flow.

⚠️ However: The difference is usually negligible unless this is happening millions of times per second in a performance-critical section (like inside a tight loop).

  • Learn from Yash bhai

 

Loading...

Input

10 3 1

Expected Output

10