2012-01-31 50 views
9

如何將整數表示爲二進制?Lua:將整數作爲二進制打印

所以7111

+0

看看[這裏](http://lua-users.org/lists/lua-l/2002-10/msg00245.html)(我猜沒有內置函數?) – 2012-01-31 13:04:42

+0

它已經過時了一點:) – fl00r 2012-01-31 13:14:27

回答

5

你寫一個函數來做到這一點,我可以打印。

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)) 

在Lua中5.2你已經有按位的功能,它可以幫助你(bit32


這裏是最顯著,第一個版本,帶有可選的前導0填充到指定號碼

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 
+1

你已經在你的函數中得到了反轉位,所以'20'將返回'00101',而不是'10100' – fl00r 2012-01-31 13:56:20

+1

你沒有說明你是想要大的還是小的endian。這個例子也沒有讓它消失,因爲111是一個迴文;)。無論如何,適應它很容易:只需使用'nBits = ceiling(select(2,math.frexp(num))''並使用for循環從nBits開始到1. – jpjacobs 2012-01-31 13:59:49

+0

我的錯,對不起,但答案是正確且有用,謝謝! – fl00r 2012-01-31 14:00:42

0
function reverse(t) 
    local nt = {} -- new table 
    local size = #t + 1 
    for k,v in ipairs(t) do 
    nt[size - k] = v 
    end 
    return nt 
end 

function tobits(num) 
    local t={} 
    while num>0 do 
     rest=num%2 
     t[#t+1]=rest 
     num=(num-rest)/2 
    end 
    t = reverse(t) 
    return table.concat(t) 
end 
print(tobits(7)) 
# 111 
print(tobits(33)) 
# 100001 
print(tobits(20)) 
# 10100 
+2

另一個wa y是'string.reverse(table.concat(t))' – user3125367 2017-03-15 18:35:35

2
function bits(num) 
    local t={} 
    while num>0 do 
     rest=num%2 
     table.insert(t,1,rest) 
     num=(num-rest)/2 
    end return table.concat(t) 
end 
:位

既然沒有人想要使用table.insert,但在這裏很有用

+0

實際上,使用table.insert會將算法**的複雜度從O(n)**增加到** O(n^2)**。做** jpjacobs **在他的評論中說,首先確定數字的長度,然後向後填充數組,效率更高。特別是對於大數量。 (2,math.frexp(num))),1,-1 do'和't [#t + 1)替換'while num> 0 do' ]''by't [i]'。 – RPFeltz 2014-08-23 12:22:57

2

這是一個靈感來自於接受的答案的函數,它具有正確的語法,它從右向左返回wriiten中的位表。

num=255 
bits=8 
function toBits(num, bits) 
    -- returns a table of bits 
    local t={} -- will contain the bits 
    for b=bits,1,-1 do 
     rest=math.fmod(num,2) 
     t[b]=rest 
     num=(num-rest)/2 
    end 
    if num==0 then return t else return {'Not enough bits to represent this number'}end 
end 
bits=toBits(num, bits) 
print(table.concat(bits)) 

>>11111111 
2

有做到這一點更快的方式,需要的String.Format,其將數字轉換基地8是微不足道的,然後基地8轉換爲二進制的優勢。

--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 

如果你想保持他們一樣的大小,然後做

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

它可以幫助您66位。最後這是兩個額外的位,因爲八進制工作在3位組中,64位不能被3整除。如果要33位,請將其更改爲11.

如果您有BitOp庫,它是默認在LuaJIT中可用,那麼你可以這樣做:

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 

但是請注意,這隻能做前32位!如果你的號碼大於2^32,結果不會是正確的。