Set right-adjacent bits: Difference between revisions

m
syntax highlighting fixup automation
(Added AutoHotkey)
m (syntax highlighting fixup automation)
Line 53:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Set_Right_Bits is
Line 127:
Show ("010000000000100000000010000000010000000100000010000010000100010010", 2);
Show ("010000000000100000000010000000010000000100000010000010000100010010", 3);
end Set_Right_Bits;</langsyntaxhighlight>
{{out}}
<pre>
Line 164:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">setRight(num, n){
x := StrSplit(num)
for i, v in StrSplit(num)
Line 175:
res .= v
return res
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">test1 := [
(join,
"1000"
Line 199:
 
MsgBox % result
return</langsyntaxhighlight>
{{out}}
<pre>n=2; Width e = 4:
Line 234:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{out}}
<pre>
Line 268:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.parser math.ranges
sequences ;
 
Line 279:
 
{ 0b1000 0b0100 0b0010 0b0000 } [ 2 4 show nl ] each
0x10020080404082112 4 <iota> [ 66 show nl ] with each</langsyntaxhighlight>
{{out}}
<pre>
Line 317:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 371:
fmt.Println(" Result :", sb.String())
}
}</langsyntaxhighlight>
 
{{out}}
Line 405:
Implementation:
 
<langsyntaxhighlight Jlang="j">smearright=: {{ +./ (-i.1+x) |.!.0"0 1/ y }}</langsyntaxhighlight>
 
Here, we use J's bit array structure, so <tt>e</tt> is implicit in the length of the list.
Line 411:
Task examples:
 
<langsyntaxhighlight Jlang="j">b=: '1'&= :.(' '-.~":)
task=: {{y,:x&smearright&.:b y}}
 
Line 425:
3 task '010000000000100000000010000000010000000100000010000010000100010010'
010000000000100000000010000000010000000100000010000010000100010010
011110000000111100000011110000011110000111100011110011110111111111</langsyntaxhighlight>
 
<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}}==
<langsyntaxhighlight lang="julia">function setrightadj(s, n)
if n < 1
return s
Line 454:
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 2)
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 3)
</langsyntaxhighlight>{{out}}
<pre>
setrightadj("1000", 2) = "1110"
Line 471:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ShowSetRightBits]
ShowSetRightBits[b_String,n_Integer]:=Module[{poss,chars},
chars=Characters[b];
Line 485:
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",1]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",2]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",3]</langsyntaxhighlight>
{{out}}
<pre>In : 1000
Line 512:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Set_right-adjacent_bits
Line 534:
1 010000000000100000000010000000010000000100000010000010000100010010
2 010000000000100000000010000000010000000100000010000010000100010010
3 010000000000100000000010000000010000000100000010000010000100010010</langsyntaxhighlight>
{{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===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 623:
===mpz===
identical output
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
===hybrid===
Makes it even simpler, again identical output
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|Python}}==
Line 680:
The <code>set_right_adjacent_bits</code> function does all the real work.
 
<langsyntaxhighlight lang="python">from operator import or_
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")</langsyntaxhighlight>
 
{{out}}
Line 761:
The <code>set_right_adjacent_bits_list</code> function does all the real work.
 
<langsyntaxhighlight lang="python">from typing import List
 
 
Line 806:
result = set_right_adjacent_bits_list(n, b)
print(f" Input b: {_list2bin(b)}")
print(f" Result: {_list2bin(result)}\n")</langsyntaxhighlight>
 
{{out}}
Line 851:
A left-to-right ordered collection of bits is more commonly referred to as an Integer in Raku.
 
<syntaxhighlight lang="raku" perl6line>sub rab (Int $n, Int $b = 1) {
my $m = $n;
$m +|= ($n +> $_) for ^ $b+1;
Line 877:
.say for ^$bits .map: -> $b { $integer.&lab($b).fmt("%{0~$bits+$integer.msb}b") };
 
}</langsyntaxhighlight>
{{out}}
<pre>Powers of 2 ≤ 8, 0 - Right-adjacent-bits: 2
Line 947:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::ops::{BitOrAssign, Shr};
 
fn set_right_adjacent_bits<E: Clone + BitOrAssign + Shr<usize, Output = E>>(b: &mut E, n: usize) {
Line 995:
0b010000000000100000000010000000010000000100000010000010000100010010,
0b011110000000111100000011110000011110000111100011110011110111111111,
);</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="ecmascript">var setRightBits = Fn.new { |bits, e, n|
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")
}</langsyntaxhighlight>
 
{{out}}
10,333

edits