此站点大量使用JavaScript。
请在您的浏览器中启用JavaScript。
正式服
PTR
10.2.7
PTR
10.2.6
Beta
Relationship between an Integer and the length of its Binary
发表回复
返回主列表
发布者
jGarand
Does anyone know the relationship between an Integer and the length of its Binary equivalent (as a string)?
I am working on something where I want to use a dynamically allocated array to hold the binary value of an integer.
I don't want to cheat and use array (75 can contain the binary equivalent of 2147483647).
I have other options for how to code it, but this would be the easiest
int Array_length = Integer * SOMETHING; //this is where i need help
mystring = new int;
发布者
EpicDragon
I'm thinking you could do it using modulus in some way. Gonna think about it for a while and edit if I figure it out.
This might do it.
int num2; // the number you want
int && num < array) {
num2 = i + 1;
}
}
Not 100% though... (didn't test it, mistakes possible)
发布者
jGarand
thanks pal; thats what I'm using for the actual conversion. I'm just stuck with an easy way to allocate array length.
My other option is to take a complete run through the integer and count the number of steps it takes, then allocate the array length to be the # of steps, make Another run and actually store % remainders from the last position forward.
but if I am wasting that much processing time/ coding time, i might as well just slap a 75 in there and call it good
发布者
Squishalot
Can't you just do something like:
ROUNDUP(LOG(<X>+1, 2),0)
where <X> is the integer number you're using?
Edit: LOG(X,2) is the log of X to base 2.
Fixed.
发布者
Squishalot
I'm thinking you could do it using modulus in some way. Gonna think about it for a while and edit if I figure it out.
This might do it.
int num2; // the number you want
int && num < array) {
num2 = i + 1;
}
}
Not 100% though... (didn't test it, mistakes possible)
That's still fairly computationally intensive, because you'll be running it through the IF statement up to 75 times, depending on how big the numbers are.
发布者
204878
This post was from a user who has deleted their account.
发布者
Squishalot
That's what I understand it to be, but the OP hasn't come back in a while.
发布者
jGarand
Yes, basically what I am trying to do is predict the number of bits it will take to represent an integer.
based on some sort of generalized formula
Sorry, watching tosh :P phone wasnt refreshing properly
edited for clarity
发布者
Squishalot
Best single-line approach is to round up a base-2 log function then.
If your programming language doesn't support log base-2, then you might need to use an iterative one similar to what EpicDragon suggested instead.
发布者
204878
This post was from a user who has deleted their account.
发布者
jGarand
Integer.toBinaryString(in) oh java, I wish this class used you.
Thanks for the good ideas guys, as soon as my internet comes back up and i can actually access my schools drives to debug, ill work with those ideas and see what ends up working. If nothing, 75 it is!
thought as I'm typing this:
75 wouldn't be so bad if i did an array of bool statuses to limit the memory allocation
发布者
204878
This post was from a user who has deleted their account.
发布者
Squishalot
I think he realises that, but it doesn't work with C++, or whatever language he's using in his class.
发布者
204878
This post was from a user who has deleted their account.
发布者
Squishalot
Yeah, it threw me a bit too :P
It makes sense to try to do it from first principles though. If all school classes just taught "look up this in-built function", we (as in, society) would eventually lose the ability to write the functions in new languages.
发布者
seebs
Yeah, log2(n)+2 is definitely enough, assuming positive n.
发表回复
您没有登录。请
登录
发表回复或
注册
如果您还没有账号。