#include <stdio.h>
#include <stdint.h>
void print_binary(uint16_t val) {
// Your logic here
uint16_t k, mask=0x8000;
uint8_t i;
uint8_t printsize;
if ((val & 0xFF00) == 0) {
printsize=8;
mask = 0x80;
}
else {
printsize = 16;
mask = 0x8000;
}
for (i=0; i < printsize; i++) {
k= mask & val;
if (k == 0) {
printf("%c", '0');
}
else {
printf("%c", '1');
}
mask = mask >> 1;
}
}
int main() {
uint16_t val;
scanf("%hu", &val);
print_binary(val);
return 0;
}
check th eupper byte to see if we print 8 or 16 bits.
using a rolling mask to identify the bits and print them as characters.
Input
10
Expected Output
00001010