【每日一题1207】共同比特

def solution(x: int, y: int) -> bool:
    # 将整数转换为二进制
    x_bin = bin(x).replace('0b', '')
    y_bin = bin(y).replace('0b', '')
    # 转换为列表后反转列表
    x_list= list(x_bin)
    x_list.reverse()
    y_list = list(y_bin)
    y_list.reverse()
    # 最小比较长度
    len_min = min(len(x_list),len(y_list))
    count =0
    for i in range(len_min):
        if x_list[i]==y_list[i]:
            count+=1
    if count>=2:
        return True
    else:
        return False

assert solution(7, 10) is False
assert solution(7, 15) is True
assert solution(1, 2) is False
def common_bit(x:int,y:int)->bool:
    """
    如果x、y两个数共同比特位都为1的个数大于2,返回true
    :param x:
    :param y:
    :return:
    """
    x = str(bin(x).zfill(16))
    y = str(bin(y).zfill(16))
    count = 0
    for i in range(len(x)):
        if x[i] == y[i] and x[i]=="1":
            count+=1
    return count>=2
def common_bit(x:int,y:int)->bool:
    """
    如果x、y两个数共同比特位都为1的个数大于2,返回true
    :param x:
    :param y:
    :return:
    """
    if x>y:
        x,y = y,x
    x = str(bin(x))[2:]
    y = str(bin(y))[2:]
    count = 0
    for i in range(-1,-1-len(x),-1):
        if x[i] == y[i] and x[i]=="1":
            count+=1
    return count>=2
def solution(x, y):
    result = '{0:08b}'.format(x & y).count('1')
    if result > 1:
        return True
    else:
        return False

def solution(x: int, y:int) -> bool:
    a = '{0:b}'.format(x)
    b = '{0:b}'.format(y)
    list1=[]
    if len(b)>len(a):
        a=a.zfill(len(b))
    else:
        b=b.zfill(len(a))
    for i in zip(tuple(a), tuple(b)):
        if i[0] == '1' and i[1] == '1':
            list1.append('x')
    return True if len(list1)>=2 else False

def solution(x:int, y:int)->bool:
    return True if bin(x & y).count('1')>=2 else False
    def solution(x: int, y: int) -> bool:
        xb = bin(x)
        yb = bin(y)
        count = 0
        if len(xb) < len(yb):l = len(xb)
        else:l = len(yb)

        for i in range(1,l-1):
            if int(xb[-i]) == 1 and 1 == int(yb[-i]):
                count += 1

        return True if count > 1 else False

长得我头皮发麻

不影响 因为0参与按位与运算 结果一定为0

def solution(x: int, y: int) -> bool:
    x=bin(x).replace('0b','')
    y= bin(y).replace('0b', '')
    i=-1
    counts=0
    while i>=-len(x) and  i >=-len(y):
        if x[i]=='1'==y[i]:
            counts+=1
        if counts>=2:
            return True
        i -=1
    return False