I have function to transcode input hexadecimal number. For example I have initial format of number like this
And function transcodes it to this format:
I will return result from function as a string using hex built-in function. I tried something like this, but that didn't work:
def task22(number) -> str:
"""
Returns transcoded hexadecimal string, this function uses bitwise operators to transcode given value
:param number: hexadecimal number to transcode
:return: Transcoded hexadecimal string
"""
a = number & 0b111111111111111
b = number & 0b111111111111111
c = number & 0b11
a = a << 17
b = b >> 13
c = c >> 30
return hex(a | b | c)
How can I use bitwise operators to transcode number? I need to use bitwise shifts to the left and right but I have no idea how to do it for formats described above.
Solved. The problem was I needed to process all segments using bitwise AND operator with 32 zeros (because number of all bits is 32) replaced by 1 where each segment is located in the sequence of bits like this:
def task22(number) -> str:
"""
Returns transcoded hexadecimal string, this function uses bitwise operators to transcode given value
:param number: hexadecimal number to transcode
:return: Transcoded hexadecimal string
"""
a = number & 0b00000000000000000111111111111111
b = number & 0b00111111111111111000000000000000
c = number & 0b11000000000000000000000000000000
a = a << 17
b = b >> 13
c = c >> 30
return hex(a | b | c)