[RFC] Operators GET, SET and CLEAR for L bits

Jose E. Marchesi jemarch@gnu.org
Thu Jan 29 01:32:49 GMT 2026


> Hi Jose.
>
> On Wed, Jan 28, 2026 at 11:48:18AM +0100, Jose E. Marchesi wrote:
>> 
>> > Hello people!
>> >
>> > I propose to add the following operators (to apply to L bits) to the
>> > extended standard prelude:
>> >
>> >
>> >   prio SET = 7, CLEAR = 7;
>> >
>> >   op SET = (bits b, int n) bits:
>> >      if n >= 1 AND n <= bits_width
>> >      then b OR 2r1 SHL (n-1)
>> >      else b
>> >      fi;
>> >
>> >   op CLEAR = (bits b, int n) bits:
>> >      if n >= 1 AND n <= bits_width
>> >      then b AND NOT (2r1 SHL (n-1))
>> >      fi;
>> >
>
>
>
> I like the idea of SET and CLEAR dyadic operators!
>
>
>
>> >   prio GET = 4;
>> >
>> >   op GET = (bits b, int n) bool:
>> >      if n >= 1 AND n <= bits_width
>> >      then ABS (b AND n) /= 0
>> >      fi;
>> 
>> Note GET is not needed, because the standard operator ELEM does exactly
>> that:
>> 
>>    flags ELEM flag_with
>> 
>
>
> Cool!
>
>
>> 
>> but it counts from the left! i.e. it gives you the boolean corresponding
>> to the ith boolean starting from the left :)
>
>
> Yeah! Casting to []bool is also does that. Unfortunately (esp. for me)
> RR authors were big-endian people :(


I think we may want to diverge from RR when it comes to ELEM in
-std=gu68 mode, and start counting from the right instead than from the
left.

The reason is not personal preference or big-endian vs. little-endian.

It is that if you count from the left, and you communicating with C or
other foreign code via formal holes, you would need to do things like:

  /* iflag */
  #define GA68_IGNCR  (1 << 31)
  #define GA68_ICRNL  (1 << 30)
  #define GA68_IXON   (1 << 29)
  #define GA68_IGNBRK (1 << 28)
  #define GA68_PARMRK (1 << 27)
  #define GA68_BRKINT (1 << 26)
  #define GA68_ISTRIP (1 << 25)
  #define GA68_INLCR  (1 << 24)

and that only works if unsigned int is 32 bits wide.  If it happens that
unsigned int is, say, 16 bits wide, then you need another set of masks.
It is way better to use:

  #define GA68_IGNCR  (1 << 0)
  #define GA68_ICRNL  (1 << 1)
  #define GA68_IXON   (1 << 2)
  #define GA68_IGNBRK (1 << 3)
  #define GA68_PARMRK (1 << 4)
  #define GA68_BRKINT (1 << 5)
  #define GA68_ISTRIP (1 << 6)
  #define GA68_INLCR  (1 << 7)

So I propose to do:

- In -std=algol68, ELEM, SET and CLEAR on `L bits' expect the 1-based
  bit number starting from the left, as mandated by the RR.

- In -std=gnu68, ELEM, SET and CLEAR on `L bits' expect the 1-based bit
  number starting from the _right_ instead.

IMO it is not a problem that one you rowe a bits to a []bool the
resulting booleans are indexed from left to right instead... these are
two different operations on values of different modes after all.

WDYT?
  

>
>
>> 
>> Also I now realize we still have to implement the bitspack standard
>> procedures..
>> 
>
> Good outcome!


More information about the Algol68 mailing list