Thread: Print decimal in 32 bit binary

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    1

    Post Print decimal in 32 bit binary

    Hello my exercise is to convert a decimal to binary. but it has to give out as 32 bit. For example:
    ---
    value: 5
    5 = 0b00000000000000000000000000000101
    ---
    I know how to convert it to binary but i dont no how to print it, that it looks like the example above.
    Code:
    This is my code:
    #include <inttypes.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void print_binary(uint32_t value){
        if (value == 0)
        {
            printf("%" PRIu32, 0);
            return;
        }
        else {
            print_binary(value/2);
            printf("%" PRIu32,(value%2));
        }
        }
    
    
    int main (void){
    
    
        uint32_t value;
        
        scanf("%" SCNu32, &value);
        printf("value: \n%u = ",value);
        print_binary(value);
    
    
        return EXIT_SUCCESS;
    }
    Last edited by free44mind; 11-21-2021 at 06:41 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,885
    You'll need a for loop, say 32 iterations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print new line using 10 in decimal
    By FernandoBasso in forum C Programming
    Replies: 2
    Last Post: 12-30-2011, 05:52 AM
  2. Print to hyperterminal in Hexa (Or Decimal)
    By pedropbr in forum C Programming
    Replies: 2
    Last Post: 11-03-2010, 10:03 PM
  3. Unable to print decimal part of the value. Can someone help?
    By matthayzon89 in forum C Programming
    Replies: 2
    Last Post: 09-18-2010, 10:55 PM
  4. %g doesn't print as many decimal places as %f
    By octoc in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM

Tags for this Thread