comp.lang.ada
 help / color / mirror / Atom feed
* mixing integer and logical operations
@ 1993-05-24 17:18 Step he Leake
  0 siblings, 0 replies; 2+ messages in thread
From: Step he Leake @ 1993-05-24 17:18 UTC (permalink / raw)


Here is the much-delayed summary of the discussion of bit-wise logical operator
s
on integers. I was originally vehemently opposed to them, since they violate th
e
abstraction that integers count things, while an array of bits does not.
However, a few good examples of their use in legitimate algorithms have led me
to change my mind. On the down side, I also got an example of what I still
believe is an improper use.

First, the good stuff:

>From DEWAR@SCHONBERG.CS.NYU.EDU (Robert Dewar):

   Compute:  number of bits set to 1 in a word N

   Method:  count := 0;
	    while N != 0 loop
	      count := count + 1;
	      N := N and (N - 1);
	    end loop

	Here the essential part of the algorithm is the subtraction of 1
	from N.

This algorithm is used in communications processing (parity checking etc), and
it truly mixes integer arithmetic and logical operations.

>From stt@dsd.camb.inmet.com (Tucker Taft):

    1) Hashing
         function Hash(X : String; Mask : Unsigned) return Unsigned is
             Hash_Value : Unsigned := 0;
         begin
             -- Xor all of the characters of X together, shifting left
             -- one between characters, and preserving only bits on in
             -- "Mask."
             for I in X'Range loop
                 Hash_Value := 
                   (Hash_Value*2 xor Character'Pos(X(I))) and Mask;
             end loop;
             return Hash_Value;
         end Hash;

    2) Jumping up to next multiple of 2**N
         (X or 2**N-1) + 1

    3) Truncating down to nearest multiple of 2**N
         X and not (2**N-1)

    These kinds of calculations are quite common in systems programming.

Apparently the 9X revision team has fully endorsed this extension, so I'll have
to bow to their decision.

On the other hand, here's an example of (in my opinion) wrong usage (name 
omitted because I couldn't parse the return path):

   subtype MODE_T  is C_UNSIGNED_SHORT; -- File mode bits

   IRWXU : constant MODE_T := 8#700#; -- Read + write + execute, owner
   IRUSR : constant MODE_T := 8#400#; -- Read permission, owner
   IWUSR : constant MODE_T := 8#200#; -- Write permission, owner
   IXUSR : constant MODE_T := 8#100#; -- Execute/search permission, owner
   IRWXG : constant MODE_T := 8#070#; -- Read + write + execute, group
   [...]

   procedure CHMOD (PATH : in STRING; MODE : in MODE_T);
   [...]

   CHMOD ("foo", IRUSR or IWUSR or IRGRP);

This would be MUCH better done with a record and rep clause:

type MODE_FLAGS is
record
    Owner_Read     : BOOLEAN;
    Owner_Write    : BOOLEAN;
    Owner_Execute  : BOOLEAN;
    Group_Read     : BOOLEAN;
    Group_Write    : BOOLEAN;
    Group_Execute  : BOOLEAN;
    System_Read    : BOOLEAN;
    System_Write   : BOOLEAN;
    System_Execute : BOOLEAN;
end record;

for MODE_FLAGS use
record
    Owner_Read     at 1 range 2 .. 2;
    Owner_Write    at 1 range 1 .. 1;
    -- etc
end record;

The call to CHMOD would then be:

   CHMOD ("foo",
	Mode => (Owner_Read | Owner_Write | Group_Read => TRUE,
		 others => FALSE);

I find this much easier to understand.

It is mainly the potential misuse in the last example that motivated my origina
l
objection to mixing integer and logical operations. I guess we'll just have to
rely on good software engineers, rather than the language!

Stephen Leake	NASA Goddard Robotics Lab
internet : nbssal@robots.gsfc.nasa.gov

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: mixing integer and logical operations
@ 1993-05-26 20:52 Wes Groleau X7574
  0 siblings, 0 replies; 2+ messages in thread
From: Wes Groleau X7574 @ 1993-05-26 20:52 UTC (permalink / raw)


In article <24MAY199312181861@bambam.gsfc.nasa.gov> nbssal@bambam.gsfc.nasa.gov
 (Stephe Leake) writes:

>On the other hand, here's an example of (in my opinion) wrong usage (name 
>omitted because I couldn't parse the return path):

Obviously Unix stuff, so wierdness is to be expected  :-)

>.... would be MUCH better done with a record and rep clause:

You're right (but see previous remark)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1993-05-26 20:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-05-26 20:52 mixing integer and logical operations Wes Groleau X7574
  -- strict thread matches above, loose matches on Subject: below --
1993-05-24 17:18 Step he Leake

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox