
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.




MODULE Bitwise EXPORTS Main;
 
IMPORT IO, Fmt, Word;
 
VAR c: Word.T;
 
PROCEDURE Bitwise(a, b: INTEGER) =
  BEGIN
    IO.Put("a AND b: " & Fmt.Int(Word.And(a, b)) & "\n");
    IO.Put("a OR b: " & Fmt.Int(Word.Or(a, b)) & "\n");
    IO.Put("a XOR b: " & Fmt.Int(Word.Xor(a, b)) & "\n");
    IO.Put("NOT a: " & Fmt.Int(Word.Not(a)) & "\n");
    c := a;
    IO.Put("c LeftShift b: " & Fmt.Unsigned(Word.LeftShift(c, b)) & "\n");
    IO.Put("c RightShift b: " & Fmt.Unsigned(Word.RightShift(c, b)) & "\n");
    IO.Put("c LeftRotate b: " & Fmt.Unsigned(Word.LeftRotate(c, b)) & "\n");
    IO.Put("c RightRotate b: " & Fmt.Unsigned(Word.RightRotate(c, b)) & "\n");
  END Bitwise;
 
BEGIN
  Bitwise(255, 5);
END Bitwise.