5 ms·
J also has these, though it might be a stretch to say it "names" them. Some might argue it's more direct, however: https://code.jsoftware.com/wiki/Vocabulary/bd
by xelxebar 4y ago
J also has these, though it might be a stretch to say it "names" them. Some might argue it's more direct, however:
https://code.jsoftware.com/wiki/Vocabulary/bdot#bitwise https://code.jsoftware.com/wiki/Vocabulary/bdot#bitwise
Basically, if you read off the bits of the truth table left-to-right, top-to-bottom, you get a string of 4 bits. This encodes a 4-bit integer, which is J's "name" of the function. E.g. for OR
\ | 0 1
- | ---
0 | 0 1
1 | 1 0
which has a truth table like
0 1
1 0
which flattens to 0 1 1 0, gets the encoding 0b0110 = 6. So J would write the logical XOR as (6 b.). To get the bitwise version, just add 16.
- bhaney 4y agoThat's horrifying
- geocar 4y agoScroll the linked article down: the lisp “boole” function works the same way i.e. (boole 6 x y) is xor. Lisp has the same thing. But maybe the organisation isn’t clear? Or is it the numeric value that you fear? There is a fantastic use for this, that if you don’t know, you may want to think about for more than a few seconds.
- rawling 4y ago> There is a fantastic use for this, that if you don’t know, you may want to think about for more than a few seconds. Is it one of the things in the table towards the bottom of the J article? I have to admit, I'm struggling to read the syntax.
- geocar 4y ago> Is it one of the things in the table towards the bottom of the J article? Erm, no. That table suggests using b. as a replacement for other (clearer) algorithms for performance reasons, rather than parameterisation. What I mean by parameterisation is this: if you have a function that takes some arbitrary bitwise operation, instead of taking a lambda, you can pass the truth-table directly. This idea is mentioned only briefly in the J article (More Info, ¶2): 2. Operand m may be an array, in which case each result cell will be the array of the results of the logical functions specified by m. Maybe that is too-obvious to an array programmer and not-enough-obvious to someone who isn't, and so it is hard to properly consider the implications or applications of this. Here's one: Parsing AND/OR operations from an expression (like an SQL expression). You could use a tree of operators and then walk the tree for every row you want to consider or you could store the compositions as a list and do a single reduction. Is that enough for you to get the idea of what is going on here?
- rawling 4y agoAh, ok. I'd got as far as going, you can call "&","|" etc. a number based on its truth table, and then you can extend this to other operators we don't think about so much by completing the number range, but I hadn't got so far as going... what... a = (o >> (x + 2y)) % 2 or something. I'd hoped it might involve adding/multiplying different operators together or something like that.
- mtlmtlmtlmtl 4y agoWouldn't it be more constructive to name this fantastic use, instead of just mentioning it and snarkily suggesting it's obvious?
- 082349872349872 4y agoMaybe geocar is too young to remember ROT13, and doesn't want to spoil things for those of us who did want to think about it first. fbzrgvzrf jr jnag gb pbzcbfr bcrengvbaf
- Someone 4y ago> To get the bitwise version, just add 16. That’s the only horrifying part. It gives up the ability to do this for functions taking more than two inputs. They should have used -6 for the bitwise version. Otherwise, this technique of implementing functions as table lookups with their names being the table is brilliant. That’s self-documenting code. Imagine not having to learn what max means for IEEE floats, but just being able to read it from its function name. In fact, you would not have to implement any function; you’re just write its name, and the compiler would implement it for you. I also think they should infer from context whether (6 b.) takes three arguments and produces none, takes two arguments and produces one, takes one and produces two, or takes none and produces three. /s It would be fun to try and hack the above in a set of C macros. Might lead to a good IOCCC entry (https://www.ioccc.org/ https://www.ioccc.org/)
- oleganza 4y ago>In fact, you would not have to implement any function; you’re just write its name, and the compiler would implement it for you. Isn't this equivalent to placing the function code into its name, in other words, inlining the function? Looks cute for 2-bit programs, but I doubt it scales for 100-bit ones.