Muhammad Shaeel Abbas
Muhammad Shaeel Abbas

Reputation: 29

Binary to Decimal and Hex (bit by bit)

I'm learning C programming. I've been given a "Challenge Task" that states that I have to get binary input from user bit by bit (8 times, since there's 8 bits in a byte) asking for 1 or 0 (do not have to validate) and them.

After collecting 8 bits, I then have to give an output

"Your decimal value for binary number is: " and then the decimal value. "Your hexadecimal value for binary number is: " and then the hex value.

I am not looking for answers or code, I am looking for ways to tackle this problem. Can you tell me the right approach in terms of thinking?

I know that I'll need 8 int variables, bit1,bit2,bit3,bit4,bit5,bit6,bit7 and bit8.

Below is my code so far. Thanks!

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

int main()
{  
    int bit1,bit2,bit3,bit4,bit5,bit6,bit7,bit8;

    printf("Please enter the first bit(0 or 1): ");
    scanf("%d",&bit1);
    printf("Please enter the second bi(0 or 1)t: ");
    scanf("%d",&bit2);  
    printf("Please enter the third bit(0 or 1): ");
    scanf("%d",&bit3);  
    printf("Please enter the fourth bit(0 or 1): ");
    scanf("%d",&bit4);  
    printf("Please enter the fifth bit(0 or 1): ");
    scanf("%d",&bit5);  
    printf("Please enter the sixth bit(0 or 1): ");
    scanf("%d",&bit6);  
    printf("Please enter the seventh bit(0 or 1): ");
    scanf("%d",&bit7);
    printf("Please enter the eigth bit:(0 or 1) ");
    scanf("%d",&bit8);

    return 0;   
}

Upvotes: 1

Views: 677

Answers (3)

Pras
Pras

Reputation: 4044

2^7 * (1 OR 0) + 2^6 * (1 OR 0) + 2^5 * (1 OR 0) + 2^4 * (1 OR 0) + 2^3 * (1 OR 0) + 2^2 * (1 OR 0) + 2^1 * (1 OR 0) + 2^0 * (1 OR 0)

This is how you have to convert binary to decimal (1 OR 0) is what you enter from input

Something like this if you want inputs in 8 diffrent variables:

#include <stdio.h>

#include <math.h>

    int main()
{  
    int bit1,bit2,bit3,bit4,bit5,bit6,bit7,bit8;
int sum =0;

    printf("Please enter the first bit(0 or 1): ");
    scanf("%d",&bit1);
    printf("Please enter the second bi(0 or 1)t: ");
    scanf("%d",&bit2);  
    printf("Please enter the third bit(0 or 1): ");
    scanf("%d",&bit3);  
    printf("Please enter the fourth bit(0 or 1): ");
    scanf("%d",&bit4);  
    printf("Please enter the fifth bit(0 or 1): ");
    scanf("%d",&bit5);  
    printf("Please enter the sixth bit(0 or 1): ");
    scanf("%d",&bit6);  
    printf("Please enter the seventh bit(0 or 1): ");
    scanf("%d",&bit7);
    printf("Please enter the eigth bit:(0 or 1) ");
    scanf("%d",&bit8);

    sum = (pow(2,7) * bit8) + (pow(2,6) * bit7) + (pow(2,5) * bit6) 
+ (pow(2,4) * bit5) + (pow(2,3) * bit4) + (pow(2,2) * bit3) +
 (pow(2,1) * bit2) + (pow(2,0) * bit1);

printf("decimal = %d\n",sum);
    return 0;   
}

Upvotes: 0

sg7
sg7

Reputation: 6298

You do not need to have declare 8 separate integers. You can collect your input to int bit[8] array. After that you can use array elements in a simple processing loop.

After collection is done you can construct the value of your 8 bit byte by taking under account that every collected bit in bit[8] has to go to the different position in your byte.

You can do that by shifting 1 according to the byte position and 'adding' it to the byte.

<< is the bitwise left shift operator.

You can use binary OR operator '|' to implant 1 into the proper byte position. The binary '|' operation with 0 has no effect on the byte.

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

int main(void)
{  
    char c = 0;
    int bit[8];

   // Typically bits are counted from LS to MS where LS is bit 0   
    printf("Please enter the first bit(0 or 1): ");
    scanf("%d",&bit[0]);
    printf("Please enter the second bi(0 or 1): ");
    scanf("%d",&bit[1]);  
    printf("Please enter the third bit(0 or 1): ");
    scanf("%d",&bit[2]);  
    printf("Please enter the fourth bit(0 or 1): ");
    scanf("%d",&bit[3]);  
    printf("Please enter the fifth bit(0 or 1): ");
    scanf("%d",&bit[4]);  
    printf("Please enter the sixth bit(0 or 1): ");
    scanf("%d",&bit[5]);  
    printf("Please enter the seventh bit(0 or 1): ");
    scanf("%d",&bit[6]);
    printf("Please enter the eigth bit:(0 or 1) ");
    scanf("%d",&bit[7]);

    for(int i=0; i<8; i++)
    {
        c = c | (bit[i] << i);
    }    

    printf("\nYour decimal value for binary number is: %d\n", c);
    printf("Your hexadecimal value for binary number is: x%2.2X\n", (unsigned char)c);

    return 0;        
}

Output:

Please enter the first bit(0 or 1): 0                                                                                                          
Please enter the second bi(0 or 1): 0                                                                                                          
Please enter the third bit(0 or 1): 0                                                                                                          
Please enter the fourth bit(0 or 1): 0                                                                                                         
Please enter the fifth bit(0 or 1): 0                                                                                                          
Please enter the sixth bit(0 or 1): 0                                                                                                          
Please enter the seventh bit(0 or 1): 0                                                                                                        
Please enter the eigth bit:(0 or 1) 1 

Your decimal value for binary number is: -128                                                                                                
Your hexadecimal value for binary number is: x80

Upvotes: 0

J...S
J...S

Reputation: 5207

You don't need the 8 variables to store each bit. You can manage with a single variable inputBit to receive all the bits.

You could do this (for positive numbers) like

unsigned int n = 0
for(int i=0; i<8; ++i)
{
    printf("Enter bit %d: ", i);
    scanf("%d", &inputBit);
    n = n + (inputBit<<i);
}

The resultant decimal number would be in n which was initialised to 0.

<< is the bitwise left shift operator.

inputBit<<i is added to n on each iteration of the loop.

When inputBit is 1, the value of inputBit<<i is same as 2i and when inputBit is 0, inputBit<<i is 0.

To print n in its hexadecimal form, do

printf("\nHexadecimal form: %x", n);

Upvotes: 1

Related Questions