floaty fiddling
2008-05-06 20:24![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
So I needed some Lua functions to extract bit fields from an integral value, specifically struct stat.st_mode. Lua only has floating point numbers, and its standard library doesn't extend beyond ANSI C. So I wrote the following. (Note that a % b == a - floor(a/b)*b.)
function mask(lobit, hibit, num) local toolo = num % 2^lobit return (num - toolo) % 2^hibit end function bit(bit, num) return num / 2^bit % 2 >= 1 end
no subject
Date: 2008-05-07 09:19 (UTC)(Though it is an easy optimisation if it's a compile time known value. Or loop-invariant.)