Print Binary Representation of an 8-bit or 16-bit Value

nice task

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

void print_binary(uint16_t val) {
    int i=15,vt;
    if(val<=255) vt=8;
    else vt=0;
    char s[16]={'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};   
        while(val>0)
        {
            s[i]= (val & 1) + '0';
            val>>=1;
            i--;
        }
    for(int j=vt; j<16; j++)
     printf("%c", s[j]);

}

int main() {
    uint16_t val;
    scanf("%hu", &val);
    print_binary(val);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

10

Expected Output

00001010