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

Code

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

uint64_t power(int num, int exp)
{
    uint64_t result = 1;
    while(exp--)
    {
        result *= num;
    }
    return result;
}
void print_binary(uint16_t val) {
    // Your logic here
    uint16_t temp = val;
    uint64_t num = 0;
    int exp =0;
    while(temp)
    {
        uint16_t t = temp %2; 
        num += t*power(10,exp);
        temp /= 2;
        exp++;
    }
    if(val > 255)
    printf("%016llu",num);
    else
    printf("%08u",num);
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

10

Expected Output

00001010