Saturday 25 April 2020

circular rotation or cyclic shifts of bits in a given number

Problem link
https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/practice-problems/algorithm/lets-shift-2-36d90caa/description/


PseudoCode:

        if(c == 'R')
       {
            while(m--)
           {
                if(n%2== 0)                  // if n is even the units bit/LSB will be 0 so this 0 will become MSB
                        n= n / 2;                   // right shift means dividing by 2
                else
                       n=(n+65535)/2;       // if n is odd, LSB will be 1 and after rotation this 1 becomes MSB
            }
        }
        else
       {
             while(m--)
               {
                      n = n* 2;                  //left shift means multiplication by 2
                      if (n>65535)           // in 16 bits the largest number that can be stored is 65535
                            n = n - 65535;   //as it is rotation after 65535 it becomes 1,2,3,4,5 ............
               }
       }
               

No comments:

Post a Comment