Jump to content

Talk:Two identical strings: Difference between revisions

→‎FreeBASIC black magic: response to Chunes
(→‎FreeBASIC black magic: response to Chunes)
Line 77:
The [http://rosettacode.org/wiki/Two_identical_strings#FreeBASIC FreeBASIC solution] to this task is super neat! Any chance for an explanation as to how/why it works? --[[User:Chunes|Chunes]] ([[User talk:Chunes|talk]]) 12:48, 3 April 2021 (UTC)
:log n/log 2 is the number of bits - 1 representing n in binary. The formula produces n shifted left by this number + 1, then adds n.--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 15:24, 3 April 2021 (UTC)
 
: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)
Cookies help us deliver our services. By using our services, you agree to our use of cookies.