0

I am trying to convert some binary data to decimal numbers. I succeeded to convert binary to hex, but now I want to do the same with binary to decimal. I want to use bitwise operators and NO array.

See my code from binairy to hex. The incoming data is 8 bits. This code first calculates the most significant number, and then, the least significant number. This works good, but I cannot figure it out for bin to dec

The function console_print_char is sort of the printf function of my program.

    void console_print_hex_char(unsigned char data){
    unsigned char temp;

    if(data){
        temp = (data >> 4);
        if(temp){
            if(temp> 9) console_print_char(temp + ('A'- 10));
            else console_print_char(temp + '0');
        }
        temp = data & 0x0F;
        if(temp> 9) console_print_char(temp + ('A'- 10));
        else console_print_char(temp + '0');
    }
    else{
        console_print_char('0');
    }
}
5
  • 2
    your question is a general programming question that is not related to the Arduino ... please delete your post and go here stackoverflow.com/questions
    – jsotola
    Commented Mar 17, 2020 at 17:22
  • note: you have a stray }, that is outside of the "code area" of your post
    – jsotola
    Commented Mar 17, 2020 at 17:25
  • @jsotola is right, you might need to push this over to SO. Anyway, the conversion of a binary number into decimal is most simply done with modulo (%) and divide (/) if you don't want to use library functions. Additionally, there is an algorithm that involves shifting bit by bit, and some conditional adding of 3. Unfortunately I don't have an URL at hand. Commented Mar 17, 2020 at 21:07
  • 1
    Binary is spelled B-I-N-A-R-Y. It's bi-na-ry, not bin-air-y.
    – Duncan C
    Commented Mar 17, 2020 at 22:07
  • You can google Binary to BCD conversion. There are pretty short algorithms that do this. After the conversion each decimal digit is stored in a nibble.
    – Kwasmich
    Commented Mar 19, 2020 at 16:20

2 Answers 2

1

You cannot do what you are trying to do. There is no direct correspondence between some number of bits and a decimal digit.

With hexadecimal, every 4 bits corresponds to exactly 1 hex digit. That is why hex is used for computers.

One hex digit represents exactly 4 bits. Every time you add another hex digit, you add 4 bits.

Two hex digits corresponds to exactly a byte. 00h to FFh represents a value from 0 to 255.

There is no such direct correspondence between binary and decimal. If you have 4 bits, it takes 1 or 2 decimal digits to represent it (0-15) If you have 8 bits, it takes 1, 2, or 3 decimal digits to represent it, but there are 3 digit decimal values (values > 255) that you can't represent with 8 bits.

Binary  Hex Decimal
0000    0   0
0001    1   1
0010    2   2
0011    3   3
0100    4   4
0101    5   5
0110    6   6
0111    7   7
1000    8   8
1001    9   9
1010    A   10
1011    B   11
1100    C   12
1101    D   13
1110    E   14
1111    F   15

You simply cannot convert a binary number to decimal using bit shifting and masking.

As @Kwasmich says in their answer, the closest you're likely to come would be to convert your binary value to BCD (Binary coded decimal) where each 4 bits holds a decimal digit. You could convert THAT to decimal character output using masking and shifting.

0

here's the c++ code

    vector<int> b={1,0,1,0,1,0,1};
        int sum=0;
        for(int i=b.size()-1, j=0; i>=0; i--, j++){
            sum+=b[j]*(1<<i);
        }
        return sum;
2
  • That is a way to convert a series of bits to decimal. Note the it uses multiplication and arrays, both of which the OP wants to avoid.
    – Duncan C
    Commented Jun 27, 2020 at 1:43
  • 1
    BTW, it's silly to use b[j] * (1<<i). That's an unneeded multiplication. Just use sum += b[j]<<i` If b[j] is 0, it shifts nothing for that bit. If b[j] is a 1 bit, it shifts a one to the appropriate magnitude, without the need of a multiply.
    – Duncan C
    Commented Jun 27, 2020 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.