convert a binary into decimal

Discussion in 'C Programming' started by QQ, Mar 22, 2006.

  1. QQ

    QQ Guest

    Hello

    unsigned char a;

    the a[0-3] represents a 4-bit binary,
    a[4-6] represents another 3-bit binary.

    I'd like to convert a[0-3] into a decimal.
    For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
    5

    My program is like

    if((a && 0x00)==1) return 0;
    else if (a&& 0x01)==1) return 1;
    .....

    Is there any simpler way for it?

    Thanks a lot!
    QQ, Mar 22, 2006
    #1
    1. Advertising

  2. QQ wrote:
    > Hello
    >
    > unsigned char a;
    >
    > the a[0-3] represents a 4-bit binary,


    return a & 0xf;

    > a[4-6] represents another 3-bit binary.


    return (a >> 4) & 0x7;
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Mar 22, 2006
    #2
    1. Advertising

  3. QQ

    pemo Guest

    QQ wrote:
    > Hello
    >
    > unsigned char a;
    >
    > the a[0-3] represents a 4-bit binary,
    > a[4-6] represents another 3-bit binary.
    >
    > I'd like to convert a[0-3] into a decimal.
    > For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should
    > be 5
    >
    > My program is like
    >
    > if((a && 0x00)==1) return 0;
    > else if (a&& 0x01)==1) return 1;
    > ....
    >
    > Is there any simpler way for it?
    >
    > Thanks a lot!


    0-3 = a & 0x0F;
    4-6 = (a & 0x70) >> 4;

    --
    ==============
    Not a pedant
    ==============
    pemo, Mar 22, 2006
    #3
  4. QQ wrote:
    > unsigned char a;


    > the a[0-3] represents a 4-bit binary,
    > a[4-6] represents another 3-bit binary.


    > I'd like to convert a[0-3] into a decimal.
    > For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
    > 5
    >
    > My program is like
    >
    > if((a && 0x00)==1) return 0;
    > else if (a&& 0x01)==1) return 1;


    I don't understand how the above can do what you want.

    > Is there any simpler way for it?


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

    int main(void)
    {
    unsigned char a;
    int i;
    srand(time(0));
    for (i = 0; i < 4; i++) {
    a = (unsigned) rand();
    printf
    ("a = %3u (%#03o, %#02x), first 4 bits=%u,"
    " next 3 bits=%u\n",
    a, a, a, a >> (CHAR_BIT - 4), 07 & (a >> (CHAR_BIT - 7)));
    }
    return 0;
    }


    a = 186 (0272, 0xba), first 4 bits=11, next 3 bits=5
    a = 171 (0253, 0xab), first 4 bits=10, next 3 bits=5
    a = 19 (023, 0x13), first 4 bits=1, next 3 bits=1
    a = 98 (0142, 0x62), first 4 bits=6, next 3 bits=1
    Martin Ambuhl, Mar 22, 2006
    #4
  5. On 22 Mar 2006 09:45:53 -0800, "QQ" <> wrote:

    >Hello
    >
    >unsigned char a;
    >
    >the a[0-3] represents a 4-bit binary,
    >a[4-6] represents another 3-bit binary.
    >
    >I'd like to convert a[0-3] into a decimal.
    >For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
    >5
    >
    >My program is like
    >
    >if((a && 0x00)==1) return 0;


    The if is guaranteed to always be false. Anything && 0 is always
    false.

    >else if (a&& 0x01)==1) return 1;


    This is no different than if(a). Did you perhaps mean (a & 0x01)?

    Apparently your unsigned char holds 0 or 1 as opposed to '0' and '1'.
    In this case, you can compute
    num1 = a[3]*8 + a[2]*4 + a[1]*2 + a[0];
    and
    num2 = a[6]*4 + a[5]*2 + a[4];


    Remove del for email
    Barry Schwarz, Mar 23, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makok
    Replies:
    1
    Views:
    6,763
    Anders Hellerup Madsen
    Feb 23, 2004
  2. Golan
    Replies:
    7
    Views:
    2,114
    Malcolm
    Dec 8, 2003
  3. Leo jay
    Replies:
    9
    Views:
    652
    Barry
    Sep 13, 2007
  4. Vitaliy
    Replies:
    1
    Views:
    471
    Peter Otten
    May 29, 2008
  5. valpa
    Replies:
    11
    Views:
    1,504
    Steven D'Aprano
    Mar 24, 2009
Loading...

Share This Page