News for hackers. Stuff that matters.
Re:A Better Way to Implement Bit Fields by rusk on Thursday November 15, 2018 @05:56AM (#18458347)
I went to look this up... I think actually the features I describe are more features of Wireshark which provides a Lua API, which is what confused me.

That Erlang syntax reminds of the python 'struct' [0], except it's generating the unpacking logic at compile time, am I right? Yes that is indeed very cool - it doesn't even seem that hard to do I'd wonder why other languages don't have a similar facility!

[0] https://docs.python.org/2/library/struct.html

Re:A Better Way to Implement Bit Fields by masklinn on Thursday November 15, 2018 @06:48AM (#18458526)
> That Erlang syntax reminds of the python 'struct' [0], except it's generating the unpacking logic at compile time, am I right?

Kinda, however there are a few superior items to the bit syntax:

* In Erlang, the bindings are in the expression itself (whether packing or unpacking) which makes the correspondance easier.

* Because the bindings are embedded and set left to right, it becomes possible to use one binding as parameter to the next pattern e.g. `L:3, D:L/binary` will first parse 3 bits as an unsigned integer L, then extract the next L bytes as a binary substring.

* Also Erlang's bit syntax can do sub-byte matching, I don't think python's struct can.

* And Erlang's bit syntax is much clearer and orthogonal (modulo knowing the — convenient — defaults) e.g. where extracting a little-endian 32-bit signed integer with struct is "<i" with Erlang it's :32/integer-signed-little (32 units, integer, signed, little-endian; the default unit size for integers is the bit). More verbose, but also completely explicit and more flexible, and very easy to change.

Re:A Better Way to Implement Bit Fields by rusk on Thursday November 15, 2018 @08:00AM (#18458895)
That is wonderful ... I’ve been looking for something like this for years. Don’t suppose you know off the top of your head are there any Erlang compilers for java and if they’re any good?
Re:A Better Way to Implement Bit Fields by rwmj on Thursday November 15, 2018 @06:12AM (#18458403)
I wrote ocaml-bitstring (based on Erlang) and it also works by generating the code to handle the bits at compile time. It's fully general too - eg. if you describe a structure of 1 bit followed by an array of bytes then it will add the necessary bit shifts automatically when reading the bytes. Or if you describe a struct where everything is aligned then at compile time it will generate the most efficient code. You can even have variable length bitfields (a huge headache when implementing, but it does deliver the best possible code based on what it can prove at compile time).

[1] https://rwmj.wordpress.com/2010/02/03/on-the-awesomeness-of-...

Re:A Better Way to Implement Bit Fields by rusk on Thursday November 15, 2018 @05:58AM (#18458355)
> I'd wonder why other languages don't have a similar facility

Actually this is the kind of thing lex/yacc/Bison, javacc etc. do ... except I think only for text ...