This site makes extensive use of JavaScript.
Please enable JavaScript in your browser.
Live
PTR
10.2.7
PTR
10.2.6
Beta
Relationship between an Integer and the length of its Binary
Post Reply
Return to board index
Post by
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;
Post by
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)
Post by
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
Post by
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.
Post by
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.
Post by
204878
This post was from a user who has deleted their account.
Post by
Squishalot
That's what I understand it to be, but the OP hasn't come back in a while.
Post by
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
Post by
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.
Post by
204878
This post was from a user who has deleted their account.
Post by
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
Post by
204878
This post was from a user who has deleted their account.
Post by
Squishalot
I think he realises that, but it doesn't work with C++, or whatever language he's using in his class.
Post by
204878
This post was from a user who has deleted their account.
Post by
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.
Post by
seebs
Yeah, log2(n)+2 is definitely enough, assuming positive n.
Post Reply
You are not logged in. Please
log in
to post a reply or
register
if you don't already have an account.