Implement a cyclic right shift for K discharges.

Example: K = 2

00011010 10 => 10 00011010.

if not difficult, then with more or less clear explanations, I will be very grateful

Closed due to the fact that the essence of the question is incomprehensible by the participants Vladimir Martyanov , aleksandr barakin , user194374, VenZell , fori1ton 10 Mar '16 at 8:17 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • The question is what? Minimal knowledge of binary arithmetic or googling will give you the code you are looking for. Moreover, for Visual Studio there are special “functions” that implement this. - Vladimir Martyanov
  • Please specify for which representation of the bits you need a shift? Is it an integer, or a string of bits of arbitrary length, or what? - Harry
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

4 answers 4

Read the code and understand:

#include <iostream> #include <bitset> using namespace std; const unsigned int BYTE = 10; // количество разрядов const unsigned int STEP = 2; // на сколько шагов нужно будет сдвинуть int main(){ bitset<BYTE> bit(106), part1, part2, result; cout << "bit\t" << bit << endl; // исходное число // результат будет составлен из двух частей part1 = bit >> STEP; // первая часть, два сдвига вправо cout << "part1\t" << part1 << endl; part2 = bit << BYTE - STEP; // вторая часть, восемь сдвигов влево (десять разрядов числа минус два шага) cout << "part2\t" << part2 << endl; result = part1 | part2; // объеденияем части cout << "result\t" << result << endl; // результат объединения частей return 0; } 

conclusion:

result

    like that

     unsigned int x = 0x1234ABCD; int k = 2; x = (x >> k) | (x << (32 - k)); 

    instead of 32, substitute the required value if different

    • 2
      It seems like a man asked with comments. But what is 32? 32 bits? And if he, as in the example 10 bits? Well, for example, take a simple 110 (6). After a shift of 2 should result in 101 (5). And what happens in your example? Zaminusovali man. Fine. Congratulations. So where is the correct answer? - Max ZS
    • Moving an int to the right is unsafe and depends on the implementation. For this formula to work, x >> k must necessarily use a logical shift, and this is not guaranteed. - ixSci
    • @ixSci will add my 2 kopecks: and the left-shift of a negative number is generally UB. - αλεχολυτ
    • yes, UB, added unsigned - Yuriy Orlov

    std::vector<bool> + std::rotate = execution result

     #include <iostream> #include <algorithm> #include <vector> using BinVec = std::vector<bool>; void print(const BinVec& v) { for(bool b : v) std::cout << b; std::cout << "\n"; } void shr(BinVec& v, int k) { k %= v.size(); std::rotate(v.begin(), v.end() - k, v.end()); } int main() { BinVec v(8); v[0] = 1; print(v); shr(v, 2); print(v); } 

      C ++ has a standard library to work with bits. The code created with its help is probably not the most optimal, but very visual. Shift implementation example :

       #include <iostream> #include <bitset> int main() { std::bitset<10> baz ("0001101010"); // 10-количество бит в числе for(int i=0; i<2; i++) // 2-на сколько бит сдвигаем { bool temp = baz[0]; baz >>= 1; // >> - сдвиг вправо baz[baz.size()-1] = temp; } std::cout << baz; }