
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses




Task

Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate.

All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer.

If any operation is not available in your language, note it.




BEGIN
   COMMENT TO MY KNOWLEDGE SIMULA DOES NOT SUPPORT BITWISE OPERATIONS SO WE MUST WRITE PROCEDURES FOR THE JOB ;
   INTEGER WORDSIZE;
   WORDSIZE := 32;
   BEGIN
 
      PROCEDURE TOBITS(N,B); INTEGER N; BOOLEAN ARRAY B;
      BEGIN
         INTEGER I,BITN;
         FOR I := WORDSIZE-1 STEP -1 UNTIL 0 DO BEGIN
            BITN := MOD(N,2); B(I) := BITN<>0; N := N // 2;
         END;
      END TOBITS;
 
      INTEGER PROCEDURE FROMBITS(B); BOOLEAN ARRAY B;
      BEGIN
         INTEGER I, RESULT;
         FOR I := 0 STEP 1 UNTIL WORDSIZE-1 DO
             RESULT := 2 * RESULT + (IF B(I) THEN 1 ELSE 0);
         FROMBITS := RESULT;
      END FROMBITS;
 
      INTEGER PROCEDURE BITOP(A,B,F);
         INTEGER A,B;
         PROCEDURE F IS BOOLEAN PROCEDURE F(A,B); BOOLEAN A,B;;
      BEGIN
         INTEGER I;
         BOOLEAN ARRAY BA(0:WORDSIZE-1);
         BOOLEAN ARRAY BB(0:WORDSIZE-1);
         TOBITS(A,BA);
         TOBITS(B,BB);
         FOR I := 0 STEP 1 UNTIL WORDSIZE-1 DO BA(I) := F(BA(I),BB(I));
         BITOP := FROMBITS(BA);
      END BITOP;
 
      INTEGER PROCEDURE BITUOP(A,F);
         INTEGER A;
         PROCEDURE F IS BOOLEAN PROCEDURE F(A); BOOLEAN A;;
      BEGIN
         INTEGER I;
         BOOLEAN ARRAY BA(0:WORDSIZE-1);
         TOBITS(A,BA);
         FOR I := 0 STEP 1 UNTIL WORDSIZE-1 DO BA(I) := F(BA(I));
         BITUOP := FROMBITS(BA);
      END BITUOP;
 
      BOOLEAN PROCEDURE OPAND(A,B); BOOLEAN A,B; OPAND := A AND B;
      INTEGER PROCEDURE BITAND(A,B); INTEGER A,B; BITAND := BITOP(A,B,OPAND);
 
      BOOLEAN PROCEDURE OPOR(A,B); BOOLEAN A,B; OPOR := A OR B;
      INTEGER PROCEDURE BITOR(A,B); INTEGER A,B; BITOR := BITOP(A,B,OPOR);
 
      BOOLEAN PROCEDURE OPXOR(A,B); BOOLEAN A,B; OPXOR := (A AND NOT B) OR (NOT A AND B);
      INTEGER PROCEDURE BITXOR(A,B); INTEGER A,B; BITXOR := BITOP(A,B,OPXOR);
 
      BOOLEAN PROCEDURE OPNOT(A); BOOLEAN A; OPNOT := NOT A;
      INTEGER PROCEDURE BITNOT(A); INTEGER A; BITNOT := BITUOP(A,OPNOT);
 
      INTEGER PROCEDURE BITSHL(A,B); INTEGER A,B;
      BEGIN
          IF B < 0 THEN A := BITSHR(A,-B)
          ELSE WHILE B > 0 DO BEGIN A := 2 * A; B := B-1; END;
          BITSHL := A;
      END BITSHL;
 
      INTEGER PROCEDURE BITSHR(A,B); INTEGER A,B;
      BEGIN
          IF B < 0 THEN A := BITSHL(A,-B)
          ELSE WHILE B > 0 DO BEGIN A := A // 2; B := B-1; END;
          BITSHR := A;
      END BITSHR;
 
      INTEGER PROCEDURE BITROTR(A,B); INTEGER A,B;
      BEGIN
         INTEGER I,J;
         BOOLEAN ARRAY BA(0:WORDSIZE-1);
         BOOLEAN ARRAY BB(0:WORDSIZE-1);
         TOBITS(A,BA);
         FOR I := 0 STEP 1 UNTIL WORDSIZE-1 DO BEGIN
            J := MOD(I + B, WORDSIZE); BB(J) := BA(I);
         END;
         BITROTR := FROMBITS(BB);
      END BITROTR;
 
      INTEGER PROCEDURE BITROTL(A,B); INTEGER A,B;
         BITROTL := BITROTR(A,-B);
 
      PROCEDURE BITWISE(A,B); INTEGER A,B;
      BEGIN
        OUTTEXT("A AND B   : "); OUTINT(BITAND(A,B),0); OUTIMAGE;
        OUTTEXT("A OR B    : "); OUTINT(BITOR (A,B),0); OUTIMAGE;
        OUTTEXT("A XOR B   : "); OUTINT(BITXOR(A,B),0); OUTIMAGE;
        OUTTEXT("NOT A     : "); OUTINT(BITNOT(A),  0); OUTIMAGE;
        OUTTEXT("A << B    : "); OUTINT(BITSHL(A,B),0); OUTIMAGE;  ! LEFT SHIFT ;
        OUTTEXT("A >> B    : "); OUTINT(BITSHR(A,B),0); OUTIMAGE;  ! ARITHMETIC RIGHT SHIFT ;
        OUTTEXT("A ROTL B  : "); OUTINT(BITROTL(A,B),0); OUTIMAGE;  ! ROTATE LEFT ;
        OUTTEXT("A ROTR B  : "); OUTINT(BITROTR(A,B),0); OUTIMAGE;  ! ROTATE RIGHT ;
      END BITWISE;
 
      BITWISE(14,3);
   END;
END
 