# -*- coding: utf-8 -*-
# @author   : yuchangqing
# @date     : 2021/5/25
# @File     : unsigned_right_shift.py
# @Software : PyCharm
import ctypes


def int_overflow(val):
    maxint = 2147483647
    if not -maxint - 1 <= val <= maxint:
        val = (val + (maxint + 1)) % (2 * (maxint + 1)) - maxint - 1
    return val


def unsigned_right_shitf(n, i):
    # 数字小于0，则转为32位无符号uint
    if n < 0:
        n = ctypes.c_uint32(n).value
    # 正常位移位数是为正数，但是为了兼容js之类的，负数就右移变成左移好了
    if i < 0:
        return -int_overflow(n << abs(i))
    # print(n)
    return int_overflow(n >> i)


# 参数分别是要移的数字和移多少位
ret = unsigned_right_shitf(20, 2)
print(ret)
print(20 >> 2)
