Currently I first check if it is a digit at all using isdigit() , then I check if it exceeds the value of 2^32 (assuming I don't care about unsigned values). Use NumPy with the appropriate integer size and the overflow is more C like: 32 bit: >>> np.uint32(2**32-3) + np.uint32(5) __main__:1: RuntimeWarning: overflow encountered in uint_scalars 2. Notice that there is no long type in Python 3. Example (Python) In each case, we will check whether the number is in a range of 32-bit integers. Let us see the implementation to get a better understanding. may give unexpected results. Assuming you're checking for signed integers, you need abs(sum) > 2 ** 31 - 1 (or, even better, abs(sum) > (1 << 31) - 1) – cs95 Aug 6 '17 at 5:03. On a 32-bit system, it would return 4 bytes. One of the things I need to do is make sure an int can be represented in 4 bytes. We can solve this problem quickly in Python. – Agostino Apr 29 '15 at 18:44 but how to check user input is a number. It's not really in the Python spirit, but in general you can set the Is there a portable way to do that? Reverse digits of an integer with overflow handled Write a program to reverse an integer assuming that the input is a 32-bit integer. ; bin() function appends 0b as a prefix in binary representation of number, skip first two characters of binary representation and reverse remaining part of string. There are following Bitwise operators supported by Python language. Well, the sign bit's gonna … (4 replies) I'm working with marshaling data over a binary wire protocol. Approach is very simple, Convert integer number into it’s binary representation using bin(num) function. Unless you know you are working with numbers that are less than a certain length, for instance numbers from arrays of integers, shifts, rotations, etc. So the following would return 32 if you're running 32-bit python and 64 if you're running 64-bit python: Python 2. import struct;print struct.calcsize("P") * 8 Python 3. import struct;print(struct.calcsize("P") * 8) 64 bit: The largest 32-bit signed integer is (1 << 31) - 1, which is 2147483647. – Tim Peters Aug 6 '17 at 4:58. Jean-Paul The bounds for the sizes are similarly simple to determine and check. Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands (a & b) (means 0000 1100) | Binary OR It copies a bit if it exists in either operand. Check if python version is 64 or 32 bit Check if python version is 64 or 32 bit Just do this following in python interpreter or python source code file: You will get 64 or 32 to indicate which python version do you have installed. On a 64-bit system, it would return 8 bytes. I'm using struct.pack() to handle the low-level encoding of ints. And finally convert them to integer before returning it, for a positive number, simply reverse the string and make it integer before returning.