1. Set or Clear a Specific Bit in a Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>
#include <stdint.h>

void setorclear(uint8_t* reg, uint8_t pos, uint8_t mode) {
	if (mode == 1) {
		*reg |= (1 << pos);
	}
	else {
		*reg &= ~(1 << pos);
	}
}

int main() {
	uint8_t reg,pos,mode;
	scanf("%hhu%hhu%hhu", &reg,&pos,&mode);
	setorclear(&reg, pos, mode);
	printf("%hhu", reg);
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote