[Hot News] [SOLVED]: How To Convert Signed To Unsigned Integers In Python

[Hot News] [SOLVED]: How To Convert Signed To Unsigned Integers In Python
Di Posting Oleh : Crew Blog
Kategori : FAQ PROGRAMMING PYTHON Q&A Solved TECHNOLOGY TUTORIALS

How to convert a signed integer in Python to an unsigned integer? This is a question that we regularly get asked in several lectures and forums across the internet, so let us do a quick tutorial about it.

Let us assume you have a signed integer, lets say having a value of:

i = -6884376

In this case, in order to convert this signed integer to an unsigned integer in Python, all you would have to do is to take a bitwise mask for a 32-bit integer (Here, we are assuming that your signed variable has a size of 32-bits) and bitwise logical and with the signed variable. The resulting value will be an unsigned integer value by Python, corresponding to the C unsigned variable you would normally see in C programming.

ui = i & 0xffffffff

Now, printing the value of ui results in the python equivalent unsigned integer value for i, which so happens to be:

>4288082920