Changing Bits Discussions | Algorithms | HackerRank
  • + 0 comments

    Python 3

    def changeBits(a, b, queries):
        a, b = int(a, 2), int(b, 2)
    
        for q in queries:
            q = q.rstrip().split()
            i = int(q[1])
            if q[0] == 'set_a':
                if q[2] == '1':
                    a |= 1<<i
                else:
                    a &= ~(1<<i)
            elif q[0] == 'set_b':
                if q[2] == '1':
                    b |= 1<<i
                else:
                    b &= ~(1<<i)
            else:
                c = a + b
                if not c & (1<<i):
                    print('0', end="")
                else:
                    print('1', end="")