13/14 Just Flip Out solution with function for flipping


#1

Just thought somebody might find it interesting.

a = 0b11101110

  • define a function for flipping bits

def fliparooni(x):

  • find the length of the variable you want to flip

    length = len(bin(x))-2

  • the mask, a string for now

    mask = "0b"

  • add +1 to your mask until it has enough of them to flip the variable

    for i in range(length):
    mask += "1"

  • turn your mask into a intreger with a base of 2 by using the int function

    mask = int(mask, 2)

  • flip

    desired = a ^ mask

  • print the binary string

    print bin(desired)

  • call our variable

fliparooni(a)


#2

Yeeesssss, I definitely find this interesting. I struggled with this for a bit. I am having some trouble grasping the filetypes that the variables exist as and what they need to be to to utilize the bitwise operators. Is it as easy as saying that everything needs to be an integer? I am not understanding why an input such as 0b001 is treated as an integer.