
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.




% performs bitwise and, or, not, left-shift and right shift on the integers n1 and n2 %
% Algol W does not have xor, arithmetic right shift, left rotate or right rotate      %
procedure bitOperations ( integer value n1, n2 ) ;
begin
    bits b1, b2;
    % the Algol W bitwse operations operate on bits values, so we first convert the   %
    % integers to bits values using the builtin bitstring procedure                   %
    % the results are converted back to integers using the builtin number procedure   %
    % all Algol W bits and integers are 32 bits quantities                            %
    b1 := bitstring( n1 );
    b2 := bitstring( n2 );
    % perform the operaations and display the results as integers                     %
    write( n1, " and ", n2, " = ", number( b1 and b2 ) );
    write( n1, " or  ", n2, " = ", number( b1 or  b2 ) );
    write( "                "
         ,     " not ", n1, " = ", number(    not b1 ) );
    write( n1, " shl ", n2, " = ", number( b1 shl n2 ), " (  left-shift )"  );
    write( n1, " shr ", n2, " = ", number( b1 shr n2 ), " ( right-shift )"  )
end bitOPerations ;