mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-24 17:31:29 -04:00
play with bits
This commit is contained in:
parent
c77eb0dd00
commit
4b22537ee7
5 changed files with 73 additions and 0 deletions
18
bit_manipulation/convert_to_hex.py
Normal file
18
bit_manipulation/convert_to_hex.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
def convert_to_hex(num: int) -> str:
|
||||
|
||||
hex_chars = "0123456789abcdef"
|
||||
size = 32
|
||||
base = 16
|
||||
|
||||
if num == 0:
|
||||
return "0"
|
||||
|
||||
if num < 1:
|
||||
num += 2**size
|
||||
|
||||
result = ""
|
||||
while num:
|
||||
result += hex_chars[num % base]
|
||||
num //= base
|
||||
|
||||
return result[::-1]
|
Loading…
Add table
Add a link
Reference in a new issue