1. Set or Clear a Specific Bit in a Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int main() {
int value, pos, mode;

// Input
scanf("%d", &value);   // 0–255
scanf("%d", &pos);     // 0–7
scanf("%d", &mode);    // 0 or 1

if (mode == 1) {
 // Set bit
value = value | (1 << pos);
} 
else if (mode == 0) {
// Clear bit
value = value & ~(1 << pos);
} 
else {
  printf("Invalid mode");
  return 0;
 }

// Output
printf("%d", value);
 return 0;
                                                                  }

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote