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?

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 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???

When you do things right, people won't be sure you've done anything at all.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014

10

posted

0

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.

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.

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.

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

Joined: Mar 05, 2008
Posts: 3014

10

posted

0

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.

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) ;)

Computers are like air conditioners - They stop working properly when you open Windows

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014

10

posted

0

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.

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.

Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)

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:

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.