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).
#include <stdio.h>
unsigned char modifyBit(unsigned char reg, int pos, int mode) {
// Write your code here
return ((mode)? (reg | (1 << pos)) : (reg & ~(1 << pos)));
}
int main() {
unsigned char reg;
int pos, mode;
scanf("%hhu %d %d", ®, &pos, &mode);
printf("%d", modifyBit(reg, pos, mode));
return 0;
}
Input
10 3 1
Expected Output
10