karthy@iesd.auc.dk (Karsten Thygesen) (12/14/90)
Hello. I need binary functions in Modula-2, such as AND, OR NOT perhaps XOR. Do you know any librarys and/or procedures to do that?? I *could* make a set of flags, where flags are declared as boolean, but...It would take about 10 years, and i only got a week. Regards, Karsten. --- Karsten Thygesen karthy@iesd.auc.dk FidoNet: 2:231/79
jordan@aerospace.aero.org (Larry M. Jordan) (12/15/90)
The following will work for JPI which supports type transfer functions (you may have to use VAL in other M2's): PROCEDURE And(a,b:CARDINAL): CARDINAL; BEGIN RETURN CARDINAL(BITSET(a) * BITSET(b)) END And; PROCEDURE Or(a,b:CARDINAL): CARDINAL; BEGIN RETURN CARDINAL(BITSET(a) + BITSET(b)) END Or; PROCEDURE Not(a:CARDINAL): CARDINAL; BEGIN RETURN CARDINAL(BITSET(0) - BITSET(a)) END Not; PROCEDURE Xor(a,b:CARDINAL): CARDINAL; BEGIN RETURN And(Or(a,b), Not(And(a,b))) END Xor; You can, of course, optimize Xor by 'inlining' by hand the other ops. This solution requires no recourse to assembly language. -- Larry
jordan@aerospace.aero.org (Larry M. Jordan) (12/15/90)
Correction. 'Not' should be: CONST N = NumberOfBitsInCardinal; (* for JPI this is 16 *) PROCEDURE Not(a: CARDINAL): CARDINAL; BEGIN RETURN CARDINAL(BITSET{0..N-1} - BITSET(a)) END Not; and, not 'BITSET(0) - BITSET(a)' as mistakenly given earlier. -- Larry
jordan@aerospace.aero.org (Larry M. Jordan) (12/18/90)
Simplification. I had implemented Xor in terms of And,Not and Or. The 'semetric set difference' operator [/] on BITSETs IS exclusive or: PROCEDURE Xor(a,b: CARDINAL): CARDINAL; BEGIN RETURN CARDINAL(BITSET(a) / BITSET(b)) END Xor; -- Larry