Binary digits: Difference between revisions
Content added Content deleted
(→{{header|Lua}}: removed incorrect second example) |
|||
Line 1,425: | Line 1,425: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
=== For version 5.1 (used in LuaJIT) === |
|||
<lang Lua>function dec2bin (n) |
<lang Lua>function dec2bin (n) |
||
local bin = "" |
local bin = "" |
||
Line 1,442: | Line 1,441: | ||
110010 |
110010 |
||
10001100101000</pre> |
10001100101000</pre> |
||
=== For version 5.2+ === |
|||
{{incorrect|Lua|The task specifically requires leading zeros to be omitted.}} |
|||
<lang Lua> |
|||
function dec2bin(n) |
|||
local bin = "" |
|||
for i = 31, 0, -1 do |
|||
bin = bin .. bit32.extract(n, i) |
|||
end |
|||
return bin |
|||
end |
|||
print(dec2bin(5)) |
|||
print(dec2bin(50)) |
|||
print(dec2bin(9000)) |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
00000000000000000000000000000101 |
|||
00000000000000000000000000110010 |
|||
00000000000000000010001100101000 |
|||
</pre> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |