Is Python's ctypes.c_long 64 bit on 64 bit systems?

前端 未结 4 831
囚心锁ツ
囚心锁ツ 2020-12-19 05:20

In C, long is 64 bit on a 64 bit system. Is this reflected in Python\'s ctypes module?

相关标签:
4条回答
  • 2020-12-19 06:05

    If a C long is 64-bit (like it is on LP64 and ILP64 systems, pretty much any 64-bit system other than Windows), then so is ctypes.c_long. If a C long isn't 64-bit (like on LLP64 systems such as 64-bit Windows) then a ctypes.c_long isn't, either.

    0 讨论(0)
  • 2020-12-19 06:09

    Actually no.

    On a Windows 64-bit system, long is 32 bits.

    Python 3.1.2 (r312:79149, Mar 20 2010, 22:55:39) [MSC v.1500 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import ctypes
    >>> ctypes.c_long(2**31)
    c_long(-2147483648)
    >>> ctypes.c_long(2**31+1)
    c_long(-2147483647)
    >>> ctypes.c_long(2**31-1)
    c_long(2147483647)
    >>>
    

    See What is the bit size of long on 64-bit Windows?

    0 讨论(0)
  • 2020-12-19 06:15

    The size of long depends on the memory model. On Windows (LLP64) it is 32-bit, on UNIX (LP64) it is 64-bit.

    If you need a 64-bit integer, use c_int64.

    If you need a pointer-sized integer, use c_void_p (“The value is represented as integer”).

    0 讨论(0)
  • 2020-12-19 06:19

    Yes.

    >>> ctypes.c_long(2**50)
    c_long(1125899906842624)
    >>> ctypes.c_long(2**64)
    c_long(0)
    >>> ctypes.c_long(2**63)
    c_long(-9223372036854775808)
    
    0 讨论(0)
提交回复
热议问题