There was a difficulty with the cyclic permutation code on C. The full condition of the problem:

Write a program that allows you to enter two numbers n and k of the unsigned type from the keyboard, type them on the display, and using bit operations make a cyclic permutation n in the number n bits to the right.

With the first part of the problem there were no problems. Thank you in advance. :)

    2 answers 2

    unsigned m; ...... // k = k % (sizeof(n)*8); m = n << (sizeof(n)*8 - k); n = n >> k; n = n | m; 
    • 1. To protect yourself from going abroad, you can not do it, then in such cases there will always be zero 2. We remember the part that is rubbed off during the shift, at the same time moving it to the beginning 3. Shift 4. Add the part that has been rubbed off. - toxicdream
    • slightly mistaken in a hurry, already corrected, should work. - toxicdream

    total bits: sizeof (n) * 8
    Mask of the first k bits: maskA
    The mask of the second nk bits: maskB

     for(i=0;i<k;i++) maskA|=1<<i; for(i=k;i<n;i++) maskB|=1<<i; a=n&maskA; b=n&maskB; new_n=(b>>k)+(a<<k); 

    if I'm not mistaken then so ... did not run. but the princes are the same.