[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mumps-l
Subject:    Re: Bit Wise Function
From:       Ben Bishop <aci () SHORE ! NET>
Date:       1998-01-26 22:01:48
[Download RAW message or body]


"Hirz, Lee, CON, OASD/HA" <Lee.Hirz@ha.osd.mil> writes:

>You have to make use of the ability of M to know the ASCII value of the
>character being evaluated as follows.  If X contains one character, then
>$A(X)="and ASCII value".  Using this fact the ASCII value can be
>manipulated as desired.  Bits can be AND'd or OR'd together.  So to do a
>bit by bit AND of bytes stored in the variables X & Y as single
>characters, you would first calculate their ASCII values.  Then you need
>to find out if each bit is set.  When both bits are set, you need to add
>the value of that bit to a sum.  So, in ANSII standard code (I hop this
>is right, I didn't test it...):

>In the following code, AX and AY are ANSII values for X and Y, Answer is
>a character that contains the result of bit by bit ANDing of X and Y.

>        Set AX=$A(X),AY=$A(Y),Answer=0
>        For J=2:2:512 Do
>         . Set BX=AX#J\(J/2)
>         . Set BY=AY#J\(J/2)
>         . S Answer=$Select(BX&BY:1,1:0)*J+Answer
>         . Q

>And if you want to change to OR'ing the bits, simply change the $Select
>to "$Select(BX!BY:1,1:0)".

The trouble starts when you get to strings instead of single
characters.  How do you 'AND' "ABCD" with "XYZ"?  Which side do you
pad out? (and I presume one woudl pad out with $C(0))

What happens when one character is a 16 bit character (like Kanji) and
the other is straight ASCII (7 bit)?

Unicode makes the possible character problems even 'worse' with 4
byte/ 32 bit characters.

90% of the work always seems to be those niggling little details that
you don't encounter often, but you have to deal with.

(I'd also have done the 1st $SELECT as $S('BX:0,'BY:0,1:1) and the
OR version as $S(BX:1,BY:1,1:0) -- I'd expect them to be faster just
from not _always_ having to lookup the 'BY' value)


--
Ben Bishop, Atlantic Consultants, Inc.                  aci @ shore.net
If you can keep your head while all those about you are losing
theirs, then perhaps you have misunderstood the situation. -- D.K.Moran

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic