need some help with binary addition.

This is a discussion on need some help with binary addition. within the C++ Programming forums, part of the General Programming Boards category; Kleid-0 have you tested your program with various inputs...I just compiled it with different values for b1 and b2 and ...

  1. #16
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Kleid-0 have you tested your program with various inputs...I just compiled it with different values for b1 and b2 and it doesn't work.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #17
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I modified your code a bit to receive user input, and as you can see, it does not work. Even 1+1 or 0+0;

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    
    using std::cout;
    using std::cin;
    
    // Allow boolean types
    
    
    // Add binary numbers and return binary
    int addBinary (int b1, int b2);
    
    int main (void)
    {
      bool notDone = true;
      // Binary numbers b1 and b2 get added together
      int b1, b2, b3;
      while( notDone )
      {
    	cout << "\n1st: ";
    	cin >> b1;
    	
    	if( b1 == 9 ) //input 9 for exit
    		break;
    	
    	cout << "\n2nd: ";
    	cin >> b2;
    
    	
    
    	b3 = addBinary (b1, b2);
    	// Print out binary addition
    	printf ("\n%d + %d = %d\n",b1, b2, b3);
      }
      return 0;
    }
    
    // Add binary numbers and return binary
    int addBinary (
        int b1,     // The first binary number
        int b2)     // The second binary number
    {
       // The binary addition answer, transforms into binary.
       int a;
       // Carrier for binary addition
       bool carrier = false;
       // Current digit step (starts at 1)
       int s = 2;
    
       // Add the numbers like decimal
       a = b1 + b2;
    
    // Next Binary digit
    NBD:
        // If the carrier is true, add one to the current placement
        if (carrier)
        {
          a += 1 * (s - (s / 2));
          carrier = false;
        }
    
        // If the carrier makes the postdigit a 3, we have trouble
        if (a % (s / 2 + s) < s / 2)
        {
          a += 1 * ((s * 10) - ((s * 10) / 2));
          a -= 1 * (s - (s / 2));
        }
    
        // If the addition yields a 2 * digit placement,
        // remove it and set carrier binary addition to true.
        if (a % s < (s / 2))
        {
          a -= 1 * (s - (s / 2));
          carrier = true;
        }
    
        // If our digit placement is high enough, exit
        if (s > a)
          return a;
    
        // Set the digit placement higher
        s *= 10;
    
        // Verify next placement (Next binary Digit)
        goto NBD;
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #18
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    All of this logical thinking has destroyed my thinking capacities, I'm going to wait out this problem and come back. I'll try Scribbler's approach next time lol....I thought I knew what I was doing :(

  4. #19
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    You might want to edit your original post with the code stating that your solution does not work.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #20
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

  6. #21
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I dug and dug, and dug somemore...and finally, I found it! Not the exact solution to your problem but I knew I wrote something very similar a long time ago in my first c++ class. The assignemnt was a bit different, as I think one of the specs was that the binary number can only have a max of 4 digits (ie. 1011 is good, and 11101 is not) - this should be easily changed. Also, each input has to have leading zeros; so if you want to add 1 + 10, you should input 0001 + 0010 - I really don't remember why I did it this way - maybe becasue that the only way I knew how to

    anywho I run the prog on some test input just now, and it seems to be working fine, but only with the rules I described above - no error handling whatsover (Hey it was my first c++ class, and one of the first assignments )

    Code:
    /*
     * Author: Jakub Bomba
     * Class: CS100, Prof. Bell
     * Date: 09.09.2002
     * Title: Binary Addition
     */
    
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    /*
     * Function that actually does the binary addition
     * @param:	f - first binary number
     *			s - second binary number
     *			carry - flag for the carry
     */
    int binaryAdd(int f, //first number
    			  int s, //second number
    			  int * carry) 
    {
    
    	int result=0;
    
    	// exhaust all of the possible cases as described in the handout
    
    	if( (f == 0) && (s == 0) ) 
    	{
    		result = ( *carry );
    		*carry = 0;
    	}
    
    	else if( (f == 0) && (s == 1) ) 
    	{ 		
    		if( *carry==0 ) 
    			result = 1;
    		else 
    		{
    			result = 0;
    			*carry = 1;
    		}
    	}
    	else if( (f == 1) && (s == 0) ) 
    	{ 
    		if( *carry==0 ) 
    		{
    			result = 1;			
    		} 
    		else 
    		{
    			result = 0;
    			*carry = 1;
    		}
    	} 
    	else if( (f == 1) && (s == 1) ) 
    	{ 
    	
    		if( *carry==0 ) 
    		{
    			result = 0;
    			*carry = 1;
    		} 
    		else 
    		{
    			result = 1;
    			*carry = 1;
    		}
    	}
    
    	return result;
    }
    
    
    int main(void) { 
    
    	int i;          // loop counter
    	int carry=0;		// Initialize carry to 0 (IMPORTANT)
    
    	int n1[4];	// MSB - index 0 LSB index 3 - MSB - Most significant bit  (leftmost)
    	int n2[4];	// MSB - index 0 LSB index 3 - LSB - Least significant bit (rightmost)
    	int result[5];	// MSB - index 0 LSB index 4
    
    	char e_result[6]; // 5+null char
    	char e_n1[5]; // 4+null char
    	char e_n2[5]; // 4+null char
    
    	// get data
    
    	cout << "Enter first number: ";
    	cin >> e_n1; 
    	cout << "Enter second number: ";
    	cin >> e_n2;
    
    	// convert from char array to int array
    	// It is useful to have data in int array when doing conversion into decimal 
    	// value of a certain binary number which is not implemented in this program. 
    
    	for(i = 0; i < 4; i++) 
    	{
    		n1[i] = (e_n1[i]-48);  
    		n2[i] = (e_n2[i]-48);	
    	}
    
    	// initialize result array
    	for( i = 0; i < 5; i++) 
    		result[i] = 0;
    
    
    	// do the addition	
    	for( i = 3; i >= 0; i--) { // go from right to left (LSB to MSB)
    		
    		result[i+1] = binaryAdd(n1[i],n2[i],&carry);	// needs to add 1 to the counter
    														// since LSB index for result 
    														// is 1 more than LSB of operands		
    
    		e_result[i+1] = result[i+1]+48; // For display purposes - convert to char array
    	}
    
    	result[0] = carry; // Add MSB of result
    	e_result[0] = result[0] + 48; 
    	e_result[5] = '\0'; // Put NULL Char
    
    	
    
    	cout << "   " << e_n1 << endl;
    	cout << "  +" << e_n2 << endl;
    	cout << "  -----" << endl;
    	cout << "  " << e_result << endl << endl;
    
    	return 0;
    
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #22
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    thanks for another tip and the boost, i was right when i said the solution uses logical operators but i was getting myself lost in the many if statements.
    When no one helps you out. Call google();

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

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Binary tree addition
    By crag2804 in forum C Programming
    Replies: 2
    Last Post: 09-30-2002, 08:48 AM

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