Bit manipulation program with binary operations

My homework is to create a program to scan in and add binary numbers and print them like so

Enter a binary number: 1

sum = 1 decimal 1 hex and 00000001 binary

Enter a binary number: 10

sum = 3 decimal 3 hex and 00000011 binary

etc.

I know that bit manipulation and input and output of strings is necessary to create this program, but I'm not experienced with either. The code I have compiles, but I am unable to make it do anything useful, and most of it is wrong. My do loop is probably wrong and I don't understand how to properly use bit manipulation. I'm rather clueless, can anyone give me some ideas on what to do next?

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
#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "iostream"
#include "string"
using namespace std;

int sum = 0;
int i = 0;
int c = 0;
int Q;
string binaryInput;

int main()
{
cout << ("Enter a binary number: "); //Prompts the user for input
cin >> binaryInput; //Writes the binary input to cin

do { //do loop to read the binary one digit at a time
	i++; 
	cout << binaryInput[i]; //I don't understand how to output a specific digit of the string and manipulate it
	c = binaryInput[i];
	if (c = Q)
		break;
	else;
		sum = sum + c * 2 * i; // supposed to be sum = sum + binaryInput[i] * pow(2,i), this is where the binary is converted to decimal

printf("\nsum = %*d ", 3, sum);
printf("decimal %*x " , 2, sum);
printf("hex %08d binary", sum);

} while (i<binaryInput.size()); 

while(1);
}
In class today we were told to use a bit mask inside the loop to read off the bits from the input. Now I have

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
#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "iostream"
#include "string"
using namespace std;

unsigned int sum = 0;
unsigned int c = 0;
unsigned int Q;
int i;
unsigned int binaryOut;
unsigned int bitmask = 000000001;
string binaryInput;

int main()
{
cout<<("Enter a binary number: "); //Prompts the user for input
cin>>binaryInput; //Writes the binary input to memory

do {i=1; //do loop to read the binary one digit at a time
	cout<<binaryInput[i]<<" ";
	c = binaryInput[i];
	i++;
	if (c = Q) //breaks when Q is input
		break;
	else;
		binaryOut = c&bitmask; //compares the digit with the bit mask to see if the sum is increased
		sum = sum + binaryOut;

		printf("\nsum = %*d ", 3, sum);
		printf("decimal %*x " , 2, sum);
		printf("hex %08d binary", sum);

} while (i<binaryInput.size()); 

while(1);
}


Can anyone help me, this is due tomorrow morning.
Last edited on
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
#include "stdio.h"
#include <math.h>
#include <iostream>
#include <string>
using namespace std;



int main()
{
	int sum = 0;
	int i = 0;
	char c = '0'; // use a char type
	int Q;
	string binaryInput;
	cout << ("Enter a binary number: "); //Prompts the user for input
	cin >> binaryInput; //Writes the binary input to cin	
	do { //do loop to read the binary one digit at a time		
		c = binaryInput[binaryInput.size() - i - 1]; // you can access the array reverse
		if (c != '0') // you have to regard the if (c = 0)  because it is an assignment expression
		{
			sum = sum + pow(2,i);  		
		}		
		i++;
	} while (i < binaryInput.size()); 
	
	printf("\nsum = %d ", sum);
	printf("decimal %*x " , 2, sum);
	printf("hex %08d binary", sum);
	return 0;
}


Of course your index variable "i" could start to count from binaryInput.size() - 1 to 0
Topic archived. No new replies allowed.