Set right-adjacent bits: Difference between revisions

Added PureBasic
(Added Easylang)
(Added PureBasic)
(3 intermediate revisions by 2 users not shown)
Line 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++}}==
Line 550 ⟶ 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,578 ⟶ 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,130

edits