C++. Create and implement the functions listed below. Precondition and postcondi
ID: 3881495 • Letter: C
Question
C++. Create and implement the functions listed below. Precondition and postconditions are provided.
string add_binaries_signed(string b1, string b2);
// precondition: b1 and b2 are strings that consists of 0s and 1s, e.g., b1 and b2 are binary two’s complement representations of two integers. Keep in mind that this is "signed" binary addition.
// postcondition: the sum of b1 and b2 is returned. e.g., if b1 = “11”, b2 = “01”,then the return value is “100”
string signed_extension(string b);
// 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. e.g., if s = "0101" then return value will be "00000000000000000101" total 12, 0s are added in front of s
Explanation / Answer
(a)
#include <iostream>
using namespace std;
//adds the two bit strings and return the result
string addBitStrings( string first, string second );
// Helper method: given two unequal sized bit strings, converts them to
// same length by aadding leading 0s in the smaller string. Returns the
// the new length
int makeEqualLength(string &str1, string &str2)
{
int len1 = str1.size();
int len2 = str2.size();
if (len1 < len2)
{
for (int i = 0 ; i < len2 - len1 ; i++)
str1 = '0' + str1;
return len2;
}
else if (len1 > len2)
{
for (int i = 0 ; i < len1 - len2 ; i++)
str2 = '0' + str2;
}
return len1; // If len1 >= len2
}
// The main function that adds two bit sequences and returns the addition
string addBitStrings( string first, string second )
{
string result; // To store the sum bits
// make the lengths same before adding
int length = makeEqualLength(first, second);
int carry = 0; // Initialize carry
// Add all bits one by one
for (int i = length-1 ; i >= 0 ; i--)
{
int firstBit = first.at(i) - '0';
int secondBit = second.at(i) - '0';
// boolean expression for sum of 3 bits
int sum = (firstBit ^ secondBit ^ carry)+'0';
result = (char)sum + result;
// boolean expression for 3-bit addition
carry = (firstBit & secondBit) | (secondBit & carry) | (firstBit & carry);
}
// if overflow, then add a leading 1
if (carry)
result = '1' + result;
return result;
}
// Driver program to test above functions
int main()
{
string str1 = "11";
string str2 = "01";
cout << "Sum is " << addBitStrings(str1, str2);
return 0;
}
#include <iostream>
using namespace std;
//adds the two bit strings and return the result
string extension( string first );
// The main function that adds two bit sequences and returns the addition
string extension( string str )
{
string result;
int length = str.size();
int number_of_zeros = 16 - length;
result = std::string( number_of_zeros, '0').append( str);
return result;
}
// Driver program to test above functions
int main()
{
string str1 = "0101";
cout << "Extended string is " << extension(str1);
return 0;
}
In output, total 12 zeros are appended in front , so the total result of the string os 16bits long.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.