Search code examples
pythonbit-shift

Python script to rotates the bits in its input two places to the right


Write shift script that expect a bit string as an input. The script shift Right rotates the bits in its input two places to the right.

sample data: ‘0111110’, '01110', '00011111'

Shift : ‘1001111’, '10011', '11000111'

I tried like to access & modify it's first two digit & last two digit by converting into b=str(input) then finding b[0], b[1], b[-1],b[-2], but it didn't work. Please help, thanks a lot


Solution

  • def shift_string(s):
       return s[-1] + s[:-1]
    
    s = "01110"
    for x in range(5):
      s = shift_string(s)
      print(x, s)
    

    prints out

    0 00111
    1 10011
    2 11001
    3 11100
    4 01110
    

    so to shift s twice,

    s = shift_string(shift_string(s))