Help with C++ Math

This is a discussion on Help with C++ Math within the C++ Programming forums, part of the General Programming Boards category; Hello, I am new to these great forums here and new to C++ too Anyway, I am coding a binary ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Smile Help with C++ Math

    Hello, I am new to these great forums here and new to C++ too

    Anyway, I am coding a binary to decimal converter and I have the math right and everything but C++ does not want to play nice with the math.

    here is the output:
    Code:
      Enter a binary number: 11110000
    
    1 = 98
            1 =  (2 ^ 0) * 1
    1 = 147
            1 =  (2 ^ 1) * 1
    1 = 0
            1 =  (2 ^ 2) * 1
    1 = 49
            1 =  (2 ^ 3) * 1
    0 = 288
            0 =  (2 ^ 4) * 0
    0 = 336
            0 =  (2 ^ 5) * 0
    0 = 192
            0 =  (2 ^ 6) * 0
    0 = 240
            0 =  (2 ^ 7) * 0
    the decimal number shows the output C++ gave for the digit, the tabbed line under it shows the math it did to get that number, if you do the math manually the output should be different.

    Here is my code:
    Code:
    char inText[32];
    int final;
    
    cout << "\n  Enter a binary number: ";
    cin >> inText;
    cout << "\n  Decimal Result: \n";
    
    for(int length = 0; length < strlen(inText); length++)
    {
            // The forumla
            final += 2 ^ length * inText[length];
    
            // Shows 1 = $output
            cout << inText[length] << " = " << ((2 ^ length) * inText[length]) << "\n\t";
            // Shows 1 = (2 ^ $len) * $digit
            cout << inText[length] << " = " << " (2 ^ " << length << ") * " << inText[length] << "\n";
    }
    
    cout << final;
    cout << "\n\n";
    Thanks in advance
    Last edited by aonic; 01-29-2005 at 02:40 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    ^ is exclusive-or (a bit operator) in C and C++

    If you want a 2 to the power of n, then do
    (1<<n)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    3
    Thanks, now I get:
    Code:
    1 = 98
            1 =  (2 << 0) * 1
    1 = 196
            1 =  (2 << 1) * 1
    1 = 392
            1 =  (2 << 2) * 1
    1 = 784
            1 =  (2 << 3) * 1
    0 = 1536
            0 =  (2 << 4) * 0
    0 = 3072
            0 =  (2 << 5) * 0
    0 = 6144
            0 =  (2 << 6) * 0
    0 = 12288
            0 =  (2 << 7) * 0
    With:
    Code:
    final += (2<<length) * inText[length];
                           
    cout << inText[length] << " = " << ((2<<length) * inText[length]) << "\n\t";
    cout << inText[length] << " = " << " (2 << " << length << ") * " << inText[length] << "\n";

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    1. I said ONE, NOT TWO
    1<<n
    you did
    2<<n

    2. If you type in 11110000 then the left-most bit is bit 7, not bit 0
    Code:
    for ( i = 0, b = 7 ; i < strlen(text) ; i++, b-- ) {
      if ( ...   1<<b  ...
    }
    3. When you do this
    ((2<<length) * inText[length])
    You're not multiplying by 0 or 1, you're multiplying by '0' or '1' (which in the ASCII character set is 48 and 49 respectively)

    So prior to maths, do this
    Code:
    int int_value_of_bit = inText[length] - '0';  // convert '0','1' into 0,1
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    3
    Thank you, that solved my problems

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 02:14 AM
  3. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  4. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 09:06 PM
  5. For the math junkies
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-14-2003, 05:09 PM

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