Set right-adjacent bits: Difference between revisions

Added PureBasic
(Added PureBasic)
 
(9 intermediate revisions by 7 users not shown)
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}}==
Line 294 ⟶ 376:
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#}}==
Line 385 ⟶ 720:
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}}==
Line 1,118 ⟶ 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}}==
Line 1,216 ⟶ 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}}==
Line 1,297 ⟶ 1,800:
=={{header|Wren}}==
Using a list of 0's and 1's so we don't have to resort to BigInt.
<syntaxhighlight lang="ecmascriptwren">var setRightBits = Fn.new { |bits, e, n|
if (e == 0 || n <= 0) return bits
var bits2 = bits.toList
2,122

edits