-
[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.
-
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.
-
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/
-
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.
-
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.
-
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.
-
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?
-
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.
-
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.
-
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'.
-
Re: Size of something in bytes?
Code:
static int CountBytes( int n ) {
int count = 0;
while ( n > 0 ) {
++count;
n >>= 8;
}
return count;
}
-
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.
-
Re: Size of something in bytes?
Quote:
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.
-
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.
-
Re: Size of something in bytes?
Quote:
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.
Quote:
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.
-
Re: Size of something in bytes?
Quote:
Originally Posted by
TheGreatCthulhu
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.
if you read the original question:
Quote:
Originally Posted by
elitelogic
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.
I interpret that to mean he is after how many bytes does the number occupy, which is what I provided.
-
Re: Size of something in bytes?
I think the OP is just not worded in a way that would be clear to everyone. It seems that what he is looking for is the number of digits in a numeric variable and unfortunately is referring to them as bytes causing confusion.
As I mentioned, convert to string and check the length of that string and he will get the result that he is apparently looking for here.
-
Re: Size of something in bytes?
Quote:
Originally Posted by
MadHatter
I interpret that to mean [...]
Quote:
Originally Posted by
DataMiser
I think the OP is just not worded in a way that would be clear to everyone.
Precisely - that's the the problem here: the question is not well formulated, most likely because, at the time it was asked, the OP didn't have a precise understanding of the relationship between the types and their values.
So, people came up with different interpretations and offered different solutions.
You also have to take note that the OP didn't say in what way exactly he would use this, so we can't draw any conclusions from that.
For example, MadHatter's method could be perfectly valid if the OP needed to do some sort of quantization.
Anyway, my point is, only the OP can tell us what is it that he's actually after here, provided that he himself has a good understanding of the topic.
-
Re: Size of something in bytes?
Quote:
Originally Posted by
elitelogic
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.
I don't see any ambiguity here.
He wants to know how many bytes an instance of IntX uses for a 500 digit number.
-
Re: Size of something in bytes?
Though I know i'm beating a dead dog here, I think i'll still respond one more time ;)
Quote:
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.
That is very ambiguous and as written makes absolutely no sense. There are three ways this could be interpreted. I left rough estimates as to the memory costs at the end of each line.
1) If i call "ToString()" on the number, i will end up with a string of 500 characters which will be 500 x sizeof (char) bytes in size. (1000 bytes)
2) This could also be validly represented by 500 x sizeof (byte) which is smaller again by 50%. (500 bytes)
3) It could also be represented by a handful of bytes. At a completely random guess, 10 bytes may be enough if you used standard binary notation. Someone else can do the math and work it out exactly. It'll be a lot less than 500 though. (about 10 bytes)
So that's three ways to represent your number, each of which have different memory requirements. That's why this question is so ambiguous. Your question implies that you want either answer 1 or 2 but it's impossible to tell so rather than argue uselessly about it I'll leave it at this ;)
-
Re: Size of something in bytes?
Quote:
Originally Posted by
Zaccheus
I don't see any ambiguity here.
He wants to know how many bytes an instance of IntX uses for a 500 digit number.
And, in addition to what Mutant_Fruit said, if you assume that IntX dynamically adapts memory-wise, you can also interpret the question as: "What is the amount of memory used by an IntX instance to represent a certain number?".
But, (1) this is totally dependent on the actual implementation, and (2): what makes you think that the answer to this question would always be the same, even when calling the hypothetical "GetSize()" method once, and then calling it again only few lines later?
Consider this: two IntX objects could contain the same value, but if the "usage-history" had been different, the memory requirements may have been different also, so the two instances would use different amount of bytes to represent the same number. (Again - depending on the implementation.)
Furthermore, if you interpret this as the minimum amount of bytes required to represent an n-digit long number, then you can get the answer mathematically - you don't need to bother with the IntX type or it's instances and values at all.
Quote:
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.
Again, I think that the implementation details are open-source, but otherwise you're right - this should be the responsibility of the class, in which case the documentation would define what exactly the number returned by my hypothetical "GetSize()" method means.
-
Re: Size of something in bytes?
Quote:
Originally Posted by
TheGreatCthulhu
what makes you think that the answer to this question would always be the same, even when calling the hypothetical "GetSize()" method once, and then calling it again only few lines later?
Quote:
Originally Posted by
TheGreatCthulhu
this should be the responsibility of the class, in which case the documentation would define what exactly the number returned by my hypothetical "GetSize()" method means.
I completely agree - there would need to be a method which reports how much memory (in bytes) a specific instance of the class is actually using at any moment in time.
-
Re: Size of something in bytes?
"the number of bytes an X digit number is" sounds like the way oracle stores numbers. its' a feasible question, but due to the way most of us think of number systems, its quite different.
-
Re: Size of something in bytes?
My apologies for the slow reply but I wanted to close the loop on this in case others in the future have a similar question. Yes, my question was worded in a manner that instantly muddied the water and caused a lot of confusion.
Zaccheus had it right when he said:
Quote:
He wants to know how many bytes an instance of IntX uses for a 500 digit number.
The answer is that the IntX class has a method called "GetInternalState()" which provides the answer I was looking for.
-
Re: Size of something in bytes?
Tada!
(@OP: You can mark the thread as resolved by clicking the "Thread Tools" menu at the top and selecting "Mark Thread Resolved")