Jump to content

Set right-adjacent bits: Difference between revisions

(→‎{{header|Raku}}: Incomplete)
Line 51:
* Use it to show, here, the results for the input examples above.
* Print the output aligned in a way that allows easy checking by eye of the binary input vs output.
 
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Set_right-adjacent_bits
use warnings;
 
while( <DATA> )
{
my ($n, $input) = split;
my $width = length $input;
my $result = '';
$result |= substr 0 x $_ . $input, 0, $width for 0 .. $n;
print "n = $n width = $width\n input $input\nresult $result\n\n";
}
 
__DATA__
2 1000
2 0100
2 0011
2 0000
0 010000000000100000000010000000010000000100000010000010000100010010
1 010000000000100000000010000000010000000100000010000010000100010010
2 010000000000100000000010000000010000000100000010000010000100010010
3 010000000000100000000010000000010000000100000010000010000100010010</lang>
{{out}}
<pre>
n = 2 width = 4
input 1000
result 1110
 
n = 2 width = 4
input 0100
result 0111
 
n = 2 width = 4
input 0011
result 0011
 
n = 2 width = 4
input 0000
result 0000
 
n = 0 width = 66
input 010000000000100000000010000000010000000100000010000010000100010010
result 010000000000100000000010000000010000000100000010000010000100010010
 
n = 1 width = 66
input 010000000000100000000010000000010000000100000010000010000100010010
result 011000000000110000000011000000011000000110000011000011000110011011
 
n = 2 width = 66
input 010000000000100000000010000000010000000100000010000010000100010010
result 011100000000111000000011100000011100000111000011100011100111011111
 
n = 3 width = 66
input 010000000000100000000010000000010000000100000010000010000100010010
result 011110000000111100000011110000011110000111100011110011110111111111
 
</pre>
 
=={{header|Python}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.