Skip to content

Instantly share code, notes, and snippets.

@gpratt
Last active August 29, 2015 14:24
Show Gist options
  • Save gpratt/3a23dd807afa932800a0 to your computer and use it in GitHub Desktop.
Save gpratt/3a23dd807afa932800a0 to your computer and use it in GitHub Desktop.
flip_hex_vaule
def flip_hex_value(hex_value):
has = ""
if hex_value.startswith("#"):
has = "#"
hex_value = hex_value.lstrip("#")
bits = bin(int(hex_value, 16))[2:]
flipped = "".join(["1" if bool(int(bit)) ^ True else "0" for bit in bits])
return has + "{0:0>{width}x}".format(int(flipped, 2), width=len(hex_value))
flip_hex_value("#262626")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment