25 lines
829 B
Text
25 lines
829 B
Text
with javascript_semantics
|
|
include mpfr.e
|
|
function mpz_srb(string input, integer n)
|
|
mpz res = mpz_init("0b"&input),
|
|
tmp = mpz_init_set(res),
|
|
bit = mpz_init(1),
|
|
mask = mpz_init(power(2,n+1)-1),
|
|
rask = mpz_init() -- (mask res)
|
|
for i=1 to length(input) do -- (backward, actually)
|
|
if mpz_even(tmp) then
|
|
mpz_and(rask,tmp,mask)
|
|
if mpz_cmp_si(rask,0)!=0 then
|
|
mpz_add(res,res,bit)
|
|
end if
|
|
end if
|
|
mpz_mul_2exp(bit, bit, 1) -- aka left shift
|
|
mpz_fdiv_q_2exp(tmp, tmp, 1) -- aka right shift
|
|
end for
|
|
string ret = mpz_get_str(res,2)
|
|
integer lz = length(input)-length(ret)
|
|
if lz then ret = repeat('0',lz)&ret end if
|
|
return ret
|
|
end function
|
|
--...
|
|
printf(1,"n = %d: %s\n",{n,mpz_srb(input,n)})
|