Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. const int MAX = 8; // max size of 8, 0-7
  9. void NOT_Operation(int array[MAX]); // NOT
  10. void AND_Operation(int array_1[MAX], int array_2[MAX]); // AND
  11. void OR_Operation(int array_1[MAX], int array_2[MAX]); // OR
  12. void CONVERT_Operation(int array[MAX]); // CONVERT
  13. void LSHIFT_Operation(int array[MAX], int N); // L SHIFT
  14.  
  15. int toPower(int value, int power); // function i made to take some value to some power
  16.  
  17. int main()
  18. {
  19.     int test_array_1[MAX] = {0, 0, 0, 0, 0, 0, 0, 1};
  20.     int test_array_2[MAX] = {1, 0, 0, 1, 1, 1, 0, 1};
  21.    
  22.     cout<< "NOT : ";
  23.     NOT_Operation(test_array_1); // testing NOT
  24.     cout<< "AND : ";
  25.     AND_Operation(test_array_1, test_array_2); // testing AND
  26.     cout<< "OR : ";
  27.     OR_Operation(test_array_1, test_array_2); // testing OR
  28.     cout<< "toPower Function : " << toPower(2,3) << endl; // testing toPower function
  29.     cout<< "CONVERT : ";
  30.     CONVERT_Operation(test_array_1); // testing CONVERT
  31.     cout<<"LEFT SHIFT : ";
  32.     LSHIFT_Operation(test_array_1, 0); // testing LEFT SHIFT
  33.    
  34. }
  35.  
  36. void NOT_Operation(int array[MAX]) // NOT OPERATION
  37. {
  38.    
  39.     for(int i = 0; i < MAX; i++)
  40.     {
  41.         if(array[i] == 0) // if its a 0 at position "i"
  42.         {
  43.             cout<< "1"; // output 1
  44.         } else
  45.         {
  46.             cout<<"0"; // otherwise output 0
  47.         }
  48.     }
  49.     cout<<endl;
  50. }
  51.  
  52. void AND_Operation(int array_1[MAX], int array_2[MAX]) // AND OPERATION
  53. {
  54.     for(int i = 0; i < MAX; i++)
  55.     {
  56.         if(array_1[i] == 1 && array_2[i] == 1) // if both values are 1
  57.         {
  58.             cout<<"1"; // output 1
  59.         } else
  60.         {
  61.             cout<<"0"; // otherwise ouput 0
  62.         }
  63.     }
  64.     cout<<endl;
  65. }
  66.  
  67. void OR_Operation(int array_1[MAX], int array_2[MAX]) // OR OPERATION
  68. {
  69.     for(int i = 0; i < MAX; i++)
  70.     {
  71.         if(array_1[i] == 1 || array_2[i] == 1) // if one of the values are 1
  72.         {
  73.             cout<<"1"; // output 1
  74.         } else
  75.         {
  76.             cout<<"0"; // otherwise ouput 0
  77.         }
  78.     }
  79.     cout<<endl;
  80. }
  81.  
  82. int toPower(int value, int power) // toPOWER FUNCTION
  83. {
  84.     int result = 1; // start with 1 bc we are multiplying
  85.    
  86.     for(int i = 0; i < power; i++) // loop "power" many times
  87.     {
  88.     result *= value; // mutliply value to result the amount of "power" times
  89.     }
  90.     return result; // return result
  91. }
  92.  
  93. void CONVERT_Operation(int array[MAX])
  94. {
  95.     int power = 7; // make power 7 because going from 7-1
  96.     int result = 0; // result variable
  97.    
  98.     for(int i = 0; i < MAX; i++)
  99.     {
  100.         result += array[i] * toPower(2, power); // add array value times 2 to power of power variable to result
  101.         power--; // decrease power
  102.     }
  103.     cout<< result << " in base 10." <<endl; // output result
  104. }
  105.  
  106. void LSHIFT_Operation(int array[MAX], int N)
  107. {
  108.     int temp[MAX]; // temp array
  109.    
  110.     for(int i = 0; i < MAX; i++)
  111.     {
  112.         temp[i] = array[i]; // copy over values from array
  113.     }
  114.    
  115.     for(int j = 0; j < N; j++) // repeat for N shifts
  116.     {
  117.         for (int i = 0; i < MAX - 1; i++) // shift all values excluding last
  118.         {
  119.             temp[i] = temp[i + 1]; // all values shifted over one
  120.         }
  121.         temp[MAX - 1] = 0; // last becomes 0
  122.     }
  123.    
  124.     for(int k = 0; k < MAX; k++)
  125.     {
  126.         cout<<temp[k]; // output values
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement