Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- using namespace std;
- const int MAX = 8; // max size of 8, 0-7
- void NOT_Operation(int array[MAX]); // NOT
- void AND_Operation(int array_1[MAX], int array_2[MAX]); // AND
- void OR_Operation(int array_1[MAX], int array_2[MAX]); // OR
- void CONVERT_Operation(int array[MAX]); // CONVERT
- void LSHIFT_Operation(int array[MAX], int N); // L SHIFT
- int toPower(int value, int power); // function i made to take some value to some power
- int main()
- {
- int test_array_1[MAX] = {0, 0, 0, 0, 0, 0, 0, 1};
- int test_array_2[MAX] = {1, 0, 0, 1, 1, 1, 0, 1};
- cout<< "NOT : ";
- NOT_Operation(test_array_1); // testing NOT
- cout<< "AND : ";
- AND_Operation(test_array_1, test_array_2); // testing AND
- cout<< "OR : ";
- OR_Operation(test_array_1, test_array_2); // testing OR
- cout<< "toPower Function : " << toPower(2,3) << endl; // testing toPower function
- cout<< "CONVERT : ";
- CONVERT_Operation(test_array_1); // testing CONVERT
- cout<<"LEFT SHIFT : ";
- LSHIFT_Operation(test_array_1, 0); // testing LEFT SHIFT
- }
- void NOT_Operation(int array[MAX]) // NOT OPERATION
- {
- for(int i = 0; i < MAX; i++)
- {
- if(array[i] == 0) // if its a 0 at position "i"
- {
- cout<< "1"; // output 1
- } else
- {
- cout<<"0"; // otherwise output 0
- }
- }
- cout<<endl;
- }
- void AND_Operation(int array_1[MAX], int array_2[MAX]) // AND OPERATION
- {
- for(int i = 0; i < MAX; i++)
- {
- if(array_1[i] == 1 && array_2[i] == 1) // if both values are 1
- {
- cout<<"1"; // output 1
- } else
- {
- cout<<"0"; // otherwise ouput 0
- }
- }
- cout<<endl;
- }
- void OR_Operation(int array_1[MAX], int array_2[MAX]) // OR OPERATION
- {
- for(int i = 0; i < MAX; i++)
- {
- if(array_1[i] == 1 || array_2[i] == 1) // if one of the values are 1
- {
- cout<<"1"; // output 1
- } else
- {
- cout<<"0"; // otherwise ouput 0
- }
- }
- cout<<endl;
- }
- int toPower(int value, int power) // toPOWER FUNCTION
- {
- int result = 1; // start with 1 bc we are multiplying
- for(int i = 0; i < power; i++) // loop "power" many times
- {
- result *= value; // mutliply value to result the amount of "power" times
- }
- return result; // return result
- }
- void CONVERT_Operation(int array[MAX])
- {
- int power = 7; // make power 7 because going from 7-1
- int result = 0; // result variable
- for(int i = 0; i < MAX; i++)
- {
- result += array[i] * toPower(2, power); // add array value times 2 to power of power variable to result
- power--; // decrease power
- }
- cout<< result << " in base 10." <<endl; // output result
- }
- void LSHIFT_Operation(int array[MAX], int N)
- {
- int temp[MAX]; // temp array
- for(int i = 0; i < MAX; i++)
- {
- temp[i] = array[i]; // copy over values from array
- }
- for(int j = 0; j < N; j++) // repeat for N shifts
- {
- for (int i = 0; i < MAX - 1; i++) // shift all values excluding last
- {
- temp[i] = temp[i + 1]; // all values shifted over one
- }
- temp[MAX - 1] = 0; // last becomes 0
- }
- for(int k = 0; k < MAX; k++)
- {
- cout<<temp[k]; // output values
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement