Dec to Bin, Compare 2 Bins, Back to Dec

Hello,

I'm having trouble with part of my assignment. I'm supposed to take 2 decimal numbers change them into binary numbers, do AND and OR comparisons between those 2 binary numbers, change the resulting binary number into a decimal number and store it into an array.

Ex. decA = 53 decB = 26

binaryA = 110101
binaryB = 11010
---------------------
And comparison = 010000
OR camparison = 111111

The binary equivalent of the AND comp is: 16
The binary equivalent of the OR comp is: 63

What I decided to do was change the decimal numbers to bin and store it them in 2 arrays, and then compare those 2 arrays and that's where I'm stuck. I feel like its correct but I'm not getting the right results.

My code for decimal to binary work great. Here is my code for comparing both arrays:
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
  
        //which bin number has more digits
        int max;
	if(indexB > indexC)
		max = indexB;
	else 
		max = indexC;

        //AND comparison
        int andComp[12];
	int newmax = max;

	while(newmax >= 0 )
	{
		int i = 0;
		
		if( (binB[i] != 0) && (binC[i] != 0) )
			andComp[i] = 1;
		else andComp[i] = 0;
			
		i++;
		newmax--;
	}
	
	cout << "Comparing B and C: ";

	for(int i = max - 1; i >= 0; i--)
		cout << andComp[i];
	cout << endl;





Last edited on
Never mind, I finally found what I was doing wrong, had my int i = 0 inside the while look. >.<
Topic archived. No new replies allowed.