|
-
September 17th, 2010, 03:29 PM
#1
[RESOLVED] Size of something in bytes?
I've searched and maybe I'm not using the right keywords. This seems like a simple question.
I'm working with very large integers and I'm using the Codeplex class IntX. When I get a very large integer, I want to be able to determine the size in bytes.
I can't seem to find anything that works...
The sizeof operator will give me the size of the type, but that's not what I'm after. I need the size of the data in the variable.
-
September 17th, 2010, 03:41 PM
#2
Re: Size of something in bytes?
You seem to be confused.
If you have a 32 bit integer, regardless of whether it's value is 1 or 1,000,000, the size is 4 bytes. The fact that it is representing a value that perhaps does not require 4 bytes is irrelevant. The data type requires 4 bytes.
-
September 17th, 2010, 03:46 PM
#3
Re: Size of something in bytes?
No, this is beyond 32 or 64. The integer size is limited by the memory of the computer.
http://intx.codeplex.com/
-
September 17th, 2010, 04:17 PM
#4
Re: Size of something in bytes?
...it doesn't matter. You are missing the point. sizeof(type) *is* the size. You should study up on how memory and data types work. Just because the value is 1 doesn't mean that it doesn't require 128 (or whatever sizeof(type) * 8 is) bits in memory.
-
September 17th, 2010, 04:22 PM
#5
Re: Size of something in bytes?
I know.
In my original post, I said the sizeof operator will give me the sizeof the type. I know this.
I'm not after the size of the int, or the double, or the long.
I'm after the size of the variable that is 500 digits long.
How long is that in bytes? I hope I'm being clear. My apologies for any confusion.
-
September 17th, 2010, 04:36 PM
#6
Re: Size of something in bytes?
...
Ok, I hear you, but I still think you are missing the point. It would help if you told us your use case for this. Anyhow, an easy way would be to just count bits from most significant down until one is set. Of course, you run into endian issues this way. There are other ways, but I am curious as to why your would ever need such a thing. The value doesn't make *any* difference; the data takes however many bits the class required.
Last edited by BigEd781; September 17th, 2010 at 04:42 PM.
-
September 18th, 2010, 01:22 AM
#7
Re: Size of something in bytes?
From what I've seen in the description of the class at CodePlex (a brief look really, I didn't go into details) - everything that BigEd781 said still holds, no matter this is not your standard integer.
Take a byte for example. When you count all the ways in which you can possibly arrange the 8 bits it has, you can see that a byte can show 256 different values (0 to 255). Now, whether this value is 0, 1, 2... or 255 it doesn't matter at all - a byte is still a byte - all the memory of the datatype is used. Because the bit-representation of a number like 1 is 00000001.
Same goes for integer types, except that with them you have more than one byte - because more than 256 numbers need to be represented.
The IntX class should be designed to enable you to forget about the datatype limitations, minimum and maximum values. I'm guessing that, internally, it starts with a certain size, and after some calculations are done, if there's a risk of an overflow, the internal storage is increased.
What happens after this depends on the actual implementation - the memory used may never go down, or it may accommodate to optimally suit the new value.
The thing is, the number of digits is NOT correlated with the storage size the way you think it is. You can have numbers differing by several orders of magnitude using the same amount of memory.
But, I must repear BigEd781's question:
Why your would ever need such a thing?
Last edited by TheGreatCthulhu; September 18th, 2010 at 01:24 AM.
-
September 18th, 2010, 06:20 AM
#8
Re: Size of something in bytes?
Thanks, I really appreciate the help.
Yes, I understand...let's say type IntX is 32 bits. Those 32 bits can hold a range of values.
So, maybe IntX the type never grows, but rather the memory the type "points" to. So, if my 32 bit IntX points to a number 3 million digits long, I don't care about the size of the 32 bit IntX, but rather I care about the 3 million digit long number..how many bytes is that?
Would a work around be to put this in a binary buffer and get the size of that?
My use case is mathematical processing. Pretty boring stuff.
-
September 18th, 2010, 07:33 AM
#9
Re: Size of something in bytes?
You could convert to a string and check the string length to get the number of characters.
btw as everyone has said the number of bytes is not chaning in a numeric data type. The number of characters required to display this value is however which seems like what you are after.
Last edited by DataMiser; September 18th, 2010 at 07:35 AM.
Always use [code][/code] tags when posting code.
-
September 18th, 2010, 10:18 AM
#10
Re: Size of something in bytes?
int number = 66;
int length = 66.ToString ().Length;
Is that what you want? In this case it would return '2'. If you had the number '12345', you'd get '5'.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
September 23rd, 2010, 03:31 PM
#11
Re: Size of something in bytes?
Code:
static int CountBytes( int n ) {
int count = 0;
while ( n > 0 ) {
++count;
n >>= 8;
}
return count;
}
-
September 25th, 2010, 07:03 AM
#12
Re: Size of something in bytes?
It sounds like this is something the class itself would need to provide, because there really is no way for you to know how the type is implemented.
-
September 25th, 2010, 08:23 AM
#13
Re: Size of something in bytes?
 Originally Posted by MadHatter
Code:
static int CountBytes( int n ) {
int count = 0;
while ( n > 0 ) {
++count;
n >>= 8;
}
return count;
}
All that'd tell you is the minimum number of bytes that are needed to represent a particular number, but sizeof (int) is still 4 no matter what number it represents The size of "n.ToString ()" is different again.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
September 25th, 2010, 10:47 AM
#14
Re: Size of something in bytes?
wasn't meant as THE answer, but as a means to an end. the way you count the number of bits needed to represent the numeric value is to shift it & count (there are other more efficient ways, but this is the most basic way).
obviously the data type isn't int it was intX, but they can adapt to suit their own needs. it solves the OP's original need.
-
September 25th, 2010, 12:13 PM
#15
Re: Size of something in bytes?
 Originally Posted by Zaccheus
It sounds like this is something the class itself would need to provide, because there really is no way for you to know how the type is implemented.
I think that the implementation details are open source, but otherwise you're right - this should be the responsibility of the class.
 Originally Posted by MadHatter
wasn't meant as THE answer, but as a means to an end. the way you count the number of bits needed to represent the numeric value is to shift it & count (there are other more efficient ways, but this is the most basic way).
obviously the data type isn't int it was intX, but they can adapt to suit their own needs. it solves the OP's original need.
But, as Mutant_Fruit said, it is only the minimum amount of the required memory - there's no guarantee that the IntX type isn't using more. For example, the internal storage could initially be set to an amount that would be reasonable for most of the time (that could be way off from what your method would show for a particular value), and would only be incremented when required.
But, again I haven't really read anything about the type, or downloaded any of the code at it's CodePlex site.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|