/// file some fun help(the code is bellow) #include #include #include #include #
ID: 3839839 • Letter: #
Question
/// 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, }; // bit
// ALL binary numbers are stored MSB at [0] to LSB at [N-1]
// E.G.:
// BIT a[] = {1, 0, 0, 0}; <---- it looks good.
// a[0] is MSB and a[3] is LSB
///////////// 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 < N; i++)
value += static_cast(num[i]) * pow(2,N-i-1); // force conversion
return value;
}
// 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 < N; i++)
a[i] = (a[i] == BIT::one) ? BIT::zero : BIT::one;
}
// 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]= ''; // 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<<(ostream &out, const BIT num[])
{ // use of global N, very dangerous
char * bitstring = Bit2Str(num, N);
out << "dec value: "<< setw(4)<< bin2dec(num, N) << " in base 2 is " << bitstring;
delete []bitstring; // string no longer needed, clean-up or leak memory
return out;
}
////////////////////////////////////////////////////////////////
// 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 << '0'; else cout << '1';
cout << endl; // skip a line
}
// 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 << "a is :" << a << endl;
cout << "b is :" << b << endl;
cout << "a + b:" << p << endl;
return 0;
}
///file object bitset (the code bellow)
// bitset operators
#include // std::cout
#include // std::string
#include // std::bitset
int main ()
{
std::bitset<4> foo (std::string("1001"));
std::bitset<4> bar (std::string("0011"));
std::bitset<16> boo (0x000f); // dec 15
std::cout << (foo^=bar) << ' '; // 1010 (XOR,assign)
std::cout << (foo&=bar) << ' '; // 0010 (AND,assign)
std::cout << (foo|=bar) << ' '; // 0011 (OR,assign)
std::cout << (foo<<=2) << ' '; // 1100 (SHL,assign)
std::cout << (foo>>=1) << ' '; // 0110 (SHR,assign)
std::cout << (~bar) << ' '; // 1100 (NOT)
std::cout << (bar<<1) << ' '; // 0110 (SHL)
std::cout << (bar>>1) << ' '; // 0001 (SHR)
std::cout << (foo==bar) << ' '; // false (0110==0011)
std::cout << (foo!=bar) << ' '; // true (0110!=0011)
std::cout << (foo&bar) << ' '; // 0010
std::cout << (foo|bar) << ' '; // 0111
std::cout << (foo^bar) << ' '; // 0101
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, }; // 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 << '0';
else
cout << '1';
cout << endl; // skip a line
}
// 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, one, one, one, zero }; //0000 1110
BIT b[N] = { one, one, one, zero, one, one, zero, one }; //1110 1101
BIT p[2 * N]; // answer goes here
displayBits(a, N);
return 0;
}
Explanation / Answer
multiple function:
void multiple(BIT answer[],const BIT a[],const BIT b[],int N)
{
int i,j;
for(i=0;i<N;i++)
{
if(a[i]==BIT::zero||b[i]==BIT::zero)
{
answer[i]=BIT::zero;
}
else
{
answer[i]=BIT::one;
}
}
for(j=0;j<N;j++)
{
i=0;
if(b[j]==BIT::one)
{
i=N-1-j;
if(a[i]!=BIT::zero)
if(answer[j]==BIT::zero)
answer[j]=BIT::one;
else
answer[j]=BIT::zero;
}
else
{
answer[j]=BIT::zero;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.