This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

convert decimal to binary and contrary !
#2
Use this function, it returns a number that is stored in bits starring from offset to offset + length (not inclusive). offset is the lowest bit number that you want to get. length is number of bits to get.
Code:
function getbits(value, offset, length)
  local mask = bit.lshift(1, length) - 1
  return bit.band(bit.rshift(value, offset), mask)
end

value = 5000
result = getbits(value, 9, 3)
log(result)

Note that bits are counted from right to left starting from 0. So bits 9..11 are this: 0001 0011 1000 1000
Reply


Messages In This Thread
RE: convert decimal to binary and contrary ! - by admin - 23.03.2022, 09:50

Forum Jump: