Question: string twos_complement(string s) { //Write code here to convert binary string s to two's complement notation using the code below. // Reverse the bits, then

string twos_complement(string s) { //Write code here to convert binary string s to two's complement notation using the code below. // Reverse the bits, then add 1 to the value. }

string add_binaries(string b1, string b2) { assert(is_binary(b1) && is_binary(b2)); string result = ""; int carry = 0; int i1 = (int)b1.length() - 1; int i2 = (int)b2.length() - 1; while (i1 >= 0 || i2 >= 0) { int d1 = 0, d2 = 0; if (i1 >= 0) d1 = b1[i1] - 48; if (i2 >= 0) d2 = b2[i2] - 48; int sum = carry + d1 + d2; carry = sum / 2; result = string(1, (char)(48 + sum % 2)) + result; i1--; i2--; }

if (carry != 0) result = "1" + result; return result; } For example: Enter a binary number: 1001101 should turn into: 0110011

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!