Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void moveNumber(unsigned number, int amount);
- void convertDecToBin(vector<short> &binary, unsigned number);
- void writeDefaultBinary(vector<short> &binary);
- void writeConvertedBinary(vector<short> &binary, int amount);
- int main()
- {
- unsigned number;
- int amount;
- cout << "Podaj liczbe naturalna: ";
- cin>>number;
- cout << "Podaj wartosc o jaka przesunac bity: "; //ujemna w lewo, dodatnia w prawo
- cin>>amount;
- cout<<endl;
- moveNumber(number, amount);
- return 0;
- }
- void moveNumber(unsigned number, int amount)
- {
- vector<short> binary;
- convertDecToBin(binary, number);
- writeDefaultBinary(binary);
- writeConvertedBinary(binary, amount);
- }
- void convertDecToBin(vector<short> &binary, unsigned number)
- {
- while(number!=0)
- {
- binary.insert(binary.begin(), number%2);
- number/=2;
- }
- }
- void writeDefaultBinary(vector<short> &binary)
- {
- cout<<"Domyslna wartosc binarna: ";
- for(int i=0; i<binary.size(); ++i)
- cout<<binary[i];
- cout<<endl;
- }
- void writeConvertedBinary(vector<short> &binary, int amount)
- {
- int i, bufor;
- amount%=binary.size();
- if(amount<=0) i = -amount;
- else i = -amount+binary.size();
- bufor = i;
- cout<<"Odwrocona wartosc binarna: ";
- cout<<binary[i];
- i=(++i)%binary.size();
- for(; i!=bufor; i=(++i)%binary.size())
- {
- cout<<binary[i];
- }
- cout<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement