Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua: print integer as a binary

Tags:

binary

lua

How can I represent integer as Binary?

so I can print 7 as 111

like image 755
fl00r Avatar asked Jan 31 '12 12:01

fl00r


People also ask

How do you print int to binary?

Using Integer#toString Method It returns a string representation of the integer input in the base specified by the radix. Let's use this method to convert an integer into its binary format using a radix value of 2: int n = 7; String binaryString = Integer. toString(n, 2); assertEquals("111", binaryString);

How do I convert an integer to binary in JavaScript?

To convert an integer to binary in JavaScript, divide the integer by 2 and store the remainder.

How do you write binary numbers in JavaScript?

Example 2: Convert Decimal to Binary Using toString() The parseInt() method is used to convert a string value to an integer. The JavaScript built-in method toString([radix]) returns a string value in a specified radix (base). Here, toString(2) converts the decimal number to binary number.


2 Answers

You write a function to do this.

num=7
function toBits(num)
    -- returns a table of bits, least significant first.
    local t={} -- will contain the bits
    while num>0 do
        rest=math.fmod(num,2)
        t[#t+1]=rest
        num=(num-rest)/2
    end
    return t
end
bits=toBits(num)
print(table.concat(bits))

In Lua 5.2 you've already have bitwise functions which can help you ( bit32 )


Here is the most-significant-first version, with optional leading 0 padding to a specified number of bits:

function toBits(num,bits)
    -- returns a table of bits, most significant first.
    bits = bits or math.max(1, select(2, math.frexp(num)))
    local t = {} -- will contain the bits        
    for b = bits, 1, -1 do
        t[b] = math.fmod(num, 2)
        num = math.floor((num - t[b]) / 2)
    end
    return t
end
like image 138
jpjacobs Avatar answered Sep 21 '22 14:09

jpjacobs


There's a faster way to do this that takes advantage of string.format, which converts numbers to base 8. It's trivial to then convert base 8 to binary.

--create lookup table for octal to binary
oct2bin = {
    ['0'] = '000',
    ['1'] = '001',
    ['2'] = '010',
    ['3'] = '011',
    ['4'] = '100',
    ['5'] = '101',
    ['6'] = '110',
    ['7'] = '111'
}
function getOct2bin(a) return oct2bin[a] end
function convertBin(n)
    local s = string.format('%o', n)
    s = s:gsub('.', getOct2bin)
    return s
end

If you want to keep them all the same size, then do

s = string.format('%.22o', n)

Which gets you 66 bits. That's two extra bits at the end, since octal works in groups of 3 bits, and 64 isn't divisible by 3. If you want 33 bits, change it to 11.

If you have the BitOp library, which is available by default in LuaJIT, then you can do this:

function convertBin(n)
    local t = {}
    for i = 1, 32 do
        n = bit.rol(n, 1)
        table.insert(t, bit.band(n, 1))
    end
    return table.concat(t)
end

But note this only does the first 32 bits! If your number is larger than 2^32, the result wont' be correct.

like image 29
Houshalter Avatar answered Sep 22 '22 14:09

Houshalter