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

Back To All Submissions
Previous Submission
Next Submission

Code

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

void print_binary(uint16_t val) {
    int start = (val < 256) ? 7 : 15;

    while (start >= 0) {
        int bit = (val >> start) & 1;
        printf("%u", bit);
        start--;
    }
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote