Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that reads two strings of binary digits and adds them using

ID: 3602079 • Letter: W

Question

Write a C++ program that reads two strings of binary digits and adds them using a simulation of a full-adder.

The program must contain a routine that simulates a half-adder, which the full adder routine will need to call twice in order to perform the addition.


The program should implement routines to simulate AND, OR, and NOT gates, (and possibly XOR gates). Your program should accept strings of binary digits up to eight in length (can be shorter than 8 bits), and print each step in the binary addition of the two strings, including any carries. Then the program should output the final result of the addition.

The program should respond gracefully (but not end) if the input is not a string of ones and zeros.

Set up the program to repeat four times so that I can test it on four different sets of numbers without having to restart it each time.

See attached file for example output.

Hints:

1. You will need to write a routine called something like HALFADDER. It will have two parameters, p and q. The routine will calculate a sum and a carry using AND, OR and NOT operations and/or possibly XOR. Your main program will not call the HALFADDER routine. This routine will only be called by the FULLADDER routine.

2. Then you will need a routine called something like FULLADDER. It will call the half adder routine twice for each pair of bits that you want to add. The full adder routine will have three parameters, the two bits that you want to add and the carry-in from the previous operation. The full adder routine will calculate a sum and a carry-out for the two bits that you are adding. The carry-out will become the carry-in for the next two bits.

•   Runs 4 times
   •   Shows each step
   •   Adds correctly
   •   Simulates full and half adders
   •   Responds gracefully if input is not correct
   •   Bonus: Put a space every 4 bits

Explanation / Answer

//This Program will take in two integer values seperated by a space.
//It will then use logical bit operators ( & | ^ ) to add binary numbers
// the problem and solution will be writen to the screen in both dec and binary.


#include <iostream>
#include <iomanip>
#include <bitset>
using namespace std;

void print_bool (bitset<8> bs)    //This function runs through the bitsets passed to it writing them to the screen
{                                                     //with an endl at the end it.
    for ( int i = (int) bs.size()-1; i >= 0; i--)
        {cout << bs[i] ;}
        cout<<endl;
}

int main()
{
//*********Declare variables and initialize data values includes taking inputs**********


    int int_a=0, int_b=0, final= 0;
  
    cout <<"Eneter two positive intager values seperated by a space"<<endl;    //Askes for the input and takes in the input as intagers
    cout<<endl;                              
    cin >>int_a>>int_b;
  
    bitset<8> bool_a (int_a);    //These bitset statemens creates the binary values I will work with two to represnt the integers to be added
    bitset<8> bool_b (int_b);    //as well as a sum and carry all intialized to 0
    bitset<8> sum, carry = 0;
  
    cout <<endl;
    cout<<endl;


    //***************Computations are performed here********************


    sum = bool_a ^ bool_b;        //Gets the starting sum by using exclusive or s = a xor b
    carry = bool_a & bool_b;    //The starting carry is c = a and b
    carry = carry <<1;        //YOU HAVE TO SHIFT THE BITS IN THE CARRY TO MAKE THEM LINE UP
                    //THIS INSURES THAT THE FIRST ONE ON THE RIGHT IS ALWAYS A ZERO
                    //This is also know as a half adder  
  
    for (int i = 0; i < 7; i++)    //Sets up the loop to iterate through the bits for the full adder
    {
        carry[i+1] = bool_a[i] & bool_b[i] | carry[i] & (bool_a[i] ^bool_b[i]);    // c = x and y or z and (x xor y)
        sum[i] = (bool_a[i] ^ bool_b[i]) ^ carry[i];                // s = (x xor y) xor z
    }  

//The key there is that z needs to be your c from the previous itteration
//hence having to establish starting values the full adder must be feed by
//an half adder  
//*******Printing and formating*******


    cout<<"c ";                                                  
    print_bool(carry);
    cout<<" ^^^^^^^^"<<endl;
    cout<<" ";
    print_bool(bool_a);
    cout<<"+ ";
    print_bool(bool_b);
    cout <<"----------"<<endl;
    cout<<" ";
    print_bool(sum);
    cout<<endl;
    cout<<endl;
    final = sum.to_ulong();    //This converts the bitset back to an interger value to printed to the screen
    cout<<int_a<<" + "<<int_b<<" = "<<final<<endl;
return 0 ;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote