Solutions for Toggle Bits

    • @Victorfreef
    • @Liam, submitted in Python 2, achived 100% score
    def solve(a, b, c):
        # your code here
        return
    
    def solve(n,l,r):
    
        # calculating a number
        # 'num' having 'r'
        # number of bits and
        # bits in the range l
        # to r are the only set bits
        num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1)
    
        # toggle bits in the
        # range l to r in 'n'
        # Besides this, we can calculate num as: num=(1<<r)-l .
    
        # and return the number
        return (n ^ num)
    
    • @Victorfreef
    • @Liam, submitted in Python 3, achived 100% score
    
    def solve(n,l,r):
    
        # calculating a number
        # 'num' having 'r'
        # number of bits and
        # bits in the range l
        # to r are the only set bits
        num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1)
    
        # toggle bits in the
        # range l to r in 'n'
        # Besides this, we can calculate num as: num=(1<<r)-l .
    
        # and return the number
        return (n ^ num)