In: Computer Science
please use C++
Create a class named Byte that encapsulates an array of 8 Bit objects. The Byte class will provide the following member functions:
Byte - word: Bit[8] static Bit array defaults to all Bits false
+ BITS_PER_BYTE: Integer16 Size of a byte constant in Bits; 8
+ Byte() default constructor
+ Byte(Byte) copy constructor
+ set(Integer): void sets Bit to true
+ clear(): void sets to 0
+ load(Byte): void sets Byte to the passed Byte
+ read(): Integer16 returns Byte as Integer
+ place(Integer): Bit Return Bit& at passed Base2 place
+ NOT(): Byte returns this Byte with its bits not'ed
+ AND(Byte): Byte returns this Byte and'ed with passed Byte
+ OR(Byte): Byte returns this Byte or'ed with passed Byte
+ NAND(Byte): Byte returns this Byte nand'ed with passed Byte
+ NOR(Byte): Byte returns this Byte nor'ed with passed Byte
+ XOR(Byte): Byte returns this Byte xor'ed with passed Byte
+ XNOR(Byte): Byte returns this Byte xnor'ed with passed Byte
#include<bits/stdc++.h>
#include<climits>
using namespace std;
class Byte {
public:
int Bit[8];
const int BITS_PER_BYTE = 8;
Byte() {
for (int i = 0; i < 8; i++) {
Bit[i] = 0;
}
}
Byte(const Byte &obj) {
for (int i = 0; i < 8; i++)
Bit[i] = obj.Bit[i];
}
void set(int n) {
for (int i = 0; i < 8; i++)
Bit[i] = 0;
}
void clear() { //sets to 0
for (int i = 0; i < 8; i++)
Bit[i] = 0;
}
void load(Byte &obj) { //sets Byte to the passed Byte
for (int i = 0; i < 8; i++)
Bit[i] = obj.Bit[i];
}
int read() {
int ans = 0;
int mul = 0;
for (int i = 0; i < 8; i++) {
ans += Bit[i] * (1 << mul);
mul++;
}
return ans;
}
Byte NOT() { //returns this Byte with its bits not'ed
for (int i = 0; i < 8; i++)
Bit[i] = ~Bit[i];
return *this;
}
Byte OR(Byte obj) { //returns this Byte with its bits or'ed
for (int i = 0; i < 8; i++) {
Bit[i] = Bit[i] | obj.Bit[i];
}
return *this;
}
Byte AND(Byte obj) { //returns this Byte with its bits and'ed
for (int i = 0; i < 8; i++) {
Bit[i] = Bit[i] & obj.Bit[i];
}
return *this;
}
Byte NAND(Byte obj) { //returns this Byte with its bits nand'ed
for (int i = 0; i < 8 ; i++) {
Bit[i] = ~(Bit[i] & obj.Bit[i]);
}
return *this;
}
Byte NOR(Byte obj) { ////returns this Byte with its bits nor'ed
for (int i = 0; i < 8; i++) {
Bit[i] = ~(Bit[i] | obj.Bit[i]);
}
return *this;
}
Byte XOR(Byte obj) { //returns this Byte with its bits xor'ed
for (int i = 0; i < 8; i++) {
Bit[i] = Bit[i] ^ obj.Bit[i];
}
return *this;
}
Byte XNOR(Byte obj) { //returns this Byte with its bits xnor'ed
for (int i = 0; i < 8; i++) {
Bit[i] = ~(Bit[i] ^ obj.Bit[i]);
}
return *this;
}
};
// Driver program to test above functions
int main()
{
Byte *b = new Byte();
for (int i = 0; i < 8; i++) {
cout << b->Bit[i] << " ";
}
// rest of the code
}
Example usage:
If you like the answer, please consider upvoting, ;-)