[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: bitwise ops -- make them enums! (Re: what's wrong?)
- From: Mike Pall <mikelu-0509@...>
- Date: Sun, 4 Sep 2005 15:32:00 +0200
Hi,
Asko Kauppi wrote:
> n.b. normally the only place where enums are defined (within LuaX)
> are the C modules. This comes from the simple reason that any
> bitfields normally are used only in contact with C code. In Lua only
> world, there are other ways.
IMHO writing a C function for every bitfield I want to use
is not the way to go. Most of the bitfields I have are really
N bit integers and not enumerations. Of course bitfields are
a low-level approach, but they _are_ more generic. :-)
A simple example:
local function wputd(n)
if n < 0 then
n = n + 4294967296
end
local r = n % 256
n = (n - r) / 256
wputb(r)
r = n % 256
n = (n - r) / 256
wputb(r)
r = n % 256
n = (n - r) / 256
wputb(r, n)
end
Why oh why can't I simply write:
local function wputd(n)
wputb(bitfield(n, 0,8, 8,8, 16,8, 24,8))
end
I spare you the code for some of the more uglier examples
like splitting the ModRM and SIB bytes of an x86 opcode. :-)
And I really don't want to write things like this anymore:
local opcode = op == "movzx" and 4023 or 4031
But rather:
local opcode = op == "movzx" and 0x0fb7 or 0x0fbf
Just so this doesn't sound like a rant about a specific
application: dealing with some network protocols (like DNS
or TLS) is really, really ugly without hex numbers and
bitfields. Don't tell me I have to write this in C ...
Bye,
Mike