File : bitarith.sp


$ spar bitarith
A and B =  170
A or  B =  255
A xor B =  85

A < B =  256
A >> B =  64
A >>> B =  64
A rotl B =  256
A rotr B =  64


#!/usr/local/bin/spar

pragma annotate( summary, "bitarith" )
       @( description, "Write a routine to perform a bitwise AND, OR, and XOR on" )
       @( description, "two integers, a bitwise NOT on the first integer, a left" )
       @( description, "shift, right shift, right arithmetic shift, left rotate," )
       @( description, "and right rotate. All shifts and rotates should be done on" )
       @( description, "the first integer with a shift/rotate amount of the second" )
       @( description, "integer." )
       @( see_also, "http://rosettacode.org/wiki/Bitwise_operations" )
       @( author, "Ken O. Burtch" );
pragma license( unrestricted );

pragma restriction( no_external_commands );

procedure bitarith is
  A : constant natural := 255;
  B : constant natural := 170;
  X : constant natural := 128;
  N : constant natural := 1;
begin
  put( "A and B = " ) @ (A and B); new_line;
  put( "A or  B = " ) @ (A or  B); new_line;
  put( "A xor B = " ) @ (A xor B); new_line;
  new_line;
  put( "A << B = " ) @ ( numerics.shift_left( X, N ) ); new_line;
  put( "A >> B = " ) @ ( numerics.shift_right( X, N ) ); new_line;
  put( "A >>> B = " ) @ ( numerics.shift_right_arithmetic( X, N ) ); new_line;
  put( "A rotl B = " ) @ ( numerics.rotate_left( X, N ) ); new_line;
  put( "A rotr B = " ) @ ( numerics.rotate_right( X, N ) ); new_line;
end bitarith;

-- VIM editor formatting instructions
-- vim: ft=spar