Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def correct_shift_v2(ac, n):
- if ac >= 0:
- flag = False
- ac = bin(ac << n).lstrip("0b")[-32:].zfill(32)
- else:
- flag = True
- ac = bin(ac << n).lstrip("-0b")[-32:].zfill(32)
- if ac[0] == "1":#JavaScript这种情况会溢出成为负数
- #翻转替换末尾的0以及挨着的1
- temp = "1" + re.findall("(.*?)1",ac[::-1])[0]
- ac = ac.replace(temp, "")
- ac = list(ac)
- for i, c in enumerate(ac):#取反
- if c == "1":
- ac[i] = "0"
- elif c == "0":
- ac[i] = "1"
- else:
- pass
- ac = "".join(ac) + temp
- ac = -int(ac ,2)
- else:
- ac = int(ac ,2)
- if flag == True:
- ac = -ac
- else:
- pass
- return ac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement