Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I have presented a code bit that appeared underneath: 

def shift():

    m = 8

    low = '10100111'

    lw = int(low, 2)

    lw = lw << 1

    l_bin = bin(lw)[2:].zfill(m)

Output ==> 101001110(9-bits)

while my desired output ==> 01001110(8-bits)

I could comprehend that the right shifting the lw variable causes the number an incentive to move from 167 to 334. However, I need the yield to be the ideal yield.

1 Answer

0 votes
by (26.4k points)

You need to cover the upper bits on the off chance that you need to copy a byte (like it would carry on in C): 

lw = (lw<<1) & 0xFF

Obviously, that is, on the off chance that you hold m set to 8 or it will not work. 

In the event that m changes, you can process (or pre-register) the mask value this way:

mask_value = 2**m-1

lw=(lw<<1) & mask_value

Looking for a good python tutorial course? Join the python certification course and get certified.

For more details, do check out the below video tutorial...

Related questions

0 votes
1 answer
asked Nov 26, 2019 in Java by Nigam (4k points)
0 votes
1 answer
0 votes
1 answer
0 votes
4 answers
asked Apr 4, 2021 in Python by laddulakshana (16.4k points)
0 votes
4 answers

Browse Categories

...