Question
Only the signed extension function is passing, even though Twos complement is outputting correctly. #include #include #include using namespace std; int binary_to_decimal_signed(string s); // precondition:
Only the signed extension function is passing, even though Twos complement is outputting correctly.
#include
#include
#include
using namespace std;
int binary_to_decimal_signed(string s);
// precondition: s is a string that consists of only 0s and 1s
// postcondition: the decimal integer that is represented by s in two's complement
string signed_extension(string s);
// precondition: s is a string that consists of only 0s and 1s that is at most 16 bits
// postcondition: a 16 bit string has been returned as signed extension of s. For instance, if s = "0101" then
// return value will be "00000000000000000101" total 12 0s are added in front of s
string decimal_to_binary_signed(int n);
// precondition: n is an integer
// postcondition: ns two's complement binary representation is returned as a string of 0s and 1s
string add_binaries_signed(string b1, string b2);
// precondition: b1 and b2 are strings that consists of 0s and 1s at most 32 bits, i.e.
// b1 and b2 are two's complement binary representations of two integers. "0" is 0, "1" is -1
// However, "10" will be consider as "1111111111111110" as -2
// postcondition: the sum of b1 and b2 is returned as (up to) 32 bits two's complement representation.
// For instance, if b1 = 1101 (-3), b2 = 01 (+1), then the return value is 1111111111111110 (-2)
string twos_complement(string s);
// precondition: s is a string that consists of only 0s and 1s
// postcondition: two's complement of s is returned as an 16 bits binary integer. For instance, if s = "1101", then
// return value will be "1111111111111101"
int binary_to_decimal(string s);
// precondition: s is a string that consists of only 0s and 1s
// postcondition: the positive decimal integer that is represented by s
string decimal_to_binary(int n);
// precondition: n is a positive integer
// postcondition: ns binary representation is returned as a string of 0s and 1s
string add_binaries(string b1, string b2);
// precondition: b1 and b2 are strings that consists of 0s and 1s, i.e.
// b1 and b2 are binary representations of two positive integers
// postcondition: the sum of b1 and b2 is returned. For instance,
// if b1 = 11, b2 = 01, then the return value is 100
void menu();
// display the menu. Student shall not modify this function
int grade();
// returns an integer that represents the students grade of this projects.
// Student shall NOT modify
bool is_binary(string b);
// returns true if the given string s consists of only 0s and 1s; false otherwise
bool test_binary_to_decimal_signed();
// returns true if the students implementation of binary_to_decimal function
// is correct; false otherwise. Student shall not modify this function
bool test_decimal_to_binary_signed();
// returns true if the students implementation of decimal_to_binary function is correct; false otherwise. Student shall not modify this function
bool test_add_binaries_signed();
// which returns true if the students implementation of add_binaries function
// is correct; false otherwise. Student shall not modify this function
bool test_signed_extension();
// return true if the student's implementation of sign_extension function
// is correct; false otherwise. Student shall not modify this function
bool test_twos_complement();
// return true if the student's implementation of twos_complement function
// is correct; false otherwise. Student shall not modify this function
int main()
{
int choice;
string b1, b2;
int x, score;
do{
// display menu
menu();
cout << "Enter your choice: ";
cin >> choice;
// based on choice to perform tasks
switch(choice){
case 1:
cout << "Enter a binary string: ";
cin >> b1;
if(!is_binary(b1))
cout << "It is not a binary number ";
else
cout << "Its decimal value is: " << binary_to_decimal_signed(b1) << endl;
break;
case 2:
cout << "Enter an integer: ";
cin >> x;
cout << "Its binary representation is: " < break; case 3: cout << "Enter two binary numbers, separated by white space: "; cin >> b1 >> b2; if(!is_binary(b1) || !is_binary(b2)) cout << "At least one number is not a binary" << endl; else cout << "The sum is: " << add_binaries_signed(b1, b2) << endl; break; case 4: cout << "Enter a binary number: "; cin >> b1; cout << "Its signed extension to 16 bits is: " << signed_extension(b1) << endl;; break; case 5: cout << "Enter a binary number: "; cin >> b1; cout << "Its two's complement is: " << twos_complement(b1) << endl; break; case 6: score = grade(); cout << "If you turn in your project on blackboard now, you will get " << score << " out of 10" << endl; cout << "Your instructor will decide if one-two more points will be added or not based on your program style, such as good commnets (1 points) and good efficiency (1 point)" << endl; break; case 7: cout << "Thanks for using binary calculator program. Good-bye" << endl; break; default: cout << "Wrong choice. Please choose 1-5 from menu" << endl; break; } }while(choice != 7); return 0; } string signed_extension(string s){ // you implement this one first int n = s.length(); int k = 16 - n; string result = ""; if (s[0] == '0') { for (int i = 0; i < k; i++) { result += "0"; } } else { for (int i = 0; i < k; i++) { result += "1"; } } result += s; return result; } int binary_to_decimal_signed(string s){ for (int i = 0; i < s.size(); i++) { assert(s[i] == '0' || s[i] == '1'); } int result = 0; int sign = (s[0] == '1') ? -1 : 1; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { result += (1 << (s.size() - i - 1)); } } return result * sign; } string decimal_to_binary_signed(int n){ // you implement this one fourth string result = ""; int bits = 16; if (n < 0) { result = '1'; n = ~n + 1; } else { result = '0'; } for (int i = 0; i < bits - 1; i++) { if (n & (1 << (bits - i - 2))) { result += '1'; } else { result += '0'; } } return result; } string add_binaries_signed(string b1, string b2){ // you implement this one fifth string result = ""; int carry = 0; int n = b1.length(); int m = b2.length(); // sign extension for both numbers if(n < 32) b1 = string(32 - n, b1[0] == '1' ? '1' : '0') + b1; if(m < 32) b2 = string(32 - m, b2[0] == '1' ? '1' : '0') + b2; // add each bit with carry for(int i = 31; i >= 0; i--){ int sum = (b1[i] - '0') + (b2[i] - '0') + carry; result = char(sum % 2 + '0') + result; carry = sum / 2; } return result; } string twos_complement(string s){ int n = s.length(); string result = ""; bool carry = 1; for (int i = n - 1; i >= 0; i--) { if (s[i] == '0' && carry) { result = '0' + result; carry = 0; } else if (s[i] == '1' && carry) { result = '0' + result; } else if (s[i] == '0' && !carry) { result = '1' + result; carry = 0; } else { result = '1' + result; carry = 1; } } while (result.length() < 16) { result = '0' + result; } return result; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started