4. Set the Bit in an 8-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

// Function to set a bit
unsigned char setBit(unsigned char reg, int pos) {
    return reg | (1 << pos);
}

int main() {
    unsigned char reg;
    int pos;

    // Input
    scanf("%hhu %d", &reg, &pos);

    // Set the bit
    reg = setBit(reg, pos);

    // Output
    printf("%hhu", reg);

    return 0;
}

Solving Approach
 

 

 

Was this helpful?
Upvote
Downvote