17. Extract the Nibble from an 8-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

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


union{
unsigned char reg;
struct {
    uint8_t lower_nibble: 4;
    uint8_t upper_nibble: 4;
}reg_t;
}reg_u;
int main() {
    
    int pos;
    scanf("%hhu %d", &reg_u.reg, &pos);

    if(pos){
    printf("%d", reg_u.reg_t.upper_nibble);
    }
    else{
    printf("%d", reg_u.reg_t.lower_nibble);
    }
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote