How to convert signed to unsigned integer in python

How to convert signed to unsigned integer in python


  1. You have 2s-complement representations in mind; and,
  2. By (unsigned long) you mean unsigned 32-bit integer,

then you just need to add 2**32 (or 1 << 32) to the negative value.

For example, apply this to -1:

>>> -1
>>> _ + 2**32
>>> bin(_)

Assumption #1 means you want -1 to be viewed as a solid string of 1 bits, and assumption #2 means you want 32 of them.

Nobody but you can say what your hidden assumptions are, though. If, for example, you have 1s-complement representations in mind, then you need to apply the ~ prefix operator instead. Python integers work hard to give the illusion of using an infinitely wide 2s complement representation (like regular 2s complement, but with an infinite number of sign bits).

And to duplicate what the platform C compiler does, you can use the ctypes module:

>>> import ctypes
>>> ctypes.c_ulong(-1)  # stuff Pythons -1 into a C unsigned long
>>> _.value

Cs unsigned long happens to be 4 bytes on the box that ran this sample.

To get the value equivalent to your C cast, just bitwise and with the appropriate mask. e.g. if unsigned long is 32 bit:

>>> i = -6884376
>>> i & 0xffffffff

or if it is 64 bit:

>>> i & 0xffffffffffffffff

Do be aware though that although that gives you the value you would have in C, it is still a signed value, so any subsequent calculations may give a negative result and youll have to continue to apply the mask to simulate a 32 or 64 bit calculation.

This works because although Python looks like it stores all numbers as sign and magnitude, the bitwise operations are defined as working on twos complement values. C stores integers in twos complement but with a fixed number of bits. Python bitwise operators act on twos complement values but as though they had an infinite number of bits: for positive numbers they extend leftwards to infinity with zeros, but negative numbers extend left with ones. The & operator will change that leftward string of ones into zeros and leave you with just the bits that would have fit into the C value.

Displaying the values in hex may make this clearer (and I rewrote to string of fs as an expression to show we are interested in either 32 or 64 bits):

>>> hex(i)
>>> hex (i & ((1 << 32) - 1))
>>> hex (i & ((1 << 64) - 1)

For a 32 bit value in C, positive numbers go up to 2147483647 (0x7fffffff), and negative numbers have the top bit set going from -1 (0xffffffff) down to -2147483648 (0x80000000). For values that fit entirely in the mask, we can reverse the process in Python by using a smaller mask to remove the sign bit and then subtracting the sign bit:

>>> u = i & ((1 << 32) - 1)
>>> (u & ((1 << 31) - 1)) - (u & (1 << 31))

Or for the 64 bit version:

>>> u = 18446744073702667240
>>> (u & ((1 << 63) - 1)) - (u & (1 << 63))

This inverse process will leave the value unchanged if the sign bit is 0, but obviously it isnt a true inverse because if you started with a value that wouldnt fit within the mask size then those bits are gone.

How to convert signed to unsigned integer in python

Python doesnt have builtin unsigned types. You can use mathematical operations to compute a new int representing the value you would get in C, but there is no unsigned value of a Python int. The Python int is an abstraction of an integer value, not a direct access to a fixed-byte-size integer.

Leave a Reply

Your email address will not be published. Required fields are marked *