1

Possible Duplicate:
How do I determine the size of an object in Python?

In C if I do

USHORT x;
ULONG y;
ULONGLONG yy;

printf("%d %d %d\n", sizeof(x), sizeof(y), sizeof(yy))

Output: 2 4 8

How can I get this in Python if I were to to

valByte = 0x16
valWord = 0x1234
valDWord = 0x12345678
val64Word = 0x1234567887654321

Output I expect should be 1, 2, 4, 8

thanks

Community
  • 1
  • 1
Santhosh Kumar
  • 381
  • 1
  • 5
  • 13
  • 1
    Python doesn't really work like that - what exactly are you trying to do? – Carl Norum Dec 28 '12 at 23:35
  • 1
    Python integers do not *have* a size. What are you trying to achieve? – Martijn Pieters Dec 28 '12 at 23:40
  • @MartijnPieters sure they do, it just varies. You can use [`sys.getsizeof`](http://docs.python.org/2/library/sys.html#sys.getsizeof), though the value it gives should probably not be _used_ for anything. – kenm Dec 28 '12 at 23:42
  • @kwatford: have you actually tried to use that function on integers? – Martijn Pieters Dec 28 '12 at 23:44
  • @MartijnPieters yes, it returns different values depending on the number of bytes needed to store the integer. – kenm Dec 28 '12 at 23:44
  • 3
    @kwatford: It returns the number of bytes python currently needs for the instance, yes, which happens to have a (small amount) of a correlation to how large the python integer is. But I doubt it is any use to the OP. – Martijn Pieters Dec 28 '12 at 23:45
  • Like I said, probably shouldn't be used for anything. If he just wants it for informational purposes ("I wonder how much memory this datastructure takes up") it can be used. Not so much for most of the things you'd use the size for in C. OP has not yet said what he actually needs, though. – kenm Dec 28 '12 at 23:47
  • For informational purposes, I made a plot of this: http://stackoverflow.com/a/30008338/2087463, it was interesting to see... – tmthydvnprt Mar 13 '16 at 13:26

2 Answers2

5

In Python3 there is the bit_length method:

>>> import math

>>> math.ceil(0x16.bit_length()/8)
1
>>> math.ceil(0x1234.bit_length()/8)
2
>>> math.ceil(0x12345678.bit_length()/8)
4
>>> math.ceil(0x1234567887654321.bit_length()/8)
8

In Python2.7, long integers also have the bit_length method:

>>> (10**20).bit_length()
67

>>> x = 123
>>> long(x).bit_length()
7
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • This is fantastic and answers my question. I am from the C world and am porting a C program to Python. These values I mention represent register width and they come in all sizes. I need to know the size of the register, thank you very much unutbu – Santhosh Kumar Dec 29 '12 at 01:18
  • However one issue please ... I have to use the variable name, i cannot use the actual number right? In my case the variable name is always reg. reg can hold any one of the values, byte, word ... Just for an example I gave different names. reg.bit_length does not work. So how do I do do the same work with a variable name? – Santhosh Kumar Dec 29 '12 at 01:26
  • @SanthoshKumar: `reg.bit_length()` will work. Are you sure you need to worry about this in Python? I suspect you are doing too-literal a translation from C to Python, and there is an easier way to accomplish your goal. What will you do with these "integer sizes"? – Ned Batchelder Dec 29 '12 at 01:33
  • In C this is the program: – Santhosh Kumar Dec 29 '12 at 01:34
  • Sorry ignore the above comment. My idea is to check if each bit is set from Right to left (LSB to MSB) and if the bit set then to print out the meaning of that bit. Like the error registers or status registers. for i in range(reg): shift = reg & (1 << i) the problem here is val will be the actual value of the number where as I want it to be number of bits – Santhosh Kumar Dec 29 '12 at 01:38
  • BTW I did try reg.bit_length() and it gave me an error: AttributeError: 'int' object has no attribute 'bit_length' – Santhosh Kumar Dec 29 '12 at 01:45
  • for now I have hard coded the highest bit value possible (64) and it works but I wish to remove the hard coding regVal = 0x16 for i in range(64): bitVal = regVal & (1 << i) if (bitVal): print '1' #"The meaning of that bit being set to 1" else: print '0' #"The meaning of that bit being set to 0" print Done – Santhosh Kumar Dec 29 '12 at 02:01
  • @SanthoshKumar: (1): `reg.bit_length()` only works in Python3, not in Python2. The `AttributeError` you received strongly suggests you are using Python2. However, in Python2, `long` integers do have the `bit_length` method, so you could do `long(reg).bit_length()`. (2): Python `long` integers are unbounded. They can certainly be larger than 64 bits. For example, `(10**20).bit_length()` is 67. (3): Your code suggests you want the binary string representation for `regVal`. That could be done with `'{0:b}'.format(regVal)`. (4): Pay attention to Ned Batchelder's comment. He is invariably right. :) – unutbu Dec 29 '12 at 02:20
  • Correction: In Python2.7, `long`s have the `bit_length` method. In earlier versions of Python, use the `bit_length` function shown in [the docs](http://docs.python.org/2/library/stdtypes.html#long.bit_length). – unutbu Dec 29 '12 at 02:22
  • YES!! The docs helped me, I am done. Sorry I am stuck with 2.6, do not have the freedom to go to higher versions of Python. Thanks to all this case can be closed. – Santhosh Kumar Dec 29 '12 at 02:52
0

You can use sys.getsizeof, but the result may be annoying because there is a minimum that a integer can be.

For example, in my 64 bits PC all you variables have a size of 24 bytes.

JoseLSegura
  • 3,830
  • 3
  • 20
  • 27