[comp.lang.modula2] Bitwise EXOR

Herman.Stevens@p21701.f601.n292.z2.fidonet.org (Herman Stevens) (09/24/90)

Hello there !

I am a beginner with Modula-II. I work on an Amiga with the M2Amiga
compiler. I fail to see how I can do a bitwise EXOR, something you
can easily write in C as :

         tmpbuf[i] = inbuf[i]^key[kp]

Can someone help me on this ?
Tnx,
     Herman


--  
uucp: uunet!m2xenix!puddle!2!292!601.21701!Herman.Stevens
Internet: Herman.Stevens@p21701.f601.n292.z2.fidonet.org

Ian.Green@f104.n153.z1.fidonet.org (Ian Green) (09/28/90)

   Assuming that the data type is a CARDINAL then try the following:
 
   a = b XOR c                                          ; pseudo code

   a := CARDINAL(BITSET(b)/BITSET(c));                  ; Modula-2
 
 
By using a type transfer to convert the data into a set, set operations 
can then be applied. Following the operation, a type transfer is used 
to convert back to the original type.
 
When I use set operations, I usually use SETs in the first place since 
technically the operations are set operations.
 
Ian Green



--  
uucp: uunet!m2xenix!puddle!153!104!Ian.Green
Internet: Ian.Green@f104.n153.z1.fidonet.org

Martin.Baur@p70.f801.n302.z2.fidonet.org (Martin Baur) (09/28/90)

In a message of <Sep 23 22:52> Herman Stevens (2:292/601.21701@fidonet) writes:
 

  HS: "I am a beginner with Modula-II. I work on an Amiga with the M2Amiga
  HS: "compiler. I fail to see how I can do a bitwise EXOR, something you
  HS: "can easily write in C as :
  HS: "
  HS: "         tmpbuf[i] = inbuf[i]^key[kp]

BITSET (tmpbuf[i]) = BITSET (inbuf[i]) / BITSET (key[kp])

By the way don't be afraid of a possible code generating by the BITSET. It 
doesn't occur.



     !Beware of Martin Baur!
(His point of view: 2:302/801.70)
 


--  
uucp: uunet!m2xenix!puddle!2!302!801.70!Martin.Baur
Internet: Martin.Baur@p70.f801.n302.z2.fidonet.org

a665@mindlink.UUCP (Anthon Pang) (09/29/90)

Herman.Stevens@p21701.f601.n292.z2.fidonet.org writes:
> I am a beginner with Modula-II. I work on an Amiga with the M2Amiga
> compiler. I fail to see how I can do a bitwise EXOR, something you
> can easily write in C as :
> 
>          tmpbuf[i] = inbuf[i]^key[kp]
> 
> Can someone help me on this ?

I think the following work...

For booleans:

   x := ( a OR b) AND (NOT (a AND b));

and for other stuff (using sufficient type transfer):

   TYPE
     longbs = SET OF [0..31]; (* LONG WORD BITSET *)

   PROCEDURE XOR ( x, y: longbs ) : longbs;
     CONST
       allset = longbs{0..31};
     BEGIN
       RETURN (x + y) * (allset - (x * y));
     END XOR;

For the latter case, your example would translate (roughly) to:
tmpbuf[i] := LONGCARD( XOR(longbs(inbuf[i]), longbs(key[kp])));
assuming tmpbuf is an ARRAY of LONGCARD.  Change "LONGCARD" to "CARDINAL" and
"31" to "15"...and it should work for word size (16 bit) values as well (ie on
the Amiga).

a665@mindlink.UUCP (Anthon Pang) (09/30/90)

steve@cs.su.oz writes:
> It ain't as simple as in C (but then neither are bit-ands and ors). Try using
> the symmetric set difference operator by casting. Something
> like this might work:
> 
>         tmpbuf[i] := CARDINAL(BITSET(inbuf[i]) / BITSET(key[kp]))

preston@titan.rice.edu writes:
> Well, if you want to fiddle bits, use BITSETs in the first place.
> Then, assuming you've declared all your variables as arrays
> of BITSET, you simply write
> 
>         tmpbuf[i] := inbuf[i] / key[kp]
> 
> I think "symmetric set difference" is a rather unhelpful name,
> but perhaps it's more common in Europe.

John.Matthews@comp.vuw.ac.nz writes:
> I have an idea that in Modula-2 speak, XOR is represented as
> Symmetrical Difference (of set types).
> 
> Convert whatever to a set type, eg BitSet, then
> 
> a = b XOR c
>  is equal to
> 
> a = b / c;

Gee...I've been outnumbered 3 to 1...I guess I was wrong :)

preston@titan.rice.edu (Preston Briggs) (09/30/90)

In article <1230@cluster.cs.su.oz.au> steve@cluster.cs.su.oz (Stephen Russell) writes:
>Herman Stevens asks:
>>I fail to see how I can do a bitwise EXOR, something you
>>can easily write in C as :
>>
>>         tmpbuf[i] = inbuf[i]^key[kp]
>>
>>Can someone help me on this ?
>
>It ain't as simple as in C (but then neither are bit-ands and ors). Try
>using the symmetric set difference operator by casting. Something
>like this might work:
>
>	tmpbuf[i] := CARDINAL(BITSET(inbuf[i]) / BITSET(key[kp]))

Well, if you want to fiddle bits, use BITSETs in the first place.
Then, assuming you've declared all your variables as arrays
of BITSET, you simply write

	tmpbuf[i] := inbuf[i] / key[kp]

I think "symmetric set difference" is a rather unhelpful name,
but perhaps it's more common in Europe.

Note also that the BITSET idea is handy for testing and setting
particular bits.

	IF 2 IN key[kp] THEN ...

or

	tmpbuf[i] := key[kp] + {5, 7}

-- 
Preston Briggs				looking for the great leap forward
preston@titan.rice.edu

steve@cs.su.oz (Stephen Russell) (09/30/90)

Herman Stevens asks:
>I fail to see how I can do a bitwise EXOR, something you
>can easily write in C as :
>
>         tmpbuf[i] = inbuf[i]^key[kp]
>
>Can someone help me on this ?

It ain't as simple as in C (but then neither are bit-ands and ors). Try
using the symmetric set difference operator by casting. Something
like this might work:

	tmpbuf[i] := CARDINAL(BITSET(inbuf[i]) / BITSET(key[kp]))

You may have to fiddle with the appropriate set types to get this past
the compiler, but hopefully you get the idea.

Cheers

John.Matthews@comp.vuw.ac.nz (John Matthews) (10/01/90)

I have an idea that in Modula-2 speak, XOR is represented as 
Symmetrical Difference (of set types).

Convert whatever to a set type, eg BitSet, then

a = b XOR c
 is equal to

a = b / c;

Try It.

pf@artcom0.north.de (Peter Funk) (10/01/90)

Herman.Stevens@p21701.f601.n292.z2.fidonet.org (Herman Stevens) writes:
[...]
hs> ......... I fail to see how I can do a bitwise EXOR, something you
hs> can easily write in C as :

hs>          tmpbuf[i] = inbuf[i]^key[kp]

hs> Can someone help me on this ?

Bit-Operations are usually coded in Modula-2 using the Type BITSET:
Assume : VAR b1, b2, b3 : BITSET;

	b3 := b1 * b2;     <-->     b3 <- b1 AND b2;
	b3 := b1 + b2;     <-->     b3 <- b1 OR  b2;
	b3 := b1 - b2;     <-->     b3 <- b1 AND NOT b2;
	b3 := b1 / b2;     <-->     b3 <- b1 XOR b2;

So your problem may be solved typing :
	     tmpbuf [i] := yourType (BITSET(inbuf [i]) / BITSET(key [kp]));
where 'yourType' is the element data type of 'tmpbuf'.

Gruss, Peter
-->
Peter Funk \\ ArtCom GmbH, Schwachhauser Heerstr. 78, D-2800 Bremen 1
Work at home: Oldenburger Str.86, D-2875 Ganderkesee 1 /+49 4222 6018 (8am-6pm)
>> PLEASE Don't send BIG mails (oversea) ! I've to pay for it : $0.3/kB
   Don't use the bang path of this news article for mails (They will bounce).
   Only the address 'pf@artcom0.north.de' will work. Thank You ! <<

preston@titan.rice.edu (Preston Briggs) (10/01/90)

In article <3356@mindlink.UUCP> a665@mindlink.UUCP (Anthon Pang) writes:
>Herman.Stevens@p21701.f601.n292.z2.fidonet.org writes:
>> I am a beginner with Modula-II. I work on an Amiga with the M2Amiga
>> compiler. I fail to see how I can do a bitwise EXOR, something you

>For booleans:
>
>   x := ( a OR b) AND (NOT (a AND b));

Or more simply,

	x := a # b;

-- 
Preston Briggs				looking for the great leap forward
preston@titan.rice.edu

a665@mindlink.UUCP (Anthon Pang) (10/02/90)

My apologies to the net, for my posting of such utter drivel.  Apparently the
conversion effects of the "C brainwashing" I underwent have not worn off, and I
am completely in the dark with regards to coding in M2.

randy@m2xenix.psg.com (Randy Bush) (10/03/90)

Martin.Baur@p70.f801.n302.z2.fidonet.org (Martin Baur) writes:

> BITSET (tmpbuf[i]) = BITSET (inbuf[i]) / BITSET (key[kp])

Yes, some compilers might allow that, but this is Modula-2, not PL/I which had
such 'pseudovariables' (and even in ANSI C, you can't cast an l-value).

If you must cast, as opposed to using declared set types, then try

  tmpbuf[i] = TmpBufElementType ( BITSET (inbuf[i]) / BITSET (key[kp]) )

or something similar.

-- 
..!{uunet,qiclab,intelhf}!m2xenix!randy