Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions