Re:My Favorite Programming Problem to Teach: Digit Length
by gpderetta on Monday November 11, 2019 @11:50AM
(#21506329)
Oh, agree that for arbitrary large or non integer inputs it might not work. But elsethread jepler has shown a solution off Hacker's Delight which which simply multiplies the index of the most significant bit with a magic constant.
Re:My Favorite Programming Problem to Teach: Digit Length
by MauranKilom on Monday November 11, 2019 @02:32PM
(#21508050)
> But elsethread jepler has shown a solution off Hacker's Delight which which simply multiplies the index of the most significant bit with a magic constant.
...and then checks for and corrects the potential off-by-one thus incurred.
Re:My Favorite Programming Problem to Teach: Digit Length
by slavik81 on Monday November 11, 2019 @02:46PM
(#21508174)
When N is fixed, all algorithms become O(1). That's why they appear the same.
If you do these calculations by hand, the complexity will be more obvious because each operation will be smaller.
Also, counting digits in base 2 is not the log2. The former gives you the latter but not the reverse. Finding the number of decimal digits in a number given in base 2 is not a simplification.