I want to differentiate between 32-bit and 64-bit integers in Python. In C it's very easy as we can declare variables using int_64
and int_32
. But in Python how do we differentiate between 32-bit integers and 64-bit integers?

- 6,030
- 2
- 25
- 34

- 81
- 1
- 5
-
2Why do you want to know? Python's datatypes don't map directly. – Chris Morgan Dec 29 '12 at 07:19
-
Actually I want to implement the udp tracker protocol in which it says I need to pack data as follows [64-bit data][32-bit data][32-bit data] – user101847 Dec 29 '12 at 08:09
-
1That doesn't end up a question of Python datatypes. That becomes a question of encoding data for transfer on the wire, which is quite a different matter. – Chris Morgan Dec 29 '12 at 08:26
-
Nope.. actually in c we can implement it by declaring variables of data-types int_64 and int_32 and then concatinating it we can get the packed data as discussed above. But in python there is no way we can find whether the integer is 32 bit integer or 64 bit integer.We can only find out that the number is of integer type or of long type. But i am asking can we find out whether the number is 32-bit integer or 64-bit integer – user101847 Dec 29 '12 at 08:30
-
1*You can't.* There is no such thing in Python. In Python 2, `int` may be 32-bit or 64-bit, and `long` is of arbitrary length. You can determine whether a number will *fit* in 32 or 64 bits, and you can attempt to pack a number into a binary format of suitable size, but that's a quite different question. – Chris Morgan Dec 29 '12 at 10:17
4 Answers
There's no need. The interpreter handles allocation behind the scenes, effectively promoting from one type to another as needed without you doing anything explicit.

- 25,682
- 5
- 48
- 63
-
But I need to send data as [64-bits data][32-bits data][32-bits data] So how to do it in python? – user101847 Dec 29 '12 at 08:15
-
What if I want to check if the number entered by the user is signed 32-bit or unsigned 32-bit? – Shedrack Apr 02 '20 at 12:08
-
1
Basically, you don't. There's no reason to. If you want to deal with types of known bit size, look at numpy
datatypes.
If you want to put data into a specified format, look at the struct module.

- 242,874
- 37
- 412
- 384
-
But I need to pack data as [64-bits integer][32-bit integer][32-bit integer] and send it onto the network. So how this should be done in python? – user101847 Dec 29 '12 at 08:18
-
@user101847: Wanting to *make* data fit a certain byte format is totally different than wanting to know how many bytes the standard Python types have. For your question, look at the struct module. – BrenBarn Dec 29 '12 at 08:25
-
I agree that one can pack and unpack data using struct but can we pack the 64-bit integer with 32-bit integer. As far as I have searched, there is no way to distinguish between 64-bit integer and 32-bit integer.You can tell whether the number is int or long but is there any inbuilt function which can tell that this number is 32-bit or 64-bit integer.In c we can say just by declaring variable using datatypes int_64 and int_32. – user101847 Dec 29 '12 at 09:26
-
@user101847: You don't need to distinguish between them. You just take the integer and pack it into the number of bits you need. It's true there will be a problem if the number you need to pack into 32 bits is too big to fit into 32 bits, but that's not because it "is a 32-bit integer". It's because the number is too big to fit into 32 bits. – BrenBarn Dec 29 '12 at 19:09
The struct
module mentioned in the other answers is the thing you need.
An example to make it clear.
import struct
struct.pack('qii', # Format string here.
100, # Your 64-bit integer
50, # Your first 32-bit integer
25) # Your second 32-bit integer
# This will return the following:
'd\x00\x00\x00\x00\x00\x00\x002\x00\x00\x00\x19\x00\x00\x00'
documentation for the formatting string.

- 3,460
- 2
- 13
- 17
The following snippet from an ipython interpreter session indicates one way of testing the type of an integer. Note, on my system, an int
is a 64-bit data type, and a long
is a multi-word type.
In [190]: isinstance(1,int)
Out[190]: True
In [191]: isinstance(1,long)
Out[191]: False
In [192]: isinstance(1L,long)
Out[192]: True
Also see an answer about sys.getsizeof
. This function is not entirely relevant, since some additional overhead bytes are included. For example:
In [194]: import sys
In [195]: sys.getsizeof(1)
Out[195]: 24
In [196]: sys.getsizeof(1L)
Out[196]: 28

- 1
- 1

- 8,593
- 2
- 22
- 37