home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
drdobbs
/
1990
/
03
/
dunteman.lst
< prev
next >
Wrap
File List
|
1990-02-13
|
4KB
|
158 lines
STRUCTURED PROGRAMMING COLUMN
by Jeff Duntemann
[LISTING ONE]
(*---------------------------------------------------*)
(* BITWISE.MOD *)
(* Definition Module *)
(* *)
(* Bit-manipulation routines for Modula-2 *)
(* *)
(* by Jeff Duntemann *)
(* For DDJ : March 1990 *)
(* Last modified 11/25/89 *)
(*---------------------------------------------------*)
DEFINITION MODULE Bitwise;
PROCEDURE And(A,B : CARDINAL) : CARDINAL;
PROCEDURE Or(A,B : CARDINAL) : CARDINAL;
PROCEDURE Xor(A,B : CARDINAL) : CARDINAL;
PROCEDURE Not(Target : CARDINAL) : CARDINAL;
PROCEDURE SetBit(Target : CARDINAL; BitNum : CARDINAL) : CARDINAL;
PROCEDURE ClearBit(Target : CARDINAL; BitNum : CARDINAL) : CARDINAL;
PROCEDURE TestBit(Target : CARDINAL; BitNum : CARDINAL) : BOOLEAN;
PROCEDURE SHR(Target : CARDINAL; By : CARDINAL) : CARDINAL;
PROCEDURE SHL(Target : CARDINAL; By : CARDINAL) : CARDINAL;
END Bitwise.
[LISTING TWO]
(*---------------------------------------------------*)
(* BITWISE.MOD *)
(* Implementation Module *)
(* *)
(* Bit-manipulation routines for Modula-2 *)
(* *)
(* by Jeff Duntemann *)
(* For DDJ : March 1990 *)
(* Last modified 11/25/89 *)
(* *)
(* NOTES ON THE CODE: *)
(* *)
(* In all cases below, BitNum MOD 16 is used as a *)
(* means of ensuring that BitNum will be in the *)
(* range of 0..15. MOD 16 divides by 16 but returns *)
(* the remainder, which cannot be over 15 when you *)
(* divide by 16. *)
(*---------------------------------------------------*)
IMPLEMENTATION MODULE Bitwise;
VAR
I : CARDINAL;
TempSet : BITSET;
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 Xor(A,B : CARDINAL) : CARDINAL;
BEGIN
RETURN CARDINAL(BITSET(A) / BITSET(B));
END Xor;
PROCEDURE Not(Target : CARDINAL) : CARDINAL;
BEGIN
RETURN CARDINAL({0..15} - BITSET(Target));
END Not;
PROCEDURE SetBit(Target : CARDINAL; BitNum : CARDINAL) : CARDINAL;
BEGIN
TempSet := BITSET(Target); (* INCL does not operate on expressions! *)
INCL(TempSet,BitNum MOD 16);
RETURN CARDINAL(TempSet); (* Cast the target back to type CARDINAL *)
END SetBit;
PROCEDURE ClearBit(Target : CARDINAL; BitNum : CARDINAL) : CARDINAL;
BEGIN
TempSet := BITSET(Target); (* EXCL does not operate on expressions! *)
EXCL(TempSet,BitNum MOD 16);
RETURN CARDINAL(TempSet); (* Cast the target back to type CARDINAL *)
END ClearBit;
PROCEDURE TestBit(Target : CARDINAL; BitNum : CARDINAL) : BOOLEAN;
BEGIN
IF (BitNum MOD 16) IN BITSET(Target) THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END;
END TestBit;
PROCEDURE SHR(Target : CARDINAL; By : CARDINAL) : CARDINAL;
BEGIN
FOR I := 1 TO By DO
Target := Target DIV 2;
END;
RETURN Target;
END SHR;
PROCEDURE SHL(Target : CARDINAL; By : CARDINAL) : CARDINAL;
BEGIN
FOR I := 1 TO By DO
Target := Target * 2;
END;
RETURN Target;
END SHL;
END Bitwise.