Python Forum
[closed] check whether an integer is 32 or 64 bits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[closed] check whether an integer is 32 or 64 bits
#1
Hi,

A basic question: how to check if the variable t1 is wheter a 32 bits integer or 64 one? Natively I've been thinking its a 32 bits but I've not so sure.

Thanks

import numpy as np

t = 2.0

t1 = int(t)
print(f"t1 = {type(t1)}")


t2 = np.int32(t)
print(f"t2 = {type(t2)}")

t3 = np.int64(t)
print(f"t3 = {type(t3)}")
Reply
#2
Python's native int type is unbounded,meaning it can grow as large as the memory allows.
NumPy has fixed bit-width like eg int32 or int64 and also int8, int16.
Can use .bit_length() to check both standard Python and NumPy(has no own bit_length() check).
>>> t1 = 30
>>> t1.bit_length()
5
>>> t2 = np.int32(t)
# Convert to int to use bit_length()
>>> int(t2).bit_length()
5
So if go over max of 32bit(2147483647),will get a error as should in NumPy
>>> t = 2147483648
>>> t.bit_length()
32
>>> t2 = np.int32(t)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
OverflowError: Python int too large to convert to C long

# As mention standar int is unbounded
>>> t = 21474836480000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>> t.bit_length()
304
Reply
#3
t1 is an int, not 32 or 64. int does not have a size.
Reply
#4
I got it, thanks
Reply
#5
did you see this?

https://www.youngwonks.com/blog/Coding-o...and-PyGame
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 6 1,672 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,347 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  How to rotate bits ? korenron 2 1,741 Mar-23-2022, 04:05 PM
Last Post: Larz60+
  From list of bits to PDF drimades 1 2,018 Nov-02-2021, 08:55 PM
Last Post: Gribouillis
  A question about 'Event loop is closed' fc5igm 2 2,402 Oct-05-2021, 02:00 AM
Last Post: fc5igm
  ValueError: I/O operation on closed file problem aliwien 0 2,193 Apr-23-2021, 05:50 PM
Last Post: aliwien
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,870 Jan-11-2021, 07:42 AM
Last Post: kamui123
  Run an app in a standalone terminal and wait until it's closed glestwid 2 2,627 Aug-30-2020, 08:14 AM
Last Post: glestwid
  How to check if user entered string or integer or float?? prateek3 5 11,509 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  check if the number is a prime integer atlass218 5 3,079 Sep-26-2019, 07:58 AM
Last Post: atlass218

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020