Thread: printing out bit values without using an array

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    41

    printing out bit values without using an array

    I wrote a code that requests user input and then converts binary to bcd then back to binary using bitwise operations.
    While I think my code is good I want to know if there is a better way to write the code.
    Is there also a way to keep the code mostly the same but getting rid of the arrays for printing out the bit positions?
    Code:
        #include <stdio.h>
        #include <limits.h>
        #include <stdlib.h>
        #include <string.h>
        
        
        #define UINT_MAXIMUM 4294967294
        #define MAX_NUMBER 99999999
        
        void printBits(unsigned long number);
        unsigned long convertBinaryToBCD(unsigned long number);
        unsigned long convertBCDToBinary(unsigned long number);
        unsigned long printNumber(unsigned long input)
        {
          printf(" Input: ");
          printf("%10lu", input);
          printBits(input);
          unsigned long output_bcd = convertBinaryToBCD(input);
          unsigned long  output_binary = convertBCDToBinary(output_bcd);
          printf("   BCD: ");
          
          if(output_bcd != UINT_MAX)
          {
            printf("%10lu", output_bcd);
            printBits(output_bcd);
          }
          printf("Binary: ");
          if(output_binary != UINT_MAX)
          {
            printf("%10lu", output_binary);
            printBits(output_binary);
          }
          return 0;
        }
        void printBits(unsigned long number)
        {
            int bit_position[32];
            printf(" - ");
            int counter = 0;
            for(counter = 32; counter > 0; counter--)
            {
                bit_position[counter] = number % 2;
                number = number / 2;
            }
            for(counter = 1; counter <= 32; counter++)
            {
                if(counter == 9 || counter == 17 || counter == 25)
                {
                    printf(" | ");
                }
                if(counter == 5 || counter == 13 || counter == 21 || counter == 29)
                {
                    printf(" ");
                }
                printf("%d", bit_position[counter]);
            }
            printf("\n");
        }
        
        int main(int argc, char *argv[])
        {
          unsigned long number;
        
          number = strtoul(argv[1], NULL, 10);
        
          if(number > UINT_MAXIMUM)
          {
            printf("too big\n");
            return 0;
          }
          printNumber(number);
        
          return 0;
        }
        
        unsigned long convertBinaryToBCD(unsigned long number)
        {
          if (number > MAX_NUMBER)
          {
            return UINT_MAX;
          }
          if(number < 100)
          {
              return (((number/10)+((number/100)*6))*16)+(number%10);
          }
          unsigned long sub_number_one = ((number / 12) << 5);
          unsigned long sub_number_two = (number % 10) ;
          unsigned long temporary;
          while(sub_number_two != 0)
          {
              temporary = sub_number_one & sub_number_two;
              sub_number_one = sub_number_one ^ sub_number_two;
              sub_number_two = temporary << 1;
          }
          return sub_number_one;
        }
        
        unsigned long convertBCDToBinary(unsigned long number)
        {
          if (number == UINT_MAX)
          {
            return UINT_MAX;
          }
          if(number < 100)
          {
              return (((number >> 8) * 100) + ((number >> 4) * 10) + (number & 0xF));
          }
          
          unsigned long sub_number_one = (number >> 8) * 100;
          unsigned long sub_number_two = (number & 0x0F);
          unsigned long temporary;
          while(sub_number_two != 0)
          {
              temporary = sub_number_one & sub_number_two;
              sub_number_one = sub_number_one ^ sub_number_two;
              sub_number_two = temporary << 1;
        
          }
          return sub_number_one;
        }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,736
    There are a few ways to do what you want. The first problem I see with your printBits function is that it assumes that unsigned long is 32 bits. On my system it's not 32 bits. One way might be something like:

    Code:
    void printBits(unsigned long number)
    {
        printf(" - ");
        unsigned counter;
        unsigned bitcount = CHAR_BIT * sizeof(number);
        unsigned long mask;
    
        mask = 1UL << (bitcount - 1);
    
        for(counter = 0; counter < bitcount; counter++)
        {
            if (counter != bitcount - 1) {
                if (counter % 8 == 0)
                {
                    printf(" | ");
                }
                if (counter % 4 == 0)
                {
                    printf(" ");
                }
            }
            printf("%d", number & mask ? 1 : 0);
            mask >>= 1;
        }
        printf("\n");
    }
    Doesn't use an array and doesn't assume that unsigned long is 32 bits. There are other ways to do this but they all boil down to bitwise operations. There are ways to not rely on CHAR_BIT, for example, bit if it was me I'd write it as above and call it a day. (I haven't tested the above code by the way... but it should be ok. Don't worry about the mask = 1UL << (bitcount - 1)... any decent compiler will evaluate that at compile time rather than runtime so *shrug*. If it ever becomes an issue then worry about it then). Edit: you could also get rid of the counter variable but to be honest I wouldn't bother because it's useful for formatting the columns like you are

    Edit 2: Perhaps it's not so easy to get rid of counter and get the output formatted how you want. Here's another way without using CHAR_BIT though

    Code:
    void printBits(unsigned long number)
    {
        printf(" - ");
        unsigned long mask = ~(~0UL >> 1);
        unsigned counter = 0;
    
        while (mask) {
            /* > 1 so extraneous characters don't get printed at EOL */
            if (mask > 1) {
                if (counter % 8 == 0)
                {
                    printf(" | ");
                }
                if (counter % 4 == 0)
                {
                    printf(" ");
                }
            }
            printf("%d", number & mask ? 1 : 0);
            mask >>= 1;
            counter++;
        }
        printf("\n");
    }
    Last edited by Hodor; 4 Weeks Ago at 06:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing the whole array's values.
    By RyanC in forum C Programming
    Replies: 5
    Last Post: 11-22-2015, 08:32 AM
  2. Printing out odd and even values of an array
    By phillyflyers in forum C Programming
    Replies: 9
    Last Post: 04-17-2013, 10:55 AM
  3. Trouble printing x and y values of struct points in an array?
    By arcadedragon in forum C Programming
    Replies: 6
    Last Post: 10-03-2012, 10:15 AM
  4. Replies: 3
    Last Post: 10-21-2010, 12:39 PM
  5. stuck printing values from array
    By Guti14 in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2003, 09:56 PM

Tags for this Thread