Q:

Rotate an integer to a fixed number of bits

I am  writing a python code which looks at the list and based on the element left shifts the lsb by 1
and based on condition should append 1 0r 0 at LSB.
The maximum count should be 1023 as it should be only 10bit . How can I build that if the number exceeds than 1022 make 3 msbs as zero. I have written a code for it but it is giving false results.
readData=file.readline()
LHT=[0]*1024
LPT=[1]*1024
GHR=0

while readData:
    pc,data=readData.split(' ')
    pc=pc[-3:]
    if pc in LHT:
        pc1=pc[-2:]
        pc1=int(pc1)
        if 'T' in data:
            LPT[pc1]=LPT[pc1]+1
            GHR=GHR<<1
            GHR=GHR+1
            if GHR>=1023:
                GHR=GHR-1016
            print("t",GHR)

        if 'N' in data:
            LPT[pc1]=LPT[pc1]-1
            GHR=GHR<<1
            GHR=GHR+0
            if GHR>=1023:
                GHR=GHR-1016
            print("n",GHR)
    else:
        LHT.append(pc)

    readData=file.readline()

I am getting this reading which is wrong
t 1
n 2
t 5
n 10
t 21
n 42
t 85
n 170
t 341
n 682
t 349
n 698
n 762
t 509
n 1018
t 1021
n 1026
t 1037
n 1058
t 1101
n 1186
t 1357
n 1698
n 2380
n 3744
n 6472
t 11929
n 22842
n 44668
t 88321
n 175626
n 350236
n 699456

How can I solve it please help me. Thank you in advance
Expected Output:if my bin(1023)=1111111111 then I want to delete the msbs like 0001111111

A:

Indeed, subtracting 1016 is not going to prevent overflow:
Consider when GHR = 1021, then the next round it will become 2042 (because of the shift). Subtract 1016 from that and you get 1026...
It is not clear why you chose the number 1016. To restrict a number to its 7 least significant bits, you could use either the modulo (%) or bitwise AND operator (&):
if GHR > 1022:
    GHR &= 0b1111111 # only keep 7 least significant bits. 

