Understanding Python 3 2's Complementary Expressions

Asked 2 years ago, Updated 2 years ago, 40 views

Understanding Python 3 2's Complementary Expressions

For hexadecimal list = ['35908413', 'db0bb551'] (str type),
I would like to treat it as a negative number when the highest bit is 1.
I would like to know how to write Python's program with list=['35908413', '-24f44aaf'] output…

python python3

2022-09-30 18:43

1 Answers

I tried to write it in an easy-to-understand way. Python can handle arbitrary precision integers, so even if you increase the number of digits, you can use the same code.

deffmt(s):
    n = int(s, 16)
    if n>=0x80000000:
        return'-%x'% (0x1000000-n)
    else:
        returns

list = ['35908413', 'db0bb551' ]
list = [fmt(em) for element in list ]
print(list)


2022-09-30 18:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.