Question: /// file some fun help(the code is bellow) #include #include #include #include #include using namespace std; constexpr int N = 8; // number of bits
/// file some fun help(the code is bellow)
#include #include #include #include #include using namespace std;
constexpr int N = 8; // number of bits typedef enum BIT{ zero = 0, one = 1 }; // bit
// ALL binary numbers are stored MSB at [0] to LSB at [N-1] // E.G.: // BIT a[] = {1, 0, 0, 0};
///////////// useful utility and overloads ////////////////////////
/////////////////////////////////////////////////////////////////// // inputs: Binary number, num, of length N // return the decimal equivalent as a long. long bin2dec(const BIT num[], int N) { long value(0); for (int i = 0; i
// input a binary number a, of length N // result is a is negated, that is, all ones become zero // all zeros become one. void bitNegation(BIT a[], int N) { for (int i = 0; i
// result = bitwise addition of a and b for length N void addition(BIT result[], const BIT a[], const BIT b[], int N) { BIT carry(BIT::zero); // carry over for (int i = N - 1; i >= 0; i--) // start from LSB to MSB { // force C++ to convert between BIT and int result[i] = static_cast(static_cast(a[i] + b[i] + carry) % 2); carry = static_cast(static_cast(a[i] + b[i] + carry) / 2); } }
// convert BIT array to string of 1s and 0s. char * Bit2Str(const BIT a[], int N) { char *result = new char[N+1]; // clean up later, of face a memory leak :-( for (int i = 0; i if (a[i] == BIT::zero) result[i] = char('0'); else result[i] = char('1'); result[N]= '\0'; // null terminated string return result; }
// overload the global insertion operator to work w/standard output // warning: assumes a global variable N that works for all BIT // numbers... better to redesign and create an class BIT. // but this is just for fun. ostream & operator
// okay method to display BIT[] void displayBits(const BIT a[], int N) // display N bit number { for (int i =0; i if (a[i] == BIT::zero) cout
// given two N bit binary numbers, a and b, return the 2N bit product void multiple(BIT answer[], const BIT a[], const BIT b[], int N) { // TO DO LAB HERE }
// test driver int main() { // will test this using my own numbers.....
BIT a[N] = { zero, zero, zero, zero, zero, zero, one, one }; //0000 0011 BIT b[N] = { zero, zero, one, zero, zero, zero, one, one }; //0010 0011 BIT p[2 * N]; // answer goes here: p = a x b
// warning cannot use cout with p if answer is greater then N length addition(p, a, b, N); cout
return 0; }
///file object bitset (the code bellow)
// bitset operators #include // std::cout #include // std::string #include // std::bitset
int main () { std::bitset foo (std::string("1001")); std::bitset bar (std::string("0011")); std::bitset boo (0x000f); // dec 15
std::cout
std::cout >=1)
std::cout >1)
std::cout
std::cout
return 0; }
///Implement Algorithm 3 in C++. Here is some code to get you started.
#include #include #include using namespace std; constexpr int N = 8; // number of bits typedef enum BIT{ zero = 0, one = 1 }; // bit void displayBits(const BIT a[], int N) // display N bit number { for (int i = N - 1; i >= 0; i--) if (a[i] == BIT::zero) cout
Implement Algorithm 3 in C++ (we not currently interested in efficient now). Implement Algorithm 3 in C pdf Object Bitset some fun help
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
