#include <stdio.h>
#include <stdint.h>
void decode_status(uint8_t status_reg) {
uint8_t res = 0b00000001; // bit cao nhất (MSB)
int cnt = 0;
while (cnt < 8) { // kiểm tra đủ 8 bit
if (status_reg & res) { // nếu bit hiện tại là 1
if (cnt == 0) printf("Power On\n");
if (cnt == 1) printf("Error\n");
if (cnt == 2) printf("Tx Ready\n");
if (cnt == 3) printf("Rx Ready\n");
if (cnt == 4) printf("Overheat\n");
if (cnt == 5) printf("Undervoltage\n");
if (cnt == 6) printf("Timeout\n");
if (cnt == 7) printf("Reserved\n");
}
res = res << 1; // dịch sang bit thấp hơn
cnt++;
}
}
int main() {
uint8_t reg;
scanf("%hhu", ®);
decode_status(reg);
return 0;
}
Solving Approach
Input
13
Expected Output
Power On Tx Ready Rx Ready