binary problem

This is a discussion on binary problem within the C++ Programming forums, part of the General Programming Boards category; Hi there I am a brand new in C++. What i knew so far is to open and do only ...

  1. #1
    Unregistered
    Guest

    binary problem

    Hi there
    I am a brand new in C++. What i knew so far is to open and do only cout and cin with the <iostream> libary.

    Question: is anyone please give me a direction how to convert a binary to dicimal. for instance: 11111 need to convert in dicimal.
    any help or ideal is greatly appreciated.
    Thank a buch.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    is as simple as this.

    00000001 == 1
    00000010 == 2
    00000100 == 4
    00001000 == 8
    00010000 == 16
    00011111 == 31

    understand?

    each succesive bit(eg. 0 or 1) increases it's value buy a mlitiple of 2.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I think there's a simpler method but this will do the job -

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
    
    	char store[32]={0};
    	unsigned int result=0;
    	
    	cout << "Enter binary number: ";
    	cin >> store;
    
    	for(int i=strlen(store);i>=0;i--)
    	{
    		if(store[i]=='1')
    		result += pow(2,strlen(store)-i-1);
    	}
    
    
    	cout << result;
    
    
    	return 0;
    }
    zen

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    yes, but he needs to understand the concepts behind it such as how binary numbers work...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    gnaughty
    Guest

  6. #6
    Registered User UneducatedOne's Avatar
    Join Date
    Sep 2001
    Posts
    57
    no-one >>> 00011111 == 31

    I'm also very new and from seeing that its multiplied by two, why isn't 00011111 =="30" instead of 31?, therefore making it an even number and also making a multiple of 16.....am I s just stupid?, or don't I understand binary maths?
    ~Thank you~

    some have it, some don't, watch out for those who do....

  7. #7
    Registered User UneducatedOne's Avatar
    Join Date
    Sep 2001
    Posts
    57
    Also, what are the types of number maths that C++ is based on?
    octal? hexadecimal? binary...
    ~Thank you~

    some have it, some don't, watch out for those who do....

  8. #8
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    huh?

  9. #9
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    I'll see if I can make this easier ->

    128 64 32 16 8 4 2 1
    ---------------------------------------
    0 0 0 0 0 0 0 1 = 1
    0 0 0 0 0 0 1 0 = 2
    0 0 0 0 0 0 1 1 = 3
    0 0 0 1 1 1 1 1 = 31
    1 1 1 1 1 1 1 1 = 255

    If a '1' appears in a column then the number at the top of the column is added to the final number.

    PS After viewing this page i realise that the chart may not come out that well, oh well

  10. #10
    Registered User mike's Avatar
    Join Date
    Aug 2001
    Posts
    12
    see if this helps. paste code in and run


    #include "stdafx.h"
    #include <iostream.h> // add library file
    #include <iomanip.h> // add library file


    int main(int argc, char* argv[])
    {
    short number; // declare variables

    short firstPart; // declare variables

    short secondPart; // declare variables

    int binary1,binary2,binary3,binary4; // declare variables

    int remain1; // declare variables

    int remain2; // declare variables

    int remain3; // declare variables


    cout<<"Enter an integer between 0 and 15"<< endl;
    cin>>number; // user input


    firstPart=number/10;
    secondPart=number%10;


    binary1 = number / 8; // formula for binary number conversion
    remain1 = number % 8; // formula for binary number conversion
    binary2 = remain1 / 4; // formula for binary number conversion
    remain2 = remain1 % 4; // formula for binary number conversion
    binary3 = remain2 / 2; // formula for binary number conversion
    remain3 = remain2 % 2; // formula for binary number conversion
    binary4 = remain3; // formula for binary number conversion


    cout<<"N1="<<number<<"\tB1="<<binary1<<binary2<<bi nary3<<binary4;
    cout<<"\tThe digits are \t"<<" "<< firstPart<<" "<<setw(3)<< secondPart << endl;


    return 0;
    }

  11. #11
    Unregistered
    Guest

    may be useful

    It may be useful !!

    #include <iostream>

    using namespace std;

    void main()
    {

    char BinaryStr[32]={0};
    unsigned long Result=0;

    cout << "Input a binary number: ";
    cin >> BinaryStr;

    for(short i=strlen(BinaryStr);i>=1;i--)
    {
    if(BinaryStr[i]=='1')
    Result |= 1; //bitwise 0|1 = 1
    Result<<=1;
    }
    if(BinaryStr[i]=='1')
    Result |= 1; //bitwise 0|1 = 1

    cout << Result <<endl;
    }

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    2
    The last program is something wrong!
    I think the the correct answer is this....
    Using bitwise and shift operators can make your program
    much faster...

    #include <iostream>

    using namespace std;

    void main()
    {

    char BinaryStr[32]={0};
    unsigned long Result=0;

    cout << "Input a binary number: ";
    cin >> BinaryStr;

    for(short i=0;i<strlen(BinaryStr)-1;i++)
    {
    if(BinaryStr[i]=='1')
    Result |= 1; //bitwise 0|1 = 1
    Result<<=1;
    }
    if(BinaryStr[i]=='1')
    Result |= 1; //bitwise 0|1 = 1
    cout << Result <<endl;
    }

  13. #13
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    UneducatedOne,

    >I'm also very new and from seeing that its multiplied by two, why isn't 00011111 =="30" instead of 31?,

    sorry, i didn't explain that well. see phantoms chart at the end of this post.
    00000001 is 1 so the first number is 1 and it proceeds in multiples of two from then on. so if the first bit(not getting into bit ordering) is 1 the number is odd. hence 31 and not 30.

    >am I s just stupid?, or don't I understand binary maths?

    actually no to both, i just didn't explain it well.

    >Also, what are the types of number maths that C++ is based on?
    octal? hexadecimal? binary...<

    I would say decimal, all the math in computers is done at the lowest level in binary but it can easily be made to represent decimal math which is usually the case, so knowing binary math is not neccessary to use the math facilities of C++ or virtually any language for that matter.

    Xterria,

    questions? spit them out.

    phantom,

    code tags should help with the chart.

    Code:
    128 64 32 16 8 4 2 1 
    --------------------------------------- 
      0  0  0  0 0 0 0 1 = 1
      0  0  0  0 0 0 1 0 = 2
      0  0  0  0 0 0 1 1 = 3
      0  0  0  1 1 1 1 1 = 31
      1  1  1  1 1 1 1 1 = 255
    Last edited by no-one; 09-24-2001 at 08:17 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  14. #14
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    >phantom,

    >code tags should help with the chart.

    I admit I make a lousy teacher I have no problem explaining binary to a person one-to-one with a pencil and paper but I just don't know how to write it down for a third person

  15. #15
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    nah, you did better than me...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in binary search
    By LINUX in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2009, 07:50 AM
  2. Problem with Binary File I/O
    By DirX in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2004, 08:34 AM
  3. Binary trees search problem...
    By Umoniel in forum C Programming
    Replies: 2
    Last Post: 02-22-2004, 01:29 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21