Set right-adjacent bits: Difference between revisions

Added PureBasic
(Added various BASIC dialects (QBasic and True BASIC))
(Added PureBasic)
Line 378:
 
=={{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}}
2,131

edits