Question: Many functions below will refer to data type called byte. This is simply shorthand for an unsigned char. Other than string::length(), you may not use

Many functions below will refer to data type called "byte". This is simply shorthand for an "unsigned char". Other than string::length(), you may not use any STL functions. Instead, you must implement the needed functionality using logical and bitwise operations. I need help with making these 6 functions. I've attached my code below.

  • byte msb(byte) -- required

    This function returns the MSB (bit 7) of the given unsigned char shifted to the LSB position (bit 0).

  • bool overflow(byte, byte, byte) -- required

    This functions checks if the result of an addition has produced overflow. You may recall that this involves comparing the MSBs of the two operands and the result. Instead of first extracting the MSBs, you may apply the same bitwise operations to the three numbers and then extract the MSB of the result.

  • byte twoscomplement(byte) -- required

    This function returns two's complement of the input.

  • byte str2bin(string &) -- required

    Given a binary number as an ASCII string, this function computes and returns the corresponding unsigned char. Seek inspiration from Lab 1. Replace multiplication with bit-shifting and use bitwise OR instead of addition when setting a bit.

  • string bin2str(byte) -- required

    This function does the reverse of the str2bin function. That is, given an unsigned char, it computes and returns the corresponding ASCII string. Use bitwise operations to determine when and how to set a bit.

  • bool isBIN8(string &) -- required

    This function checks that the binary number considered is between 1 and 8 characters long and that each character is either 0 or 1. If either check fails, the function returns false. Otherwise, true.

  • bool isVALID(string &) -- required

    This function implements a Boolean expression for checking that the operation is a single character which is either a '+' or a '-'. If not, the function return false. Otherwise, true.

#include #include #include using namespace std;

typedef unsigned char byte;

byte msb(byte); bool overflow(byte, byte, byte); byte twoscomplement(byte);

byte str2bin(string &); string bin2str(byte); string bin2dec(byte);

bool isBIN8(string &); bool isVALID(string &);

int main(int argc, char *argv[]) { string sX, sY; string op;

cout << "X> "; cin >> sX; if (cin.eof()) return 1;

if (!isBIN8(sX)) { cout << "error: not 8-bit binary number "; return 1; }

cout << "Y> "; cin >> sY; if (cin.eof()) return 1;

if (!isBIN8(sY)) { cout << "error: not 8-bit binary number "; return 1; }

cout << "op> "; cin >> op; if (cin.eof())

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!