From 5dd2629e28291c58f41a70cf3de478cfc7ae3db8 Mon Sep 17 00:00:00 2001 From: "Eugeniy E. Mikhailov" Date: Wed, 2 Jun 2021 14:14:22 -0400 Subject: avoid using insert and instead use index for num to bit conversion --- binaryLib.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/binaryLib.lua b/binaryLib.lua index cf8a167..3d5c9c6 100644 --- a/binaryLib.lua +++ b/binaryLib.lua @@ -18,9 +18,11 @@ end -- continue to divide by two until quotient is zero function toBits(num) local t = {} + local cnt = 0 while num > 0 do + cnt = cnt + 1 remainder = num % 2 -- Basics of binary. If num is divisible by two, 0 in table. If not, 1 in table - table.insert(t, remainder)-- defaults to #t+1 key + t[cnt] = remainder -- defaults to #t+1 key num = (num - remainder)/2 --num = math.floor((num - remainder)/2) -- carry out calculation -- note math.floor used here to eliminate floating values -- cgit v1.2.3