Talk:Two identical strings: Difference between revisions

(→‎FreeBASIC black magic: response to Chunes)
 
Line 79:
 
:Chunes: Nigel Galloway's explanation is correct. (Thanks, Nigel.) To help me understand it, I've added an alternate FreeBASIC version that works without utilizing <code>log()</code>. The added variable <code>p</code> (which represents <code>2^int(log(n)/log(2)</code>) 'ratchets' up the left shift amount (the <code>int(log(n)/log(2))</code> in <code>2^int(log(n)/log(2))</code>) by double, so the <code>2*n</code> in <code>k=2*n*2^int(log(n)/log(2))</code> can just be <code>n</code>, resulting in <code>k = n*p</code>.--[[User:Enter your username|Enter your username]] ([[User talk:Enter your username|talk]]) 18:33, 4 April 2021 (UTC)
 
:These explanations are correct. To see how it works, suppose I want to duplicate 1001 (9 in decimal). First I multiply by two enough times that I end up with 10010000, then add the 1001 again. So I need to figure out the number of spare digits I need, which is (1+floor(log_2(9)). So I take my original number n, multiply by 2^(1+floor(log_2(n)), and add n. [[User:Thebigh|Thebigh]] ([[User talk:Thebigh|talk]]) 16:03, 5 April 2021 (UTC)
781

edits