Thread: binary to decimal

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Question binary to decimal

    Hi there,

    I am very new to c prog and most probly have thousands of questions to ask.
    As for now, I have question on how do I make a conversion of Binary to Decimal. Having a standard in binary and standard out for decimal with an error msg where appropriate.

    1) Do I have to define the binary string (e.g. #define BSIZE 8) if I do that, does it mean that the standard in will have to be an exact of (e.g 10010001) and I can't input it as (e.g 101)

    2) How do I know when I can use a "char" or "int"? I know the function of it like char uses %d it is a single character value (e.g. ABC) and int uses %d (integer value)

    3) I know how to convert from binary to decimal in mathematical terms. However, I can't seem to have the logical ability (yet) to code it into a program.
    - in maths the binary will have the wt of 128, 64, 32, 16, 8, 4, 2,1
    - if I've to convert a binary of 101
    - it would be 101 = 4 + 1 = 5

    If I have to code it will it look like (will it?)
    - x=0 (binary standard in)
    - a=128 (the wt of the binary)
    - b=a/2 (the next wt)
    - x=b

    It is still a blunder to me.

    Really appreciate if anyone could help to clear this mud of mine. Being a novice is never a good feeling.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Things like %d perform a conversion for you. So the only thing you can do for simple input is use %s

    char buff[100];
    scanf("%s",buff);

    Now you have "101" or "11001" or "11100011001101" typed in by the user

    Try converting "1234" in decimal
    base = 10
    result = 0
    loop
    - result = result * base
    - result = result + numeric value of char
    end loop
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    If you know the bitwise operators it can be very simple.

    You can rotate the bits and test for 1 or 0 using a mask if you AND 1 with the rotated number it will give you the bit then use a conditional statement to print either a 0 or a 1 to the screen.


    Code:
    int number=3;
    int number_copy=number;
    _rotr(number,1);  //rotates number right _rotl() goes left
    
    if(number & 1)  //if number & 1 is true
    {
    putchar('1');
    }
    putchar('0');
    you have to work with it a bit because if you shift it the wrong direction it prints it out in reverse but you get the idea

    [edit]
    OOPS wrong direction you wanted binary to decimal

    You could just do the reverse. For each 1 or 0 you can use the mask to set the bits and then rotate.
    Last edited by manofsteel972; 03-14-2004 at 07:04 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >_rotr(number,1); //rotates number right _rotl() goes left
    My compiler must be broken. I just get undefined references concerning _rotr and _rotl.
    My best code is written with the delete key.

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Sorry am using Visual C++ compiler

    _rotl() and _rotr() are supposed to mimic the assembly language instructions for rotating bits.

    I suppose you could write your own using the bitwise shift operator>> and <<. Normally when you shift the bits left or right it just discards the bit and you eventually end up with 0. If you create a mask using the number for the bit you want to use (ie to test or set the 32nd bit set the mask=2^31) then you can use the following algorithm:

    for rotating right
    mask_lowbit=1; //2^0 the first bit set to 1
    mask_highbit=21474836848; //2^31 the 32nd bit set to 1

    test lowbit for 1 or 0 by ANDing (mask_lowbit & number)
    if result is 0 then simply shift number to the right
    if true then shift numbers to the right and set the 32nd bit by ORing(number | mask_highestbit)

    just reverse the process for shifting left
    Last edited by manofsteel972; 03-14-2004 at 05:26 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >_rotl() and _rotr() are supposed to mimic the assembly language instructions for rotating bits.
    I'm well aware of what they're supposed to do. My point was that the two functions/macros are not standard C. Maybe I missed the OP stating a platform and compiler where your suggestion would work, but you appear to be making far too many assumptions.
    My best code is written with the delete key.

  7. #7
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Thank you for setting me straight prelude.

    Your right. I just copied and paste one of my examples without thinking. I need to pay more attention to standards and compiler/op. Thank you. Anyway here is code example of home made functions.

    Code:
    unsigned int bit_rotate_right(unsigned int num, unsigned int numbits)
    {
        unsigned int lowbitmask=1;          // mask for 1st bit
        unsigned int highbitmask=2147483648;    //mask for 32nd bit 2^31
    
    	for(int i=0;i<numbits; i++)
    	    {
    		if(num & lowbitmask)
    		    {
    			num=num>>1;
    			num=(num|highbitmask); 
    		    }
    		else
    		    {
    			num=num>>1;
    		    }
    	    }
    return num;
    }
    
    unsigned int bit_rotate_left(unsigned int num, unsigned int numbits)
    {
        unsigned int lowbitmask=1;          // mask for 1st bit
        unsigned int highbitmask=2147483648;     //mask for 32nd bit 2^31
    
    	for(int i=0;i<numbits; i++)
    	    {
    		if(num &highbitmask )
    		    {
    			num=num<<1;
    			num=(num|lowbitmask); 
    		    }
    		else
    		    {
    			num=num<<1;
    		    }
    	    }
    return num;
    }
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    unsigned int highbitmask=2147483648;
    For the high bit of an unsigned int, consider the following.
    Code:
    unsigned int highbitmask = (-1U >> 1) + 1;
    Also,
    Code:
    for(int i=0;i<numbits; i++)
    it may be preferable to conform to C89 until C99 implementations become more prevalent. Otherwise the distinction between C and C++ can be blurred.
    Last edited by Dave_Sinkula; 03-14-2004 at 08:41 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  3. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM