Pages - Menu

Monday 8 October 2012

Binary to decimal and decimal to binary base conversion using bitwise operators in C

The six operators that C provides are shown in the table below:
Operator
Action
&
Bitwise AND
|
Bitwise OR
^
Bitwise exclusive OR (XOR)
<< 
Shift left
>> 
Shift right
~
One's complement (NOT)
The bitwise AND, OR and NOT have the same truth tables as their logical equivalents, except that they work bit by bit. The outcome of an XOR operation is true if only one of the operands is true and false otherwise.
The bit-shift operators << and >> move all the bits of the variable to the left or right, as specified, the number of positions shifted being given by the right operand, which must be a positive integer. A shift is not a rotate, so the bits shifted do not come back at the other end, but are lost.
Bitwise operations may be applied only to integral operands like the standard char, short, int, and long data types, signed or unsigned.

For the truth table of the logical operators, read this article: 
Printing the truth table of the logical operators in C#                                  

Decimal to binary radix conversion
The following C function converts an integer from base 10 to binary. The first element of the bin array represents the sign bit.
#define MAX 17

/*
 * Description:
 *  Converts an integer from base 10 to base 2
 * Parameters:
 *  dec - the integer to be converted
 *  bin - pointer to the array that will contain the integer converted in binary
 * Returns:
 *  bin - array containing the binary equivalent of the integer to be converted
 */
int *DecimalToBinary(int dec, int *bin)
{
    int i = MAX;
    if(dec < 0)
    {
        dec = -dec;
        bin[0] = 1;
    }
    else
    {
        bin[0] = 0;
    }
    for(; i>=0; i--)
    {
        bin[MAX-i+1] = (dec>>i) & 1;
    }
    return bin;
}
Example:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main(void)
{
    int dec = 0, *bin = NULL, i = 0;
    bin = (int*)malloc(sizeof(int)*MAX);
    printf("\n Enter the number in decimal: ");
    scanf("%d",&dec);
    printf("\n\tThe number in binary: ");
    if(dec >= -100)
    {
        bin = DecimalToBinary(dec,bin);
        for(; bin[i] == 0 || bin[i] == 1; i++)
        {
            printf("%d",bin[i]);
        }
    }
    getch();
    return 0;
}
Output:


Binary to decimal radix conversion
The following C function converts an integer from base 2 to decimal. The first element of the bin array represents the sign bit.
#define MAX 17

/*
 * Description:
 *  Converts an integer from base 2 to base 10
 * Parameters:
 *  bin - array containing the integer in binary
 * Returns:
 *  decimal - the equivalent integer in base 10
 */
int BinaryToDecimal(int bin[])
{
    int i = 1, decimal = 0;
    for(; bin[i] == 0 || bin[i] == 1; i++)
    {
        decimal = (decimal<<1) | bin[i];
    }
    if(bin[0] == 1)
    {
        decimal = -decimal;
    }
    return decimal;
}
Example:
#include<stdio.h>
#include<conio.h>

int BinaryToDecimal(int bin[]);

int main(void)
{
    int bin[MAX], dec = 0, b = 0, i = 0;
    printf("\n Enter the number in binary (to stop reading, enter a number other than 0 or 1:\n");
    do
    {
        scanf("%d",&b);
        if(b == 0 || b == 1)
        {
            bin[i++] = b;
        }
    }while(b == 0 || b == 1);
    dec = BinaryToDecimal(bin);
    printf("\n\tThe number in decimal is: %d",dec);
    getch();
    return 0;
}
Output:

No comments:

Post a Comment