I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here
bool add(string & sum, bool & n, bool & z, bool & v, bool & c,
const string & term0, const string & term1);
term0 and term1 should each be 5-bit strings with each char being '0' or '1' – otherwise,
return false.
If term0 and term1 are ok, add's job is to perform the addition, putting the 5-bit result
in sum, put the resulting status bits in n, z, v, and c, and return true.
For example, add(s, n, z, v, c, "11110", "00011") should
set sum to "00001"
set n to false (result not negative)
set z to false (result not zero)
set v to false (2's complement representation isn't wrong)
set c to true (unsigned representation is wrong)
In determining v and c, here's a trick that might be helpful:
You can determine the values of v and c by looking at only the high-order bit of the
operands and result. For example
1
1000
0
1100
0
0100
looking at just the first bit:
2's complement has us adding neg plus non-neg equals non-neg; this is possible, so v is
set to false.
unsigned has us adding a big number and a little number, getting a little number; this is
impossible, so c is set to true.
Here is the required code in C++
#include<bits/stdc++.h>
using namespace std;
bool add(string & sum, bool & n, bool & z, bool
& v, bool & c, const string & term0, const string &
term1)
{
if(term0.size()!=5 || term1.size()!=5)
return false;
for(int i=0;i<5;i++)
{
if(term0[i]!='0' && term0[i]!='1')
return false;
if(term1[i]!='0' && term1[i]!='1')
return false;
}
sum="00000";
string str = sum;
int carry = 0;
for(int i = 4; i >= 0 ; i--)
{
int num0 = term0[i] - '0';
int num1 = term1[i] - '0';
sum[i] = ((num0 ^ num1) ^ carry)==0 ? '0' : '1'; // c is
carry
carry = ((num0 & num1) | (num0 & carry)) | (num1 &
carry);
}
n = (sum[0]=='1');
z = (sum == str);
v = (term0[0]==term1[0] && term0[0]!=sum[0]);
c = (carry==1);
return true;
}
int main()
{
string sum="";
string s1, s2;
bool n, z, v, c;
cout<<"Enter 1st 5-bit Binary Number: ";
cin>>s1;
cout<<"Enter 2nd 5-bit Binary Number: ";
cin>>s2;
cout<<endl;
if(add(sum, n, z, v, c, s1, s2))
{
cout<<"Sum is: "<<sum<<endl;
cout<<"Is Negative: "<<n<<endl;
cout<<"Is Zero: "<<z<<endl;
cout<<"Is 2's Complement representation wrong:
"<<v<<endl;
cout<<"Is Unsigned representation wrong:
"<<c<<endl;
}
else
cout<<"Invalid Inputs";
return 0;
}
Here is the Output for Sample Inputs:

I'm trying to convert between different number representations in C++ , I have the prototype but...
1. Implement an algorithm to convert binary number into an integer. Binary number is represented as a string. Ex. int n = to_int("01010"); int to_int(const std::string& b) { } 2. Implement an algorithm to convert a decimal number to binary. The return type is string which holds the binary number as string. std::string to_binary(int n) { } 3. Implement a function to check if the number is positive or negative. The function should return true if number is positive. bool...
e,f,g,h,i
1) Given: X 0xA4 and Y 0x95, a) Convert X and Y to 8-bit binary numbers. b) Compute the 8-bit sum X+Y of X and Y o) Compute Y the 8-bit two's complement of Y. d) Compute the 8-bit difference X"Y of X and Y. (Use two's complement addition.) o) Convert XiY, Y, and, X Y to hexadecimal. D What are the values of the condition flags z n c v upon computing X-+Y? g) What are the values...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...
Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b); // precondition: s is a string that...
c++ question- so I have an arraylist header with a pointer Student * list; (i have class Student in another header file) and I am trying to do a seqSearch function with a string parameter. int studentList::seqSearchName(string n) const{} theres a part in the function: if (list[loc] == n) ... etc and I am getting an error “no operator “==“ matches these operands operand types are: Student == std::string “ how would I be able to compare the class Student...
i
have received an irrelevant answer, please answer if you do want
help.
Thanks
URGENT could you please help me go through the solution of (d and e) expecting a detailed answer Thanks (c) Does adding 2t to an n-bit number affect any of the least significant n bits of the resulting (n+1)-bit number? (d) Use the above result and the fact that a (b-c) (a c)+b to explain how the two's complement representation of a negative number may be...
I need help with this assignment. My answers are in bold but I am not getting the correct output which is also below. Can you please take a look at it. #include <stdlib.h> #include <stdio.h> #include <float.h> #include <math.h> // PURPOSE: To define a nickname for type 'unsigned int'. typedef unsigned int uInt; //-- Sign related constants --// // PURPOSE: To tell how many bits to shift the sign field from the // least...
C++ CODE
/* This is program project 2 on page 695.
* Before you begin the project, please read the project description
* on page 695 first.
*
* Author: Your Name
* Version: Dates
*/
#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
class Fraction
{
public:
// constructor
Fraction(int a, int b);
// generate a fraction which is a/b
Fraction(int a);
// generate a fraction which is a/1
Fraction();
// generate a fraction which is 0/1. i.e...
I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...
C++ CODE
/* This is program project 2 on page 695.
* Before you begin the project, please read the project description
* on page 695 first.
*
* Author: Your Name
* Version: Dates
*/
#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
class Fraction
{
public:
// constructor
Fraction(int a, int b);
// generate a fraction which is a/b
Fraction(int a);
// generate a fraction which is a/1
Fraction();
// generate a fraction which is 0/1. i.e...