Question

In: Computer Science

please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects....

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

Solutions

Expert Solution

#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, ;-)


Related Solutions

IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should...
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should have the following properties: Movie Name Director Name Year Released WasSuccessful (this should be a boolean and at least 2 should be false) Genre Loop through all of the objects in Array If the movie is successful, display all the movie information on the page. These movies were a success: Title: Forrest Gump Year Realeased: 1994 Director: Robert Zemeckis Genre: Comedy
C++ Classes & Objects Create a class named Student that has three private member: string firstName...
C++ Classes & Objects Create a class named Student that has three private member: string firstName string lastName int studentID Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects. In the 'main' function do the following (1) Create a student object "student1". (2) Use set methods to assign StudentID: 6337130 firstName: Sandy lastName: Santos (3) Display the students detail using get functions in standard output using cout: Sandy Santos 6337130
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Please create an array of Leg objects, one constructor that takes three parameters as constant C...
Please create an array of Leg objects, one constructor that takes three parameters as constant C string, and one number representing the distance in miles between the two cities Write a code block to create a static array (that is, not dynamic and not a vector) of 3 Leg objects using city names of your choosing. That's THREE objects, each created using THREE parameters. For example, the Leg class declaration looked like, class Leg { const char* const startCity; const...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. YES, an integer can be put into and retrieved from a character array. It's all binary under the hood. In some languages this is very easy (C/C++)...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (using name & id), and - keeps track of a user's available balance. - how many transactions (deposits and/or withdrawals) are made. public class BankAccount { private int id; private String name private double balance; private int numTransactions; // ** Please define method definitions for Accessors and Mutators functions below for the class private member variables string getName();...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to store game states and at least two member functions for adding players’ moves and printing the game board. Write a driver to test your class. (2). The data type of the two-dimensional array can be either int or char. As a passlevel program, the size of the board can be hardcoded to 10 or a constant with a pre-set value, anything between 4 and...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT