This blog is under construction

Saturday 13 July 2013

C Program to convert decimal to binary using bitwise operator

Write a C Program to convert decimal to binary using bit-wise operator.

To convert a decimal value to binary, find the number of bytes needed to store the given input value.

For example, user input is 31
31 can be represented in 8 bits(1 byte) => 0001 1111
So, 1 byte is enough to store the given user input.

Find the binary value with most significant bit as 1 and all other bits as 0.  It can be found using the below formula.

No of bits in a byte = 8 bits
2^((no of bytes * no of bits in a byte) - 1) => 2^(1 * 8 - 1) => 2^7 => 128

Binary equivalent of 128 is 1000 0000.  So, 1000 0000(decimal value 128) is our mask value.

Find bit-wise AND between given input and mask.  If the result is one, store 1 in output character array.  Otherwise, store 0 in output array.

31   => 0001 1111
128 => 1000  0000
          --------------
          0000  0000

So, store 0 in 8 byte character array.
            +-----------------------------------+
Output: | 0 |    |    |    |    |    |    |    |
            +-----------------------------------+

Shift mask value by one bit to right to check whether the next bit in given input is set or not.

(0000 1111) & (0100 0000) => 0

            +-----------------------------------+
Output: | 0 | 0  |    |    |    |    |    |   |
            +-----------------------------------+

Continue this operation until mask becomes 0.

Finally, output array will have the value 00011111.




  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <math.h>

  int main() {
        unsigned int num, sz, val, i, j = 0;
        char *str, *ptr;

        /* get the input value from user */
        printf("Enter your input value:");
        scanf("%u", &num);

        for (i = 1; i < sizeof(unsigned int); i++) {
                /* find the no of bytes needed for the given input */
                sz = i * 8 - 1;
                if (num < pow(2, sz))
                        break;
        }

        /* 
         * convert sz bytes to bits and allocate memory to store
         * to binary value.  One additional bit is to store NULL
         */
        str = (char *)malloc(sizeof(char) * pow(2, sz) + 1);
        val = pow(2, sz);

        /*
         * val has 1 at MSB and other bits are 0. Shift one bit
         * per iteration and check whether that particular bit
         * is set in the user input. If yes, store 1 in ouput 
         * array.  Otherwise, 0 will be stored in output array(str)
         */
        while (val) {
                if (num & val) {
                        str[j++] = '1';
                } else {
                        str[j++] = '0';
                }
                val = val >> 1;
        }

        str[j] = '\0';

        /* skips 0's at the front of first 1 */
        ptr = strstr(str, "1");

        /* print the result */
        printf("Binary format: %s\n", ptr?ptr:str);
        return 0;
  }


Note:
gcc decToBin.c -lm => link math library since we have used math function pow().

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input value:31
  Binary format: 11111


No comments:

Post a Comment