Set right-adjacent bits: Difference between revisions
m
syntax highlighting fixup automation
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 53:
=={{header|Ada}}==
<
procedure Set_Right_Bits is
Line 127:
Show ("010000000000100000000010000000010000000100000010000010000100010010", 2);
Show ("010000000000100000000010000000010000000100000010000010000100010010", 3);
end Set_Right_Bits;</
{{out}}
<pre>
Line 164:
=={{header|AutoHotkey}}==
<
x := StrSplit(num)
for i, v in StrSplit(num)
Line 175:
res .= v
return res
}</
Examples:<
(join,
"1000"
Line 199:
MsgBox % result
return</
{{out}}
<pre>n=2; Width e = 4:
Line 234:
=={{header|F_Sharp|F#}}==
<
// Set right-adjacent bits. Nigel Galloway: December 21st., 2021
let fN g l=let rec fG n g=[|match n,g with ('0'::t,0)->yield '0'; yield! fG t 0
Line 243:
[("1000",2);("0100",2);("0010",2);("0001",2);("0000",2);("010000000000100000000010000000010000000100000010000010000100010010",0);("010000000000100000000010000000010000000100000010000010000100010010",1);("010000000000100000000010000000010000000100000010000010000100010010",2);("010000000000100000000010000000010000000100000010000010000100010010",3)]|>List.iter(fun(n,g)->printfn "%s\n%s" n (fN n g))
</syntaxhighlight>
{{out}}
<pre>
Line 268:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
sequences ;
Line 279:
{ 0b1000 0b0100 0b0010 0b0000 } [ 2 4 show nl ] each
0x10020080404082112 4 <iota> [ 66 show nl ] with each</
{{out}}
<pre>
Line 317:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 371:
fmt.Println(" Result :", sb.String())
}
}</
{{out}}
Line 405:
Implementation:
<
Here, we use J's bit array structure, so <tt>e</tt> is implicit in the length of the list.
Line 411:
Task examples:
<
task=: {{y,:x&smearright&.:b y}}
Line 425:
3 task '010000000000100000000010000000010000000100000010000010000100010010'
010000000000100000000010000000010000000100000010000010000100010010
011110000000111100000011110000011110000111100011110011110111111111</
<code>b</code> converts from character list to bit list (and its obverse converts back to character list, for easy viewing).
Line 431:
=={{header|Julia}}==
<
if n < 1
return s
Line 454:
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 2)
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 3)
</
<pre>
setrightadj("1000", 2) = "1110"
Line 471:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
ShowSetRightBits[b_String,n_Integer]:=Module[{poss,chars},
chars=Characters[b];
Line 485:
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",1]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",2]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",3]</
{{out}}
<pre>In : 1000
Line 512:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Set_right-adjacent_bits
Line 534:
1 010000000000100000000010000000010000000100000010000010000100010010
2 010000000000100000000010000000010000000100000010000010000100010010
3 010000000000100000000010000000010000000100000010000010000100010010</
{{out}}
<pre>
Line 575:
Note that both the string and mpz versions propagate any number of bits in a single pass, in other words explicitly iterating down all the input bits as opposed to implicitly setting all those bits n times, albeit the latter is probably a smidge faster.
===string===
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">str_srb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 603:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 623:
===mpz===
identical output
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 649:
<span style="color: #000080;font-style:italic;">--...</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"n = %d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mpz_srb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
<!--</
===hybrid===
Makes it even simpler, again identical output
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 673:
<span style="color: #000080;font-style:italic;">--...</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"n = %d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mpz_srb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
<!--</
=={{header|Python}}==
Line 680:
The <code>set_right_adjacent_bits</code> function does all the real work.
<
from functools import reduce
Line 715:
result = set_right_adjacent_bits(n, b)
print(f" Input b: {b:0{e}b}")
print(f" Result: {result:0{e}b}\n")</
{{out}}
Line 761:
The <code>set_right_adjacent_bits_list</code> function does all the real work.
<
Line 806:
result = set_right_adjacent_bits_list(n, b)
print(f" Input b: {_list2bin(b)}")
print(f" Result: {_list2bin(result)}\n")</
{{out}}
Line 851:
A left-to-right ordered collection of bits is more commonly referred to as an Integer in Raku.
<syntaxhighlight lang="raku"
my $m = $n;
$m +|= ($n +> $_) for ^ $b+1;
Line 877:
.say for ^$bits .map: -> $b { $integer.&lab($b).fmt("%{0~$bits+$integer.msb}b") };
}</
{{out}}
<pre>Powers of 2 ≤ 8, 0 - Right-adjacent-bits: 2
Line 947:
=={{header|Rust}}==
<
fn set_right_adjacent_bits<E: Clone + BitOrAssign + Shr<usize, Output = E>>(b: &mut E, n: usize) {
Line 995:
0b010000000000100000000010000000010000000100000010000010000100010010,
0b011110000000111100000011110000011110000111100011110011110111111111,
);</
{{out}}
<pre>
Line 1,026:
=={{header|Wren}}==
Using a list of 0's and 1's so we don't have to resort to BigInt.
<
if (e == 0 || n <= 0) return bits
var bits2 = bits.toList
Line 1,053:
bits = setRightBits.call(bits, e, n)
System.print(" Result: %(bits.join())\n")
}</
{{out}}
|