function not adding correctly

ok so this is just a function in a larger program im writting but i need help getting the output to come out correctly. i am adding binary numbers as chars not ints . i worked the logic out with some help from online math but i cannot seem to get the carry while adding 4 bit binary numbers t work correctly any help would be appreciated. if you want me to post the class files and the whole program i will just ask for it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 BinNum operator+ ( BinNum &b1, BinNum &b2 )// make return value a char and both inputs char arrays so it works without the class 
{    
	
	BinNum temp; // change this to a char array so it can work without the class file 

	int i;
	char carry[4];
	
	for (i = 3; i >= 0; i--)
	{
		if (b1.the_num[i] == '0' && b2.the_num[i] == '0')
		{
			temp.the_num[i] = '0';
			carry[i] = '0';
		}
		if (b1.the_num[i] == '0' && b2.the_num[i] == '1')
		{
			temp.the_num[i] = '1';
			carry[i] = '0';
		}
		if (b1.the_num[i] = '1' && b2.the_num[i] == '0')
		{
			temp.the_num[i] = '1';
			carry[i] = '0';
			
		}
		
		if (b1.the_num[i] == '1' && b2.the_num[i] == '1')
		{
			if (carry[i] != '1')
			{
				temp.the_num[i] = '0';
				carry[i] = '1';
			}
			if (carry[i] == '1')
			{

				temp.the_num[i] = '1';
				carry[i] = '1';
			} 
			
		}
		if (b1.the_num[i] == '0' && b2.the_num[i] == '0' && carry[i] == '1')
		{
			temp.the_num[i] = '1';
			carry[i] = '0';
		}
		if (carry[i] == '1')
		{

			temp.the_num[i] = '1';
			carry[i] = '1';
		}
		
	}

		
		return temp;
}
http://www.cplusplus.com/forum/beginner/181237/ this is the rest of the code if you need it
The problem that I see is that you are computing the carry OUT of when adding bits, but you aren't then accounting for the carry IN in the next digit position.

Generally, when adding bits at a particular position you have 2 bits from the digits being added (call them A and B) and the carry bit from the previous position (call it C). When adding the first bits, you just set the carry in to 0.

The output of the addition is the sum but (S) and the carry bit C' to the next position.

If A, B, and C are 0 or 1 (the numbers, not characters '0' and '1') then
1
2
S = A^B^C;
C' = A&&B || A&&C || B&&C 

Actually there is probably an easier way to compute C.
i thought this would work going off of what you said but it doesnt seem to help much i think i need to change the whole code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

BinNum operator+ ( BinNum &b1, BinNum &b2 )
{    
	
	BinNum temp;
	int i;
	char carry[4];
	int j = 3;
	
	for (i = 3; i >= 0; i--)
	{
		if (b1.the_num[i] == '0' && b2.the_num[i] == '0')
		{
			temp.the_num[i] = '0';
			carry[i] = '0';
		}
		if (b1.the_num[i] == '0' && b2.the_num[i] == '1')
		{
			temp.the_num[i] = '1';
			carry[i] = '0';
		}
		if (b1.the_num[i] = '1' && b2.the_num[i] == '0')
		{
			temp.the_num[i] = '1';
			carry[i] = '0';
			
		}
		
		if (b1.the_num[i] == '1' && b2.the_num[i] == '1')
		{
			if (carry[j] != '1')
			{
				temp.the_num[i] = '0';
				j--;
				carry[j] = '1';
				break;
			}
			if (carry[j] == '1')
			{

				temp.the_num[i] = '1';
				j--;
				carry[i] = '1';
				break;
			} 
			
		}
		if (b1.the_num[i] == '0' && b2.the_num[i] == '0' && carry[j] == '1')
		{
			temp.the_num[i] = '1';
			j--;
			carry[j] = '0';
			break;
		}
		if (carry[j] == '1')
		{

			temp.the_num[i] = '1';
			j--;
			carry[j] = '1';
			break;
		}
		
	}

		
		return temp;
}
this was my next attempt and now the answer is always 0000 so i know i messed somthing up
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
BinNum operator+ ( BinNum &b1, BinNum &b2 )
{    
	
	BinNum temp;
	int i;
	char carry[5];
	int j = 2;
	carry[3] = 0;
	
	for (i = 3; i >= 0; i--)
	{
		if (b1.the_num[i] == '0' && b2.the_num[i] == '0' && carry[i] == '0')
		{
			temp.the_num[i] = '0';
			carry[j] = '0';
			j--;
		}
		if (b1.the_num[i] == '0' && b2.the_num[i] == '1' && carry[i] == '0')
		{
			temp.the_num[i] = '1';
			carry[j] = '0';
			j--;
		}
		if (b1.the_num[i] = '1' && b2.the_num[i] == '0' && carry[i] == '0')
		{
			temp.the_num[i] = '1';
			carry[j] = '0';
			j--;
			
		}
		
		if (b1.the_num[i] == '1' && b2.the_num[i] == '1' && carry[i] == '0')
		{
			
			
				temp.the_num[i] = '0';
			    carry[j] = '1';
				j--;

		}
		if (b1.the_num[i] == '1' && b2.the_num[i] == '1' && carry[i] == '1')
		{
			temp.the_num[i] = '1';
			carry[j] = '1';
			j--;
		}

		if (b1.the_num[i] == '0' && b2.the_num[i] == '0' && carry[i] == '1')
		{
			temp.the_num[i] = '1';
			carry[j] = '0';
			j--;
		
		}
		
		
	}

		
		return temp;
}
im getting closer now but im not sure what is messing it up now the output of 0110 and 0111 being added is coming out to 0001 when it needs to be 1101
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
BinNum operator+ ( BinNum &b1, BinNum &b2 )
{    
	
	BinNum temp;
	int i;
	char carry[4];
	int j = 2;
	carry[3] = '0';
	
	for (i = 3; i >= 0; i--)
	{
		if (carry[i] == '0'){
			if (b1.the_num[i] == '0' && b2.the_num[i] == '0')
			{
				temp.the_num[i] = '0';
				carry[j] = '0';
				j--;
			}
			if (b1.the_num[i] == '0' && b2.the_num[i] == '1')
			{
				temp.the_num[i] = '1';

				carry[j] = '0';
				j--;
			}
			if (b1.the_num[i] = '1' && b2.the_num[i] == '0')
			{
				temp.the_num[i] = '1';
				carry[j] = '0';
				j--;

			}
			if (b1.the_num[i] == '1' && b2.the_num[i] == '1')
			{
				temp.the_num[i] = '0';
			carry[j] = '1';
			j--;

		}
		}

		if (carry[i] == '1'){
		if (b1.the_num[i] == '1' && b2.the_num[i] == '1')
		{
			temp.the_num[i] = '1';
			carry[j] = '1';
			j--;

		}
		
		if (b1.the_num[i] == '0' && b2.the_num[i] == '0')
		{
			temp.the_num[i] = '1';
			carry[j] = '0';
			j--;

		}
	}
	
	}

		
		return temp;
}
Topic archived. No new replies allowed.