Assuming:

  1. You have 2's-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
-1
>>> _ + 2**32
4294967295L
>>> bin(_)
'0b11111111111111111111111111111111'

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 1's-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 2's complement representation (like regular 2's 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 Python's -1 into a C unsigned long
c_ulong(4294967295L)
>>> _.value
4294967295L

C's unsigned long happens to be 4 bytes on the box that ran this sample.

Answer from Tim Peters on Stack Overflow
Top answer
1 of 7
146

Assuming:

  1. You have 2's-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
-1
>>> _ + 2**32
4294967295L
>>> bin(_)
'0b11111111111111111111111111111111'

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 1's-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 2's complement representation (like regular 2's 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 Python's -1 into a C unsigned long
c_ulong(4294967295L)
>>> _.value
4294967295L

C's unsigned long happens to be 4 bytes on the box that ran this sample.

2 of 7
95

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
4288082920

or if it is 64 bit:

>>> i & 0xffffffffffffffff
18446744073702667240

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 you'll 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 two's 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 f's as an expression to show we are interested in either 32 or 64 bits):

>>> hex(i)
'-0x690c18'
>>> hex (i & ((1 << 32) - 1))
'0xff96f3e8'
>>> hex (i & ((1 << 64) - 1)
'0xffffffffff96f3e8L'

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))
-6884376

Or for the 64 bit version:

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

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

🌐
Reddit
reddit.com › r/learnpython › unsigned string to signed integer?
r/learnpython on Reddit: Unsigned String to Signed Integer?
November 14, 2023 -

I have a string (list member) that's being received as 65520, for instance. However this value is being received from a register that's a regular INT 2's complement representation. So really it's -16 in disguise. Obviously that's a problem on the sending side, when it does the INT to ASCII it should just encode it as -16, but it doesn't/can't.

When I read this value into an INT it of course reads the INT as 65520 since it has no way of knowing the original representation. Is there a way to force the ASCII to INT conversion to interpret the string value as a signed INT? So that input string = 65520 outputs INT = -16?

🌐
Reddit
reddit.com › r/learnpython › convert unsigned int to signed int in python 3
r/learnpython on Reddit: Convert unsigned int to signed int in Python 3
February 19, 2019 -

I have an integer from the result of binascii.crc32(). In Python 2, this function returned a signed int. However, in Python 3, it has been changed to always return an unsigned int. I am porting a piece of software from 2 to 3, and one of the things it does is calculate the crc and pack it with struct.pack(">l", crc32). This now causes an error as ">l" expects -2147483648 <= number <= 2147483647 but the crc can now exceed the upper limit.

How would I go about converting the crc to a signed value? As I understand Python doesn't have a concept of signed/unsigned, so you can't do signed_int = (int)unsigned_int; like you can in C.

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-signed-to-unsigned-integer-in-python
How to convert signed to unsigned integer in Python ? - GeeksforGeeks
April 5, 2021 - The bitwise AND operator can be used with 0xFFFFFFFF to keep only the lower 32 bits of an integer. ... The & operator keeps the lower 32 bits of the integer. For -10, this produces its 32-bit unsigned representation. A reusable function can perform the conversion for different signed integer values.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 359675 › signed-and-unsigned-int-in-python
Signed and Unsigned int in python [SOLVED] | DaniWeb
A signed integer just uses one of the bits as a sign, so an 8 bit unsigned can store 0-->255, and an 8 bit signed -127-->127 because one bit is used as a sign. There is bitstring, bitbuffer and bitarray, Also, check PyPi for existing packages ...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-signed-to-unsigned-integer-in-python
How to Convert Signed to Unsigned Integer in Python?
March 27, 2026 - A signed 32-bit integer ranges from -2,147,483,648 to 2,147,483,647, while an unsigned 32-bit integer ranges from 0 to 4,294,967,295.
🌐
IncludeHelp
includehelp.com › python › signed-and-unsigned-integer-arrays-in-python.aspx
Signed and Unsigned Integer Arrays in Python
May 3, 2025 - # unsigned integer array a = arr.array ("I", [10, 20, 30, 40, 50]) # signed integer array b= arr.array ("i", [10, -20, 30, -40, 50]) # importing array class to use array import array as arr # an unsigned int type of array # declare and assign elements a = arr.array ("I", [10, 20, 30, 40, 50] ) # print type of a print ("Type of a: ", type (a)) # print array print ("Array a is: ") print (a) # a signed int type of array # declare and assign elements b = arr.array ("i", [10, -20, 30, -40, 50] ) # print type of a print ("Type of b: ", type (a)) # print array print ("Array b is: ") print (b)
Find elsewhere
🌐
Kristrev
kristrev.github.io › programming › 2013 › 06 › 28 › unsigned-integeres-and-python
Unsigned integers and Python
June 28, 2013 - The easiest (and portable!) way to get unsigned integers in Python is to import the required types from the ctypes module. However, sometimes you need to convert Pythons ‘normal’ integers to unsigned values. As will be explained, the values are still signed integers (or long), but this has little effect on the application.
🌐
AI_FOR_ALL
kiran-parte.github.io › aiforall › blog-post-4.html
Python 101: DATA TYPES Ⅰ - NUMBERS
April 10, 2021 - The int in Python acts like a signed integer. The signed integer can take both positive and negative values, whereas an unsigned integer can take only positive values. The positive integers are stored in the memory in their binary form and the ...
🌐
SICORPS
sicorps.com › h0me › python’s unsigned converters
Python's Unsigned Converters -
April 4, 2024 - In short, a signed integer has a bit reserved specifically for indicating whether the number is positive or negative (known as the “sign bit”), while an unsigned integer doesn’t have this luxury all its bits are used to represent magnitude. So why would we want to convert between these two formats in Python?
🌐
Insanely Low-Level
ragestorm.net › blogs
Python: Converting Signedness « Insanely Low-Level
November 30, 2007 - If you pass back -1 to Python and format it as unsigned long, you will get 2**32-1. Now suppose you really want to treat the number inside your Python code as signed integer, you will have to convert 2**32-1 to -1 somehow.
🌐
Sololearn
sololearn.com › en › Discuss › 3320064 › python-and-unsigned-int
Python and unsigned int | Sololearn: Learn to code for FREE!
March 12, 2025 - sl_scroll_/de/Discuss/1953300/python-artificial-intelligence-Pending · Maximum Storage Duration: SessionType: HTML Local Storage ... sl_scroll_/de/Discuss/2152622/un-franais-pour-mexpliquer-dans-les-grosses-lignes-ce-que-a-signifie-jai-pas-tout-comprisPending
🌐
Python
docs.python.org › 3 › c-api › long.html
Integer Objects — Python 3.14.7 documentation
Returns (unsigned long long)-1 on error. Use PyErr_Occurred() to disambiguate. Changed in version 3.8: Use __index__() if available. Changed in version 3.10: This function will no longer use __int__(). int PyLong_AsInt32(PyObject *obj, int32_t *value)¶ · int PyLong_AsInt64(PyObject *obj, int64_t *value)¶ · Part of the Stable ABI since version 3.14. Set *value to a signed C int32_t or int64_t representation of obj.
🌐
YouTube
youtube.com › watch
PYTHON : How to convert signed to unsigned integer in python - YouTube
PYTHON : How to convert signed to unsigned integer in python [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYTHON : How to co...
Published: December 5, 2021