Logo

Programming-Idioms

History of Idiom 76 > diff from v45 to v46

Edit summary for version 46 by programming-idioms.org:
[Lua] No need for sample values

Version 45

2017-08-21, 13:21:07

Version 46

2017-08-21, 19:42:42

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Extra Keywords
int radix
Extra Keywords
int radix
Code
local x = 13
local s = {}

while x > 0 do
    local tmp = math.fmod(x,2)
    s[#s+1] = tmp
    x=(x-tmp)/2
end

s=table.concat(s)
Code
local s = {}

while x > 0 do
    local tmp = math.fmod(x,2)
    s[#s+1] = tmp
    x=(x-tmp)/2
end

s=table.concat(s)