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) {
    // Your logic here
    if(val <= 255){
        for(int i = 8 - 1; i >= 0; i--){
            int bit = (val >> i) & 1;
            printf("%d",bit);

            // if(i % 4 == 0 && i != 0){
            //     printf(" ");
            // }
        }
    }

    else{
        for(int i = 16 - 1; i >= 0; i--){
            int bit = (val >> i) & 1;
            printf("%d",bit);

            // if(i % 4 == 0 && i != 0){
            //     printf(" ");
            // }
        }
    }


}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote