Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# math question - (divided by 2?)

Rodrigo Bossini
Ranch Hand
Posts: 113
Is there a smart manner to determine how many times a given integer n number can be divided by two?

For example, 2 can be divided one time by 2.
Any odd number can't be divided by 2.
A number that is a power of 2 can be divided by two exactly log n ( 8 can be divided lg 8 = 3 times by two because 8 = 2^3).

But for the even numbers that are not a power of two, how do I say how many times they can be divided by two?

Thanks.

Mike Simmons
Ranch Hand
Posts: 3076
14
If you look at the number in binary, then this is determined by the number of zeroes at the end of a number. A number that ends in 1 is not divisible by 2, and for numbers that are divisible by 2, each division by 2 removes one of the trailing zeroes.

That's useful if a number is already in binary. Which is generally the case when you're programming. But if you, a human, are looking at a decimal number, then in order to convert to binary, you have to perform a series of divisions. So this trick isn't really any faster for answering your question. If you want to know how many times a number can be easily divided by two, the fastest way is probably to keep dividing by two until you get an odd number.

In Java, you can just use Integer.numberOfTrailingZeros().

marc weber
Sheriff
Posts: 11343
Determine how many times 2 goes into the final digit. (For example, if your number is 416, then 2 goes into 6 three times.)

Remove that last digit from the number and multiply by 5. (So 416 becomes 41, and 41 * 5 is 205.)

Add the results. (3 + 205 = 208.)

Mike Simmons
Ranch Hand
Posts: 3076
14
Yeah - that's much easier than simply dividing by two.

Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
marc weber wrote:Determine how many times 2 goes into the final digit. (For example, if your number is 416, then 2 goes into 6 three times.)

Remove that last digit from the number and multiply by 5. (So 416 becomes 41, and 41 * 5 is 205.)

Add the results. (3 + 205 = 208.)

This looks like some sort of mathematical voo-doo. Where do you get the 5 from?

I see it works, but WHY???

Mike Simmons
Ranch Hand
Posts: 3076
14
Removing the last digit of a number is like dividing by ten and losing any remainder. Removing the last digit and then multiplying by five is like dividing by ten and then multiplying by five, which is like multiplying by 5/10 or 1/2. Which is like dividing by two.

What about the lost remainder? Well, that was handled back at the beginning when we looked at the last digit and divided by two, then added the result back on at the end.

So for example:

123456 / 2 = (123450 + 6) / 2 = 123450 / 2 + 6 / 2 = (1234 * 5) + 6 / 2

Here you see everything but the last digit, times 5, plus the last digit, divided by two.

Janeice DelVecchio
Saloon Keeper
Posts: 1809
12

Holy crap.

Pat Farrell
Rancher
Posts: 4678
7
marc weber wrote:Determine how many times 2 goes into the final digit. (For example, if your number is 416, then 2 goes into 6 three times.)
Remove that last digit from the number and multiply by 5.

Cute. But its a lot more work than just doing the division. So while its clever, I'm not sure its "smart" by any sensible criteria.

Even the "remove the last digit" is hard to do on a computer without doing the division by 10, which binary computers do not do well.

If your computer does not have an integer divide instruction, you could do a series of mask/test the lowest bit, and shift right, which is how the division is done in software on those machines.

Mike Simmons
Ranch Hand
Posts: 3076
14
I figured it was meant as a joke.

marc weber
Sheriff
Posts: 11343
I just took the original post to be asking for methods other than the straight-forward approach of dividing by 2. The example given (deriving the base 2 log of n, for cases in which n is a power of 2) doesn't seem any easier than simply dividing by 2.

Teja Saab
Rancher
Posts: 152
Rod Bossini wrote:Is there a smart manner to determine how many times a given integer n number can be divided by two?

The answer is "infinite". Nowhere is it mentioned that the result of the division shouldn't be a fraction.

Mike Simmons
Ranch Hand
Posts: 3076
14
Oh, please. The original post was pretty clear on this with its examples.

Teja Saab
Rancher
Posts: 152
Mike Simmons wrote:Oh, please. The original post was pretty clear on this with its examples.

Frank Silbermann
Ranch Hand
Posts: 1407
Suppose you have a list of the powers of two:

2, 4, 8, 16, 32, 64, ....

So think of the problem as a binary search to determine the largest element of the list that is a factor of your number.

Initial endpoints are 2 and the largest element of the list that is smaller than your number.

Pick the power of 2 that is in the middle of your interval, and see whether it divides your number.
If no, then your new interval is your previous low value, and the power of two that is immediately to the left as your high value.
If yes, then the low end of your new interval is the number you just picked, and you keep the previous high end of your interval.
Repeat until your interval contains just one number.

This algorithm may be useful if the initial interval size (possible powers of 2) is very large.

Mike Simmons
Ranch Hand
Posts: 3076
14
Frank: nice, that actually can be faster than the standard solution, depending on what we know about the range and distribution of input values. Good thinking.

Teja:

For example, 2 can be divided one time by 2.
Any odd number can't be divided by 2.

It's obvious that that Rod is talking about being evenly divisible. To say "Nowhere is it mentioned that the result of the division shouldn't be a fraction" is to completely ignore what Rod actually did say, which contradicts the possibility of fractional results. You might think it should have been phrased differently, but it's disingenuous to simply ignore it.

Evgeniy Bulanov
Greenhorn
Posts: 23
dividing by 2 is the left shift binary operation
For ex.:
8 = 1000 in the binary form
so, to divide by 2 we should shift it to left one time.
1000(8) >> 1 = 100(4)
100(4) >> 1 = 10(2)
10(2) >> 1 = 1(1)

so, we shift it tree times = (last_value_position_bit - 1) ;)

Mike Simmons
Ranch Hand
Posts: 3076
14
Wow - it's almost as though if you look at the number in binary, the question is answered by the number of zeroes at the end of the number. How interesting.

Naren Chivukula
Ranch Hand
Posts: 577
Here is a simple program which does the job. Hope you are looking something like this, if not let us know.
Note: You can change the code so that variable n value is provided at the runtime.

Wouter Oet
Saloon Keeper
Posts: 2700
Thank you for sharing but Mike gave the answer already. Not in a concrete way but with a great hint.
The best solution would probably be: