Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void moveNumber(unsigned number, int amount);
  6. void convertDecToBin(vector<short> &binary, unsigned number);
  7. void writeDefaultBinary(vector<short> &binary);
  8. void writeConvertedBinary(vector<short> &binary, int amount);
  9.  
  10. int main()
  11. {
  12.     unsigned number;
  13.     int amount;
  14.     cout << "Podaj liczbe naturalna: ";
  15.     cin>>number;
  16.     cout << "Podaj wartosc o jaka przesunac bity: "; //ujemna w lewo, dodatnia w prawo
  17.     cin>>amount;
  18.     cout<<endl;
  19.     moveNumber(number, amount);
  20.     return 0;
  21. }
  22.  
  23. void moveNumber(unsigned number, int amount)
  24. {
  25.     vector<short> binary;
  26.     convertDecToBin(binary, number);
  27.     writeDefaultBinary(binary);
  28.     writeConvertedBinary(binary, amount);
  29. }
  30.  
  31. void convertDecToBin(vector<short> &binary, unsigned number)
  32. {
  33.     while(number!=0)
  34.     {
  35.         binary.insert(binary.begin(), number%2);
  36.         number/=2;
  37.     }
  38. }
  39.  
  40. void writeDefaultBinary(vector<short> &binary)
  41. {
  42.     cout<<"Domyslna wartosc binarna: ";
  43.     for(int i=0; i<binary.size(); ++i)
  44.         cout<<binary[i];
  45.     cout<<endl;
  46. }
  47.  
  48. void writeConvertedBinary(vector<short> &binary, int amount)
  49. {
  50.     int i, bufor;
  51.     amount%=binary.size();
  52.     if(amount<=0) i = -amount;
  53.     else i = -amount+binary.size();
  54.     bufor = i;
  55.     cout<<"Odwrocona wartosc binarna: ";
  56.     cout<<binary[i];
  57.     i=(++i)%binary.size();
  58.     for(; i!=bufor; i=(++i)%binary.size())
  59.     {
  60.         cout<<binary[i];
  61.     }
  62.     cout<<endl;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement