Bitwise operations: Difference between revisions

Content added Content deleted
mNo edit summary
m (→‎{{header|Perl}}: future-proof for 5.36, explicit :prototype)
Line 4,522: Line 4,522:
<syntaxhighlight lang="perl">use integer;
<syntaxhighlight lang="perl">use integer;
sub bitwise($$) {
sub bitwise :prototype($$) {
($a, $b) = @_;
($a, $b) = @_;
print 'a and b: '. ($a & $b) ."\n";
print 'a and b: '. ($a & $b) ."\n";
Line 4,535: Line 4,535:
print 'a >> b: ', $a >> $b, "\n"; # arithmetic right shift
print 'a >> b: ', $a >> $b, "\n"; # arithmetic right shift
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Phix}}==
=={{header|Phix}}==
Phix has four builtin bitwise operations (and/or/xor/not)_bits, which each have sequence and unsigned variants. Note careful use of latter (unsigned) routines here, since Phix naturally preserves signs (and common sense) when it can, rather than rudely treat, for instance, +4,294,967,295 as -1, unless explicitly told to do so as it is below. Likewise the builtin shift operators deliver signed and unbounded results, so we'll wrap them here. There are no builtin rotate routines, but easy enough to devise. The distributed copy (1.0.2+) also contains an (older) inline assembly version, which is obviously not JavaScript compatible, but may be significantly faster, for desktop-only applications.
Phix has four builtin bitwise operations (and/or/xor/not)_bits, which each have sequence and unsigned variants. Note careful use of latter (unsigned) routines here, since Phix naturally preserves signs (and common sense) when it can, rather than rudely treat, for instance, +4,294,967,295 as -1, unless explicitly told to do so as it is below. Likewise the builtin shift operators deliver signed and unbounded results, so we'll wrap them here. There are no builtin rotate routines, but easy enough to devise. The distributed copy (1.0.2+) also contains an (older) inline assembly version, which is obviously not JavaScript compatible, but may be significantly faster, for desktop-only applications.