Jump to content

Set right-adjacent bits: Difference between revisions

→‎{{header|Raku}}: Add a Raku example
m (whoops)
(→‎{{header|Raku}}: Add a Raku example)
Line 225:
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result: 011110000000111100000011110000011110000111100011110011110111111111</pre>
 
=={{header|Raku}}==
A left-to-right ordered collection of bits is more commonly referred to as an Integer in Raku.
 
<lang perl6>sub rab (Int $n is copy, Int $b = 1) {
for 1..$n.msb -> $i {
next unless $n +& (1 +< $i);
$n +|= exp($i - $_, 2) for 1 .. $b
}
$n
}
 
# Test with a few integers.
 
for 8, 4, 5444684034376312377319904082902529876242, 15 -> $integer, $bits {
 
say "\nInteger: $integer - Right-adjacent-bits: up to $bits";
 
.say for ^$bits .map: -> $b { $integer.&rab($b).base: 2 };
 
}</lang>
{{out}}
<pre>Integer: 8 - Right-adjacent-bits: up to 4
1000
1100
1110
1111
 
Integer: 5444684034376312377319904082902529876242 - Right-adjacent-bits: up to 15
1000000000000001000000000000010000000000000100000000000010000000000010000000000100000000010000000010000000100000010000010000100010010
1100000000000001100000000000011000000000000110000000000011000000000011000000000110000000011000000011000000110000011000011000110011011
1110000000000001110000000000011100000000000111000000000011100000000011100000000111000000011100000011100000111000011100011100111011111
1111000000000001111000000000011110000000000111100000000011110000000011110000000111100000011110000011110000111100011110011110111111111
1111100000000001111100000000011111000000000111110000000011111000000011111000000111110000011111000011111000111110011111011111111111111
1111110000000001111110000000011111100000000111111000000011111100000011111100000111111000011111100011111100111111011111111111111111111
1111111000000001111111000000011111110000000111111100000011111110000011111110000111111100011111110011111110111111111111111111111111111
1111111100000001111111100000011111111000000111111110000011111111000011111111000111111110011111111011111111111111111111111111111111111
1111111110000001111111110000011111111100000111111111000011111111100011111111100111111111011111111111111111111111111111111111111111111
1111111111000001111111111000011111111110000111111111100011111111110011111111110111111111111111111111111111111111111111111111111111111
1111111111100001111111111100011111111111000111111111110011111111111011111111111111111111111111111111111111111111111111111111111111111
1111111111110001111111111110011111111111100111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111001111111111111011111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.