Set right-adjacent bits: Difference between revisions

Added PureBasic
m (→‎{{header|Wren}}: Changed a variable.)
(Added PureBasic)
 
(41 intermediate revisions by 23 users not shown)
Line 1:
{{draft task}}
 
Given a left-to-right ordered collection of <code>e</code> bits, <code>b</code>, where <code>1 <= e <= 10000</code>,
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|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F set_right_adjacent_bits_list(Int n, [Int] b) -> [Int]
R (0 .< b.len).map(i -> Int(any(@b[max(0, i - @n) .. i])))
 
F _list2bin([Int] b) -> String
R b.map(x -> String(x)).join(‘’)
 
F _to_list(String bits) -> [Int]
R bits.map(char -> Int(char))
 
print("SAME n & Width.\n")
V n = 2
V bits_s = ‘1000 0100 0010 0000’
V first = 1B
L(b_str) bits_s.split(‘ ’)
V b = _to_list(b_str)
V e = b_str.len
I first
first = 0B
print(‘n = ’n‘; Width e = ’e":\n")
V result = set_right_adjacent_bits_list(n, b)
print(‘ Input b: ’_list2bin(b))
print(‘ Result: ’_list2bin(result)"\n")
 
print("SAME Input & Width.\n")
bits_s = ‘01’(10.<0).step(-1).map(x -> ‘0’ * x).join(‘1’)
L(n) 4
first = 1B
L(b_str) bits_s.split(‘ ’)
V b = _to_list(b_str)
V e = b_str.len
I first
first = 0B
print(‘n = ’n‘; Width e = ’e":\n")
V result = set_right_adjacent_bits_list(n, b)
print(‘ Input b: ’_list2bin(b))
print(‘ Result: ’_list2bin(result)"\n")</syntaxhighlight>
 
{{out}}
<pre>
SAME n & Width.
 
n = 2; Width e = 4:
 
Input b: 1000
Result: 1110
 
Input b: 0100
Result: 0111
 
Input b: 0010
Result: 0011
 
Input b: 0000
Result: 0000
 
SAME Input & Width.
 
n = 0; Width e = 66:
 
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result: 010000000000100000000010000000010000000100000010000010000100010010
 
n = 1; Width e = 66:
 
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result: 011000000000110000000011000000011000000110000011000011000110011011
 
n = 2; Width e = 66:
 
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result: 011100000000111000000011100000011100000111000011100011100111011111
 
n = 3; Width e = 66:
 
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result: 011110000000111100000011110000011110000111100011110011110111111111
 
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO;
 
procedure Set_Right_Bits is
 
type Bit_Number is new Positive range 1 .. 10_000;
type Bit is new Boolean;
type Bit_Collection is array (Bit_Number range <>) of Bit
with Pack;
 
function Right_Adjacent (B : Bit_Collection;
N : Natural) return Bit_Collection
is
Result : Bit_Collection := B;
Mask : Bit_Collection := B;
begin
for A in 1 .. N loop
Mask := False & Mask (Mask'First .. Mask'Last - 1);
-- Shift Mask by appending False/0 in front of slice.
 
Result := Result or Mask;
end loop;
return Result;
end Right_Adjacent;
 
procedure Put (Collection : Bit_Collection) is
use Ada.Text_IO;
begin
for Bit of Collection loop
Put ((if Bit then '1' else '0'));
end loop;
end Put;
 
function Value (Item : String) return Bit_Collection
is
Length : constant Bit_Number := Item'Length;
Result : Bit_Collection (1 .. Length);
Index : Natural := Item'First;
begin
for R of Result loop
R := (case Item (Index) is
when '0' | 'F' | 'f' => False,
when '1' | 'T' | 't' => True,
when others =>
raise Constraint_Error with "invalid input");
Index := Index + 1;
end loop;
return Result;
end Value;
 
procedure Show (Bit_String : String; N : Natural)
is
B : constant Bit_Collection := Value (Bit_String);
R : constant Bit_Collection := Right_Adjacent (B, N);
Prefix : constant String := " ";
use Ada.Text_IO;
begin
Put ("n ="); Put (N'Image);
Put ("; Width e ="); Put (Bit_String'Length'Image);
Put (":"); New_Line;
Put (Prefix); Put ("Input B: "); Put (B); New_Line;
Put (Prefix); Put ("Result : "); Put (R); New_Line;
New_Line;
end Show;
 
begin
Show ("1000", 2);
Show ("0100", 2);
Show ("0010", 2);
Show ("0000", 2);
 
Show ("010000000000100000000010000000010000000100000010000010000100010010", 0);
Show ("010000000000100000000010000000010000000100000010000010000100010010", 1);
Show ("010000000000100000000010000000010000000100000010000010000100010010", 2);
Show ("010000000000100000000010000000010000000100000010000010000100010010", 3);
end Set_Right_Bits;</syntaxhighlight>
{{out}}
<pre>
n = 2; Width e = 4:
Input B: 1000
Result : 1110
 
n = 2; Width e = 4:
Input B: 0100
Result : 0111
 
n = 2; Width e = 4:
Input B: 0010
Result : 0011
 
n = 2; Width e = 4:
Input B: 0000
Result : 0000
 
n = 0; Width e = 66:
Input B: 010000000000100000000010000000010000000100000010000010000100010010
Result : 010000000000100000000010000000010000000100000010000010000100010010
 
n = 1; Width e = 66:
Input B: 010000000000100000000010000000010000000100000010000010000100010010
Result : 011000000000110000000011000000011000000110000011000011000110011011
 
n = 2; Width e = 66:
Input B: 010000000000100000000010000000010000000100000010000010000100010010
Result : 011100000000111000000011100000011100000111000011100011100111011111
 
n = 3; Width e = 66:
Input B: 010000000000100000000010000000010000000100000010000010000100010010
Result : 011110000000111100000011110000011110000111100011110011110111111111
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">setBits: function [inp, n][
e: size inp
print "n = " ++ (to :string n) ++ "; Width e = " ++ (to :string e) ++ ":"
 
result: new ø
if? or? zero? e n =< 0 -> result: inp
else [
result: split inp
loop 0..e-1 'i [
if inp\[i] = `1` [
j: i + 1
while [and? j =< i+n j < e][
result\[j]: `1`
j: j + 1
]
]
]
result: join result
]
 
print ["\tinput :" inp]
print ["\tresult :" result]
]
 
setBits "1000" 2
setBits "0100" 2
setBits "0010" 2
setBits "0000" 2
 
setBits "010000000000100000000010000000010000000100000010000010000100010010" 0
setBits "010000000000100000000010000000010000000100000010000010000100010010" 1
setBits "010000000000100000000010000000010000000100000010000010000100010010" 2
setBits "010000000000100000000010000000010000000100000010000010000100010010" 3</syntaxhighlight>
 
{{out}}
 
<pre>n = 2; Width e = 4:
input : 1000
result : 1110
n = 2; Width e = 4:
input : 0100
result : 0111
n = 2; Width e = 4:
input : 0010
result : 0011
n = 2; Width e = 4:
input : 0000
result : 0000
n = 0; Width e = 66:
input : 010000000000100000000010000000010000000100000010000010000100010010
result : 010000000000100000000010000000010000000100000010000010000100010010
n = 1; Width e = 66:
input : 010000000000100000000010000000010000000100000010000010000100010010
result : 011000000000110000000011000000011000000110000011000011000110011011
n = 2; Width e = 66:
input : 010000000000100000000010000000010000000100000010000010000100010010
result : 011100000000111000000011100000011100000111000011100011100111011111
n = 3; Width e = 66:
input : 010000000000100000000010000000010000000100000010000010000100010010
result : 011110000000111100000011110000011110000111100011110011110111111111</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">setRight(num, n){
x := StrSplit(num)
for i, v in StrSplit(num)
if v
loop, % n
x[i+A_Index] := 1
Loop % n
x.removeAt(StrLen(num)+1)
for i, v in x
res .= v
return res
}</syntaxhighlight>
Examples:<syntaxhighlight lang="autohotkey">test1 := [
(join,
"1000"
"0100"
"0010"
"0000"
)]
 
test2 := [
(join,
"010000000000100000000010000000010000000100000010000010000100010010"
"010000000000100000000010000000010000000100000010000010000100010010"
"010000000000100000000010000000010000000100000010000010000100010010"
"010000000000100000000010000000010000000100000010000010000100010010"
)]
 
for i, num in test1
result .= "n=2; Width e = 4:`nInput :`t" num "`nResult :`t" setRight(num, 2) "`n`n"
 
for i, num in test2
result .= "n=" i-1 "; Width e = 66:`nInput :`t" num "`nResult :`t" setRight(num, i-1) "`n`n"
 
MsgBox % result
return</syntaxhighlight>
{{out}}
<pre>n=2; Width e = 4:
Input : 1000
Result : 1110
 
n=2; Width e = 4:
Input : 0100
Result : 0111
 
n=2; Width e = 4:
Input : 0010
Result : 0011
 
n=2; Width e = 4:
Input : 0000
Result : 0000
 
n=0; Width e = 66:
Input : 010000000000100000000010000000010000000100000010000010000100010010
Result : 010000000000100000000010000000010000000100000010000010000100010010
 
n=1; Width e = 66:
Input : 010000000000100000000010000000010000000100000010000010000100010010
Result : 011000000000110000000011000000011000000110000011000011000110011011
 
n=2; Width e = 66:
Input : 010000000000100000000010000000010000000100000010000010000100010010
Result : 011100000000111000000011100000011100000111000011100011100111011111
 
n=3; Width e = 66:
Input : 010000000000100000000010000000010000000100000010000010000100010010
Result : 011110000000111100000011110000011110000111100011110011110111111111</pre>
 
=={{header|BASIC}}==
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">Global Dim bits.i(1)
 
Procedure setRightBits(Array bits(1), e, n)
Protected.i i, j
If e = 0 Or n <= 0: ProcedureReturn: EndIf
Dim bits2(e)
For i = 0 To e - 1 : bits2(i) = bits(i) : Next
For i = 0 To e - 2
If bits(i) = 1
j = i + 1
While j <= i + n And j < e
bits2(j) = 1
j + 1
Wend
EndIf
Next i
For i = 0 To e - 1 : bits(i) = bits2(i) : Next
EndProcedure
 
OpenConsole()
Define.i i, k, ub, n
Define b.s = "010000000000100000000010000000010000000100000010000010000100010010"
Dim tests.s(8, 2)
tests(0, 0) = "1000": tests(0, 1) = "2"
tests(1, 0) = "0100": tests(1, 1) = "2"
tests(2, 0) = "0010": tests(2, 1) = "2"
tests(3, 0) = "0000": tests(3, 1) = "2"
tests(4, 0) = b: tests(4, 1) = "0"
tests(5, 0) = b: tests(5, 1) = "1"
tests(6, 0) = b: tests(6, 1) = "2"
tests(7, 0) = b: tests(7, 1) = "3"
 
For k = 0 To 7
ReDim bits(Len(tests(k, 0)))
For i = 0 To Len(tests(k, 0)) - 1
bits(i) = Val(Mid(tests(k, 0), i + 1, 1))
Next i
ub = ArraySize(bits())
n = Val(tests(k, 1))
PrintN("n = " + Str(n) + "; Width e = " + Str(ub))
Print(" Input b: " + tests(k, 0))
setRightBits(bits(), ub, n)
PrintN("")
Print(" Result: ")
For i = 0 To ub - 1
Print(Str(bits(i)));
Next i
PrintN(Chr(10))
Next k
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|QBasic}}===
{{trans|FreeBASIC}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">SUB setRightBits (bits(), e, n)
IF e = 0 OR n <= 0 THEN EXIT SUB
DIM bits2(1 TO e)
FOR i = 1 TO e: bits2(i) = bits(i): NEXT
FOR i = 1 TO e - 1
IF bits(i) = 1 THEN
j = i + 1
WHILE j <= i + n AND j <= e
bits2(j) = 1
j = j + 1
WEND
END IF
NEXT i
FOR i = 1 TO e: bits(i) = bits2(i): NEXT
END SUB
 
b$ = "010000000000100000000010000000010000000100000010000010000100010010"
DIM tests$(8, 2)
tests$(1, 1) = "1000": tests$(1, 2) = "2"
tests$(2, 1) = "0100": tests$(2, 2) = "2"
tests$(3, 1) = "0010": tests$(3, 2) = "2"
tests$(4, 1) = "0000": tests$(4, 2) = "2"
tests$(5, 1) = b$: tests$(5, 2) = "0"
tests$(6, 1) = b$: tests$(6, 2) = "1"
tests$(7, 1) = b$: tests$(7, 2) = "2"
tests$(8, 1) = b$: tests$(8, 2) = "3"
FOR k = 1 TO 8
REDIM bits(1 TO LEN(tests$(k, 1)))
FOR i = 1 TO LEN(tests$(k, 1))
bits(i) = VAL(MID$(tests$(k, 1), i, 1))
NEXT i
ub = UBOUND(bits)
n = VAL(tests$(k, 2))
PRINT USING "n = #; Width e = ##:"; n; ub
PRINT " Input b: "; tests$(k, 1)
CALL setRightBits(bits(), ub, n)
PRINT " Result:";
FOR i = 1 TO ub
PRINT bits(i);
NEXT i
PRINT CHR$(10)
NEXT k
END</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">SUB setrightbits (bits(),e,n)
IF e = 0 OR n <= 0 THEN EXIT SUB
DIM bits2(0)
MAT REDIM bits2(1 TO e)
FOR i = 1 TO e
LET bits2(i) = bits(i)
NEXT i
FOR i = 1 TO e-1
IF bits(i) = 1 THEN
LET j = i+1
DO WHILE j <= i+n AND j <= e
LET bits2(j) = 1
LET j = j+1
LOOP
END IF
NEXT i
FOR i = 1 TO e
LET bits(i) = bits2(i)
NEXT i
END SUB
 
 
LET b$ = "010000000000100000000010000000010000000100000010000010000100010010"
DIM tests$(8, 2)
LET tests$(1, 1) = "1000"
LET tests$(1, 2) = "2"
LET tests$(2, 1) = "0100"
LET tests$(2, 2) = "2"
LET tests$(3, 1) = "0010"
LET tests$(3, 2) = "2"
LET tests$(4, 1) = "0000"
LET tests$(4, 2) = "2"
LET tests$(5, 1) = b$
LET tests$(5, 2) = "0"
LET tests$(6, 1) = b$
LET tests$(6, 2) = "1"
LET tests$(7, 1) = b$
LET tests$(7, 2) = "2"
LET tests$(8, 1) = b$
LET tests$(8, 2) = "3"
FOR k = 1 TO 8
DIM bits(1)
MAT REDIM bits(1 TO LEN(tests$(k, 1)))
FOR i = 1 TO LEN(tests$(k, 1))
LET bits(i) = VAL((tests$(k, 1))[i:i+1-1])
NEXT i
LET ub = UBOUND(bits)
LET n = VAL(tests$(k, 2))
PRINT USING "n = #; Width e = ##:": n, ub
PRINT " Input b: "; tests$(k, 1)
CALL setrightbits (bits(), ub, n)
PRINT " Result: ";
FOR i = 1 TO ub
PRINT STR$(bits(i));
NEXT i
PRINT
PRINT
NEXT k
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
 
#include <iostream>
#include <string>
 
void setRightAdjacent(std::string text, int32_t number) {
std::cout << "n = " << number << ", Width = " << text.size() << ", Input: " << text << std::endl;
 
std::string result = text;
for ( uint32_t i = 0; i < result.size(); i++ ) {
if ( text[i] == '1' ) {
for ( uint32_t j = i + 1; j <= i + number && j < result.size(); j++ ) {
result[j] = '1';
}
}
}
 
std::cout << std::string(16 + std::to_string(text.size()).size(), ' ') << "Result: " + result << "\n" << std::endl;
}
 
int main() {
setRightAdjacent("1000", 2);
setRightAdjacent("0100", 2);
setRightAdjacent("0010", 2);
setRightAdjacent("0000", 2);
 
std::string test = "010000000000100000000010000000010000000100000010000010000100010010";
setRightAdjacent(test, 0);
setRightAdjacent(test, 1);
setRightAdjacent(test, 2);
setRightAdjacent(test, 3);
}
 
</syntaxhighlight>
{{ out }}
<pre>
n = 2, Width = 4, Input: 1000
Result: 1110
 
n = 2, Width = 4, Input: 0100
Result: 0111
 
n = 2, Width = 4, Input: 0010
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|EasyLang}}==
{{trans|Java}}
<syntaxhighlight>
proc adjacent txt$ n . .
print "n = " & n & ", width = " & len txt$
print "input: " & txt$
txt$[] = strchars txt$
res$[] = txt$[]
for i to len res$[]
if txt$[i] = "1"
for j = i + 1 to lower (i + n) len res$[]
res$[j] = "1"
.
.
.
res$ = strjoin res$[]
print "result: " & res$
print ""
.
adjacent "1000" 2
adjacent "0100" 2
adjacent "0010" 2
adjacent "0000" 2
test$ = "010000000000100000000010000000010000000100000010000010000100010010"
adjacent test$ 0
adjacent test$ 1
adjacent test$ 2
adjacent test$ 3
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight 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
|('0'::t,n)->yield '1'; yield! fG t (n-1)
|(_::t,_) ->yield '1'; yield! fG t l
|_ ->()|]
fG (g|>List.ofSeq) 0|>System.String
 
[("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>
1000
1110
0100
0111
0010
0011
0001
0001
0000
0000
010000000000100000000010000000010000000100000010000010000100010010
010000000000100000000010000000010000000100000010000010000100010010
010000000000100000000010000000010000000100000010000010000100010010
011000000000110000000011000000011000000110000011000011000110011011
010000000000100000000010000000010000000100000010000010000100010010
011100000000111000000011100000011100000111000011100011100111011111
010000000000100000000010000000010000000100000010000010000100010010
011110000000111100000011110000011110000111100011110011110111111111
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<syntaxhighlight lang="factor">USING: formatting io kernel math math.parser math.ranges
sequences ;
 
: set-rab ( n b -- result )
[0,b] [ neg shift ] with [ bitor ] map-reduce ;
 
:: show ( n b e -- )
b e "n = %d; width = %d\n" printf
n n b set-rab [ >bin e CHAR: 0 pad-head print ] bi@ ;
 
{ 0b1000 0b0100 0b0010 0b0000 } [ 2 4 show nl ] each
0x10020080404082112 4 <iota> [ 66 show nl ] with each</syntaxhighlight>
{{out}}
<pre>
n = 2; width = 4
1000
1110
 
n = 2; width = 4
0100
0111
 
n = 2; width = 4
0010
0011
 
n = 2; width = 4
0000
0000
 
n = 0; width = 66
010000000000100000000010000000010000000100000010000010000100010010
010000000000100000000010000000010000000100000010000010000100010010
 
n = 1; width = 66
010000000000100000000010000000010000000100000010000010000100010010
011000000000110000000011000000011000000110000011000011000110011011
 
n = 2; width = 66
010000000000100000000010000000010000000100000010000010000100010010
011100000000111000000011100000011100000111000011100011100111011111
 
n = 3; width = 66
010000000000100000000010000000010000000100000010000010000100010010
011110000000111100000011110000011110000111100011110011110111111111
</pre>
 
=={{header|FreeBASIC}}==
{{trans|Wren}}
<syntaxhighlight lang="vbnet">Sub setRightBits(bits() As Integer, e As Integer, n As Integer)
Dim As Integer i, j
If e = 0 Or n <= 0 Then Exit Sub
Dim bits2(1 To e) As Integer
For i = 1 To e : bits2(i) = bits(i) : Next
For i = 1 To e - 1
If bits(i) = 1 Then
j = i + 1
While j <= i + n And j <= e
bits2(j) = 1
j += 1
Wend
End If
Next i
For i = 1 To e : bits(i) = bits2(i) : Next
End Sub
 
Dim As Integer i, k, ub, n
Dim As String b = "010000000000100000000010000000010000000100000010000010000100010010"
Dim tests(8, 2) As String
tests(1, 1) = "1000": tests(1, 2) = "2"
tests(2, 1) = "0100": tests(2, 2) = "2"
tests(3, 1) = "0010": tests(3, 2) = "2"
tests(4, 1) = "0000": tests(4, 2) = "2"
tests(5, 1) = b: tests(5, 2) = "0"
tests(6, 1) = b: tests(6, 2) = "1"
tests(7, 1) = b: tests(7, 2) = "2"
tests(8, 1) = b: tests(8, 2) = "3"
For k = 1 To 8
Dim bits(1 To Len(tests(k, 1))) As Integer
For i = 1 To Len(tests(k, 1))
bits(i) = Val(Mid(tests(k, 1), i, 1))
Next i
ub = Ubound(bits)
n = Val(tests(k, 2))
Print Using "n = #; Width e = ##:"; n; ub
Print " Input b: "; tests(k, 1)
setRightBits(bits(), ub, n)
Print " Result: ";
For i = 1 To ub
Print Str(bits(i));
Next i
Print Chr(10)
Next k
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Wren entry.</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"strings"
)
 
type test struct {
bs string
n int
}
 
func setRightBits(bits []byte, e, n int) []byte {
if e == 0 || n <= 0 {
return bits
}
bits2 := make([]byte, len(bits))
copy(bits2, bits)
for i := 0; i < e-1; i++ {
c := bits[i]
if c == 1 {
j := i + 1
for j <= i+n && j < e {
bits2[j] = 1
j++
}
}
}
return bits2
}
 
func main() {
b := "010000000000100000000010000000010000000100000010000010000100010010"
tests := []test{
test{"1000", 2}, test{"0100", 2}, test{"0010", 2}, test{"0000", 2},
test{b, 0}, test{b, 1}, test{b, 2}, test{b, 3},
}
for _, test := range tests {
bs := test.bs
e := len(bs)
n := test.n
fmt.Println("n =", n, "\b; Width e =", e, "\b:")
fmt.Println(" Input b:", bs)
bits := []byte(bs)
for i := 0; i < len(bits); i++ {
bits[i] = bits[i] - '0'
}
bits = setRightBits(bits, e, n)
var sb strings.Builder
for i := 0; i < len(bits); i++ {
sb.WriteByte(bits[i] + '0')
}
fmt.Println(" Result :", sb.String())
}
}</syntaxhighlight>
 
{{out}}
<pre>
n = 2; Width e = 4:
Input b: 1000
Result : 1110
n = 2; Width e = 4:
Input b: 0100
Result : 0111
n = 2; Width e = 4:
Input b: 0010
Result : 0011
n = 2; Width e = 4:
Input b: 0000
Result : 0000
n = 0; Width e = 66:
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result : 010000000000100000000010000000010000000100000010000010000100010010
n = 1; Width e = 66:
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result : 011000000000110000000011000000011000000110000011000011000110011011
n = 2; Width e = 66:
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result : 011100000000111000000011100000011100000111000011100011100111011111
n = 3; Width e = 66:
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result : 011110000000111100000011110000011110000111100011110011110111111111
</pre>
 
=={{header|J}}==
 
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.
 
Task examples:
 
<syntaxhighlight lang="j">b=: '1'&= :.(' '-.~":)
task=: {{y,:x&smearright&.:b y}}
 
0 task '010000000000100000000010000000010000000100000010000010000100010010'
010000000000100000000010000000010000000100000010000010000100010010
010000000000100000000010000000010000000100000010000010000100010010
1 task '010000000000100000000010000000010000000100000010000010000100010010'
010000000000100000000010000000010000000100000010000010000100010010
011000000000110000000011000000011000000110000011000011000110011011
2 task '010000000000100000000010000000010000000100000010000010000100010010'
010000000000100000000010000000010000000100000010000010000100010010
011100000000111000000011100000011100000111000011100011100111011111
3 task '010000000000100000000010000000010000000100000010000010000100010010'
010000000000100000000010000000010000000100000010000010000100010010
011110000000111100000011110000011110000111100011110011110111111111</syntaxhighlight>
 
<code>b</code> converts from character list to bit list (and its obverse converts back to character list, for easy viewing).
<code>task</code> uses b on the right argument to <code>smearright</code> and its obverse on the result, which it provides with the original value (again, for easy viewing).
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
public final class SetRightAdjacentBits {
 
public static void main(String[] aArgs) {
setRightAdjacent("1000", 2);
setRightAdjacent("0100", 2);
setRightAdjacent("0010", 2);
setRightAdjacent("0000", 2);
String test = "010000000000100000000010000000010000000100000010000010000100010010";
setRightAdjacent(test, 0);
setRightAdjacent(test, 1);
setRightAdjacent(test, 2);
setRightAdjacent(test, 3);
}
private static void setRightAdjacent(String aText, int aNumber) {
System.out.println("n = " + aNumber + ", Width = " + aText.length() + ", Input: " + aText);
char[] text = aText.toCharArray();
char[] result = aText.toCharArray();
for ( int i = 0; i < result.length; i++ ) {
if ( text[i] == '1' ) {
for ( int j = i + 1; j <= i + aNumber && j < result.length; j++ ) {
result[j] = '1';
}
}
}
String spaces = " ".repeat(16 + String.valueOf(aText.length()).length());
System.out.println(spaces + "Result: " + new String(result) + System.lineSeparator());
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
n = 2, Width = 4, Input: 1000
Result: 1110
 
n = 2, Width = 4, Input: 0100
Result: 0111
 
n = 2, Width = 4, Input: 0010
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|Julia}}==
<syntaxhighlight lang="julia">function setrightadj(s, n)
if n < 1
return s
else
arr = reverse(collect(s))
for (i, c) in enumerate(reverse(s))
if c == '1'
arr[max(1, i - n):i] .= '1'
end
end
return String(reverse(arr))
end
end
 
@show setrightadj("1000", 2)
@show setrightadj("0100", 2)
@show setrightadj("0010", 2)
@show setrightadj("0000", 2)
 
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 0)
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 1)
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 2)
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 3)
</syntaxhighlight>{{out}}
<pre>
setrightadj("1000", 2) = "1110"
setrightadj("0100", 2) = "0111"
setrightadj("0010", 2) = "0011"
setrightadj("0000", 2) = "0000"
setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 0) =
"010000000000100000000010000000010000000100000010000010000100010010"
setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 1) =
"011000000000110000000011000000011000000110000011000011000110011011"
setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 2) =
"011100000000111000000011100000011100000111000011100011100111011111"
setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 3) =
"011110000000111100000011110000011110000111100011110011110111111111"
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[ShowSetRightBits]
ShowSetRightBits[b_String,n_Integer]:=Module[{poss,chars},
chars=Characters[b];
poss=Position[chars,"1"];
poss=Union[Flatten[Outer[Plus,poss,Range[n]]]];
{{"In :",b},{"Out:",StringJoin[ReplacePart[chars,(List/@poss)->"1"]]}}//Grid
]
ShowSetRightBits["1000",2]
ShowSetRightBits["0100",2]
ShowSetRightBits["0010",2]
ShowSetRightBits["0000",2]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",0]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",1]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",2]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",3]</syntaxhighlight>
{{out}}
<pre>In : 1000
Out: 1110
 
In : 0100
Out: 0111
 
In : 0010
Out: 0011
 
In : 0000
Out: 0000
 
In : 010000000000100000000010000000010000000100000010000010000100010010
Out: 010000000000100000000010000000010000000100000010000010000100010010
 
In : 010000000000100000000010000000010000000100000010000010000100010010
Out: 011000000000110000000011000000011000000110000011000011000110011011
 
In : 010000000000100000000010000000010000000100000010000010000100010010
Out: 011100000000111000000011100000011100000111000011100011100111011111
 
In : 010000000000100000000010000000010000000100000010000010000100010010
Out: 011110000000111100000011110000011110000111100011110011110111111111</pre>
 
=={{header|Nim}}==
We define a type<code>BitString</code> similar to a Nim sequence and provide some basic operations; creating, indexing, setting and clearing a bit, iterating, parsing a string to a bit string and displaying a bit string. After that, solving the task is done by a simple procedure.
<syntaxhighlight lang="Nim">import std/[bitops, strformat]
 
type
# Bit string described by a length and a sequence of bytes.
BitString = object
len: Natural
data: seq[byte]
# Position composed of a byte number and
# a bit number in the byte (starting from the left).
Position = tuple[bytenum, bitnum: int]
 
func toPosition(n: Natural): Position =
## Convert an index to a Position.
(n div 8, 7 - n mod 8)
 
proc newBitString*(len: Natural): BitString =
## Create a bit string of length "len".
result.len = len
result.data = newSeq[byte]((len + 7) div 8)
 
func checkIndex(bits: BitString; n: Natural) =
## Check that the index "n" is not out of bounds.
if n >= bits.len:
raise newException(RangeDefect, &"index out of range: {n}.")
 
proc `[]`*(bits: BitString; n: Natural): bool =
## Return the bit at index "n" (as a boolean).
bits.checkIndex(n)
let pos = n.toPosition
result = bits.data[pos.bytenum].testBit(pos.bitnum)
 
func setBit*(bits: var BitString; n: Natural) =
## Set the bit at index "n".
bits.checkIndex(n)
let pos = n.toPosition
bits.data[pos.bytenum].setBit(pos.bitnum)
 
func clearBit*(bits: var BitString; n: Natural) =
## Clear the bit at index "n".
## Not used but provided for consistency.
bits.checkIndex(n)
let pos = n.toPosition
bits.data[pos.bytenum].clearBit(pos.bitnum)
 
iterator items*(bits: BitString): bool =
## Yield the successive bits of the bit string.
for n in 0..<bits.len:
yield bits[n]
 
func toBitString*(s: string): BitString =
## Convert a string of '0' and '1' to a bit string.
result = newBitString(s.len)
for n, val in s:
if val == '1':
result.setBit(n)
elif val != '0':
raise newException(ValueError, &"invalid bit value: {val}")
 
func `$`*(bits: BitString): string =
## Return the string representation of a bit string.
const BinDigits = [false: '0', true: '1']
for bit in bits.items:
result.add BinDigits[bit]
 
func setAdjacentBitString(bits: BitString; n: Natural): BitString =
## Set the "n" bits adjacent to a set bit.
result = bits
for i in 0..<bits.len:
if bits[i]:
for j in (i + 1)..(i + n):
if j < bits.len:
result.setBit(j)
 
let n = 2
echo &"n = {n}; Width e = 4\n"
for input in ["1000", "0100", "0010", "0000"]:
echo &"Input: {input}"
echo &"Result: {input.toBitString.setAdjacentBitString(n)}"
echo()
 
echo()
const BS66 = "010000000000100000000010000000010000000100000010000010000100010010".toBitString
for n in 0..3:
echo &"n = {n}; Width e = {BS66.len}\n"
echo "Input:"
echo BS66
echo "Result:"
echo BS66.setAdjacentBitString(n)
echo()
</syntaxhighlight>
 
{{out}}
<pre>n = 2; Width e = 4
 
Input: 1000
Result: 1110
 
Input: 0100
Result: 0111
 
Input: 0010
Result: 0011
 
Input: 0000
Result: 0000
 
 
n = 0; Width e = 66
 
Input:
010000000000100000000010000000010000000100000010000010000100010010
Result:
010000000000100000000010000000010000000100000010000010000100010010
 
n = 1; Width e = 66
 
Input:
010000000000100000000010000000010000000100000010000010000100010010
Result:
011000000000110000000011000000011000000110000011000011000110011011
 
n = 2; Width e = 66
 
Input:
010000000000100000000010000000010000000100000010000010000100010010
Result:
011100000000111000000011100000011100000111000011100011100111011111
 
n = 3; Width e = 66
 
Input:
010000000000100000000010000000010000000100000010000010000100010010
Result:
011110000000111100000011110000011110000111100011110011110111111111
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'bitwise';
 
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</syntaxhighlight>
{{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|Phix}}==
The basic idea is to create a mask of n 1s on the right and either shift that left or (a copy of) the input right, and use it to determine which bits of the result need to be set, in addition to the originals. The string version actually uses a count of 1s instead of an actual mask, and obviously the odd() and even() functions work just as well on the characters '0' and '1' as they do the numbers/bits 0 and 1.
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===
<!--<syntaxhighlight lang="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>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">input</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_eq</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">m</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">n</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">bit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]):</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">bit</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span> <span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #000000;">bit</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"1000"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"0100"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"0010"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"0000"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"010000000000100000000010000000010000000100000010000010000100010010"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</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;">l</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</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;">"input: %s (width %d)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">to</span> <span style="color: #000000;">m</span> <span style="color: #008080;">do</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;">str_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: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
input: 1000 (width 4)
n = 2: 1110
input: 0100 (width 4)
n = 2: 0111
input: 0010 (width 4)
n = 2: 0011
input: 0000 (width 4)
n = 2: 0000
input: 010000000000100000000010000000010000000100000010000010000100010010 (width 66)
n = 0: 010000000000100000000010000000010000000100000010000010000100010010
n = 1: 011000000000110000000011000000011000000110000011000011000110011011
n = 2: 011100000000111000000011100000011100000111000011100011100111011111
n = 3: 011110000000111100000011110000011110000111100011110011110111111111
</pre>
 
===mpz===
identical output
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">function</span> <span style="color: #000000;">mpz_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: #004080;">mpz</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0b"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">input</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">bit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">mask</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">rask</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- (mask res)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (backward, actually)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_even</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">mpz_and</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rask</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mask</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rask</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bit</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">mpz_mul_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bit</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bit</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- aka left shift</span>
<span style="color: #7060A8;">mpz_fdiv_q_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- aka right shift</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">ret</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">lz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ret</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">lz</span> <span style="color: #008080;">then</span> <span style="color: #000000;">ret</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lz</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">ret</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">ret</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</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>
<!--</syntaxhighlight>-->
===hybrid===
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;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mpz_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: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">input</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0b"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">input</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">mask</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">rask</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- (mask res)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">mpz_and</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rask</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mask</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rask</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">mpz_fdiv_q_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- aka right shift</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</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>
<!--</syntaxhighlight>-->
 
=={{header|Python}}==
Line 57 ⟶ 1,338:
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 92 ⟶ 1,373:
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 138 ⟶ 1,419:
The <code>set_right_adjacent_bits_list</code> function does all the real work.
 
<langsyntaxhighlight lang="python">from typing import List
import re
 
 
Line 151 ⟶ 1,431:
def _list2bin(b: List[int]) -> str:
"List of 0/1 ints to bool string."
return re''.subjoin(r'[\',str(x) "\[\]]',for '',x in str(b))
 
def _to_list(bits: str) -> List[int]:
return [int(char != '0') for char in bits]
 
if __name__ == "__main__":
Line 184 ⟶ 1,464:
result = set_right_adjacent_bits_list(n, b)
print(f" Input b: {_list2bin(b)}")
print(f" Result: {_list2bin(result)}\n")</langsyntaxhighlight>
 
{{out}}
Line 225 ⟶ 1,505:
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result: 011110000000111100000011110000011110000111100011110011110111111111</pre>
 
=={{header|Quackery}}==
 
<code>bin</code> sprinkles a little syntactic sugar by extending the compiler to understand binary numbers.
 
<syntaxhighlight lang="Quackery"> [ 2 base put
nextword dup
$ '' = if
[ $ '"bin" needs a number after it.'
message put
bail ]
dup $->n iff
[ nip swap dip join ]
else
[ drop
char " swap join
$ '" is not binary.'
join message put
bail ]
base release ] builds bin ( [ $ --> [ $ )
 
[ [] unrot
times
[ dup 1 &
rot join swap
1 >> ]
drop
witheach echo ] is echobin ( n n --> )
 
 
[ dip dup times
[ 1 >> tuck | swap ]
drop ] is setrightbits ( n --> n )
 
say "n = 2; Width e = 4:"
cr cr
' [ bin 1000 bin 0100
bin 0010 bin 0001 ]
witheach
[ say "Input b: "
dup 4 echobin cr
say "Result: "
2 setrightbits
4 echobin cr cr ]
4 times
[ say "n = " i^ echo
say " Width e = 66:" cr
say "Input b: "
bin 010000000000100000000010000000010000000100000010000010000100010010
dup 66 echobin cr
say "Result: "
i^ setrightbits
66 echobin cr cr ]</syntaxhighlight>
 
{{out}}
 
<pre>n = 2; Width e = 4:
 
Input b: 1000
Result: 1110
 
Input b: 0100
Result: 0111
 
Input b: 0010
Result: 0011
 
Input b: 0001
Result: 0001
 
n = 0 Width e = 66:
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result: 010000000000100000000010000000010000000100000010000010000100010010
 
n = 1 Width e = 66:
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result: 011000000000110000000011000000011000000110000011000011000110011011
 
n = 2 Width e = 66:
Input b: 010000000000100000000010000000010000000100000010000010000100010010
Result: 011100000000111000000011100000011100000111000011100011100111011111
 
n = 3 Width e = 66:
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.
 
<syntaxhighlight lang="raku" perl6line>sub rab (Int $n is copy, Int $b = 1) {
formy 1..$n.msbm ->= $i {n;
next unless $nm +&|= (1$n +<> $i_) for ^ $b+1;
$m
$n +|= exp($i - $_, 2) for 1 .. $b
}
$n
}
 
sub lab (Int $n is copy, Int $b = 1) {
formy $n.msbm … 0 ->= $i {n;
next unless $nm +&|= (1$n +< $i_) for ^ $b+1;
$m
$n +|= exp($i + $_, 2) for 1 .. $b
}
$n
}
 
say "Powers of 2 ≤ 8, 0 - Right-adjacent-bits: 2";
# Test with a few integers.
.&rab(2).base(2).fmt('%04s').say for <8 4 2 1 0>;
 
# Test with a few integers.
for 8,4, 18455760086304825618,5, 5444684034376312377319904082902529876242,15 -> $integer, $bits {
 
Line 257 ⟶ 1,621:
.say for ^$bits .map: -> $b { $integer.&lab($b).fmt("%{0~$bits+$integer.msb}b") };
 
}</langsyntaxhighlight>
{{out}}
<pre>Integer:Powers of 2 ≤ 8, 0 - Right-adjacent-bits: up to 42
1110
0111
0011
0001
0000
 
Integer: 8 - Right-adjacent-bits: up to 4
1000
1100
Line 318 ⟶ 1,689:
011111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110</pre>
 
=={{header|RPL}}==
With the requirement of e value going up to 1000, the collection of bits must be provided as a string.
 
Using e as a variable name is generally not recommended, as it can be confused with the base of natural logarithms. Nevertheless, we use it for clarity of task implementation.
{{works with|HP|48G}}
« OVER SIZE
"" 1 4 PICK '''START''' "1" + '''NEXT'''
→ n e ones
« '''IF''' n '''THEN'''
e 1 - 1 '''FOR''' j
'''IF''' DUP j DUP SUB "1" == '''THEN'''
j 1 + ones REPL
1 e SUB
'''END'''
-1 '''STEP'''
'''END'''
» » '<span style="color:blue">SETRIGHT</span>' STO ''<span style="color:grey">@ ( "bits" n → "bits" )''</span>
« { "1000" "0100" "0010" "0000" } 1 « 2 <span style="color:blue">SETRIGHT</span> » DOLIST
« "010000000000100000000010000000010000000100000010000010000100010010" j <span style="color:blue">SETRIGHT</span> » 'j' 0 3 1 SEQ
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: { "1110" "0111" "0011" "0000" }
1: { "010000000000100000000010000000010000000100000010000010000100010010"
"011000000000110000000011000000011000000110000011000011000110011011"
"011100000000111000000011100000011100000111000011100011100111011111"
"011110000000111100000011110000011110000111100011110011110111111111" }
</pre>
 
=={{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) {
for _ in 1..=n {
*b |= b.clone() >> 1;
}
}
 
macro_rules! test {
( $t:ident, $n:expr, $e:expr, $g:ty, $b:expr, $c:expr$(,)? ) => {
#[test]
fn $t() {
let n: usize = $n;
let e: usize = $e;
let b_original: $g = $b;
let mut b = b_original.clone();
set_right_adjacent_bits(&mut b, n);
println!("n = {n}; e = {e}:");
println!(" b = {:0>e$b}", b_original);
println!(" output = {:0>e$b}", b);
assert_eq!(b, $c);
}
};
}
 
test!(test_a1, 2, 4, u8, 0b1000, 0b1110);
test!(test_a2, 2, 4, u8, 0b0100, 0b0111);
test!(test_a3, 2, 4, u8, 0b0010, 0b0011);
test!(test_a4, 2, 4, u8, 0b0000, 0b0000);
test!(
test_b1, 0, 66, u128,
0b010000000000100000000010000000010000000100000010000010000100010010,
0b010000000000100000000010000000010000000100000010000010000100010010,
);
test!(
test_b2, 1, 66, u128,
0b010000000000100000000010000000010000000100000010000010000100010010,
0b011000000000110000000011000000011000000110000011000011000110011011,
);
test!(
test_b3, 2, 66, u128,
0b010000000000100000000010000000010000000100000010000010000100010010,
0b011100000000111000000011100000011100000111000011100011100111011111,
);
test!(
test_b4, 3, 66, u128,
0b010000000000100000000010000000010000000100000010000010000100010010,
0b011110000000111100000011110000011110000111100011110011110111111111,
);</syntaxhighlight>
{{out}}
<pre>
n = 2; e = 4:
b = 1000
output = 1110
n = 2; e = 4:
b = 0100
output = 0111
n = 2; e = 4:
b = 0010
output = 0011
n = 2; e = 4:
b = 0000
output = 0000
n = 0; e = 66:
b = 010000000000100000000010000000010000000100000010000010000100010010
output = 010000000000100000000010000000010000000100000010000010000100010010
n = 1; e = 66:
b = 010000000000100000000010000000010000000100000010000010000100010010
output = 011000000000110000000011000000011000000110000011000011000110011011
n = 2; e = 66:
b = 010000000000100000000010000000010000000100000010000010000100010010
output = 011100000000111000000011100000011100000111000011100011100111011111
n = 3; e = 66:
b = 010000000000100000000010000000010000000100000010000010000100010010
output = 011110000000111100000011110000011110000111100011110011110111111111
</pre>
 
=={{header|Wren}}==
Using a list of 0's and 1's so we don't have to resort to BigInt.
<langsyntaxhighlight ecmascriptlang="wren">var setRightBits = Fn.new { |bits, e, n|
if (e == 0 || n <= 0) return bits
var bits2 = bits.toList
varfor (i =in 0...e - 1) {
while (i < e - 1) {
var c = bits[i]
if (c == 1) {
Line 334 ⟶ 1,812:
}
}
i = i + 1
}
return bits2
Line 350 ⟶ 1,827:
bits = setRightBits.call(bits, e, n)
System.print(" Result: %(bits.join())\n")
}</langsyntaxhighlight>
 
{{out}}
2,123

edits