Set right-adjacent bits: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 53: | Line 53: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Set_Right_Bits is |
procedure Set_Right_Bits is |
||
Line 127: | Line 127: | ||
Show ("010000000000100000000010000000010000000100000010000010000100010010", 2); |
Show ("010000000000100000000010000000010000000100000010000010000100010010", 2); |
||
Show ("010000000000100000000010000000010000000100000010000010000100010010", 3); |
Show ("010000000000100000000010000000010000000100000010000010000100010010", 3); |
||
end Set_Right_Bits;</ |
end Set_Right_Bits;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 164: | Line 164: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">setRight(num, n){ |
||
x := StrSplit(num) |
x := StrSplit(num) |
||
for i, v in StrSplit(num) |
for i, v in StrSplit(num) |
||
Line 175: | Line 175: | ||
res .= v |
res .= v |
||
return res |
return res |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">test1 := [ |
||
(join, |
(join, |
||
"1000" |
"1000" |
||
Line 199: | Line 199: | ||
MsgBox % result |
MsgBox % result |
||
return</ |
return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>n=2; Width e = 4: |
<pre>n=2; Width e = 4: |
||
Line 234: | Line 234: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Set right-adjacent bits. Nigel Galloway: December 21st., 2021 |
// 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 |
let fN g l=let rec fG n g=[|match n,g with ('0'::t,0)->yield '0'; yield! fG t 0 |
||
Line 243: | 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)) |
[("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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 268: | Line 268: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: formatting io kernel math math.parser math.ranges |
||
sequences ; |
sequences ; |
||
Line 279: | Line 279: | ||
{ 0b1000 0b0100 0b0010 0b0000 } [ 2 4 show nl ] each |
{ 0b1000 0b0100 0b0010 0b0000 } [ 2 4 show nl ] each |
||
0x10020080404082112 4 <iota> [ 66 show nl ] with each</ |
0x10020080404082112 4 <iota> [ 66 show nl ] with each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 317: | Line 317: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 371: | Line 371: | ||
fmt.Println(" Result :", sb.String()) |
fmt.Println(" Result :", sb.String()) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 405: | Line 405: | ||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="j">smearright=: {{ +./ (-i.1+x) |.!.0"0 1/ y }}</syntaxhighlight> |
||
Here, we use J's bit array structure, so <tt>e</tt> is implicit in the length of the list. |
Here, we use J's bit array structure, so <tt>e</tt> is implicit in the length of the list. |
||
Line 411: | Line 411: | ||
Task examples: |
Task examples: |
||
< |
<syntaxhighlight lang="j">b=: '1'&= :.(' '-.~":) |
||
task=: {{y,:x&smearright&.:b y}} |
task=: {{y,:x&smearright&.:b y}} |
||
Line 425: | Line 425: | ||
3 task '010000000000100000000010000000010000000100000010000010000100010010' |
3 task '010000000000100000000010000000010000000100000010000010000100010010' |
||
010000000000100000000010000000010000000100000010000010000100010010 |
010000000000100000000010000000010000000100000010000010000100010010 |
||
011110000000111100000011110000011110000111100011110011110111111111</ |
011110000000111100000011110000011110000111100011110011110111111111</syntaxhighlight> |
||
<code>b</code> converts from character list to bit list (and its obverse converts back to character list, for easy viewing). |
<code>b</code> converts from character list to bit list (and its obverse converts back to character list, for easy viewing). |
||
Line 431: | Line 431: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">function setrightadj(s, n) |
||
if n < 1 |
if n < 1 |
||
return s |
return s |
||
Line 454: | Line 454: | ||
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 2) |
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 2) |
||
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 3) |
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 3) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
setrightadj("1000", 2) = "1110" |
setrightadj("1000", 2) = "1110" |
||
Line 471: | Line 471: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[ShowSetRightBits] |
||
ShowSetRightBits[b_String,n_Integer]:=Module[{poss,chars}, |
ShowSetRightBits[b_String,n_Integer]:=Module[{poss,chars}, |
||
chars=Characters[b]; |
chars=Characters[b]; |
||
Line 485: | Line 485: | ||
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",1] |
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",1] |
||
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",2] |
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",2] |
||
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",3]</ |
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",3]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>In : 1000 |
<pre>In : 1000 |
||
Line 512: | Line 512: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Set_right-adjacent_bits |
use strict; # https://rosettacode.org/wiki/Set_right-adjacent_bits |
||
Line 534: | Line 534: | ||
1 010000000000100000000010000000010000000100000010000010000100010010 |
1 010000000000100000000010000000010000000100000010000010000100010010 |
||
2 010000000000100000000010000000010000000100000010000010000100010010 |
2 010000000000100000000010000000010000000100000010000010000100010010 |
||
3 010000000000100000000010000000010000000100000010000010000100010010</ |
3 010000000000100000000010000000010000000100000010000010000100010010</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 575: | 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. |
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=== |
===string=== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<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: | 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> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 623: | Line 623: | ||
===mpz=== |
===mpz=== |
||
identical output |
identical output |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<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: | Line 649: | ||
<span style="color: #000080;font-style:italic;">--...</span> |
<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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
===hybrid=== |
===hybrid=== |
||
Makes it even simpler, again identical output |
Makes it even simpler, again identical output |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<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: | Line 673: | ||
<span style="color: #000080;font-style:italic;">--...</span> |
<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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 680: | Line 680: | ||
The <code>set_right_adjacent_bits</code> function does all the real work. |
The <code>set_right_adjacent_bits</code> function does all the real work. |
||
< |
<syntaxhighlight lang="python">from operator import or_ |
||
from functools import reduce |
from functools import reduce |
||
Line 715: | Line 715: | ||
result = set_right_adjacent_bits(n, b) |
result = set_right_adjacent_bits(n, b) |
||
print(f" Input b: {b:0{e}b}") |
print(f" Input b: {b:0{e}b}") |
||
print(f" Result: {result:0{e}b}\n")</ |
print(f" Result: {result:0{e}b}\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 761: | Line 761: | ||
The <code>set_right_adjacent_bits_list</code> function does all the real work. |
The <code>set_right_adjacent_bits_list</code> function does all the real work. |
||
< |
<syntaxhighlight lang="python">from typing import List |
||
Line 806: | Line 806: | ||
result = set_right_adjacent_bits_list(n, b) |
result = set_right_adjacent_bits_list(n, b) |
||
print(f" Input b: {_list2bin(b)}") |
print(f" Input b: {_list2bin(b)}") |
||
print(f" Result: {_list2bin(result)}\n")</ |
print(f" Result: {_list2bin(result)}\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 851: | Line 851: | ||
A left-to-right ordered collection of bits is more commonly referred to as an Integer in Raku. |
A left-to-right ordered collection of bits is more commonly referred to as an Integer in Raku. |
||
<lang |
<syntaxhighlight lang="raku" line>sub rab (Int $n, Int $b = 1) { |
||
my $m = $n; |
my $m = $n; |
||
$m +|= ($n +> $_) for ^ $b+1; |
$m +|= ($n +> $_) for ^ $b+1; |
||
Line 877: | Line 877: | ||
.say for ^$bits .map: -> $b { $integer.&lab($b).fmt("%{0~$bits+$integer.msb}b") }; |
.say for ^$bits .map: -> $b { $integer.&lab($b).fmt("%{0~$bits+$integer.msb}b") }; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Powers of 2 ≤ 8, 0 - Right-adjacent-bits: 2 |
<pre>Powers of 2 ≤ 8, 0 - Right-adjacent-bits: 2 |
||
Line 947: | Line 947: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">use std::ops::{BitOrAssign, Shr}; |
||
fn set_right_adjacent_bits<E: Clone + BitOrAssign + Shr<usize, Output = E>>(b: &mut E, n: usize) { |
fn set_right_adjacent_bits<E: Clone + BitOrAssign + Shr<usize, Output = E>>(b: &mut E, n: usize) { |
||
Line 995: | Line 995: | ||
0b010000000000100000000010000000010000000100000010000010000100010010, |
0b010000000000100000000010000000010000000100000010000010000100010010, |
||
0b011110000000111100000011110000011110000111100011110011110111111111, |
0b011110000000111100000011110000011110000111100011110011110111111111, |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,026: | Line 1,026: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Using a list of 0's and 1's so we don't have to resort to BigInt. |
Using a list of 0's and 1's so we don't have to resort to BigInt. |
||
< |
<syntaxhighlight lang="ecmascript">var setRightBits = Fn.new { |bits, e, n| |
||
if (e == 0 || n <= 0) return bits |
if (e == 0 || n <= 0) return bits |
||
var bits2 = bits.toList |
var bits2 = bits.toList |
||
Line 1,053: | Line 1,053: | ||
bits = setRightBits.call(bits, e, n) |
bits = setRightBits.call(bits, e, n) |
||
System.print(" Result: %(bits.join())\n") |
System.print(" Result: %(bits.join())\n") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |