Bitwise operations: Difference between revisions

Content added Content deleted
(Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag)
No edit summary
Line 5,712: Line 5,712:
print ("a asr b: " ^ Word.fmt StringCvt.DEC (Word.>> (a, b) ) ^ "\n") (* logical right shift *)
print ("a asr b: " ^ Word.fmt StringCvt.DEC (Word.>> (a, b) ) ^ "\n") (* logical right shift *)
)</syntaxhighlight>
)</syntaxhighlight>

=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/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." )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Bitwise_operations" );
pragma license( unrestricted );

pragma software_model( shell_script );
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;</syntaxhighlight>

=={{header|Stata}}==
=={{header|Stata}}==
Stata does not have bitwise operators as of version 15.1. It's possible to use Mata functions '''[https://www.stata.com/help.cgi?mf_inbase inbase]''' and '''frombase''' to convert integers to binary strings, and operate on these, but it will be much slower than native operators. William Matsuoka has written functions for this [http://www.wmatsuoka.com/stata/building-an-api-library here].
Stata does not have bitwise operators as of version 15.1. It's possible to use Mata functions '''[https://www.stata.com/help.cgi?mf_inbase inbase]''' and '''frombase''' to convert integers to binary strings, and operate on these, but it will be much slower than native operators. William Matsuoka has written functions for this [http://www.wmatsuoka.com/stata/building-an-api-library here].