Set right-adjacent bits: Difference between revisions

Added Wren
m (→‎{{header|Raku}}: demonstrate left-adjacent-bits while we're at it)
(Added Wren)
Line 318:
011111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110</pre>
 
=={{header|Wren}}==
Using a list of 0's and 1's so we don't have to resort to BigInt.
<lang ecmascript>var setRightBits = Fn.new { |bits, e, n|
if (e == 0 || n <= 0) return bits
var bits2 = bits.toList
var i = 0
while (i < e - 1) {
var c = bits[i]
if (c == 1) {
var j = i + 1
while (j <= i + n && j < e) {
bits2[j] = 1
j = j + 1
}
}
i = i + 1
}
return bits2
}
 
var b = "010000000000100000000010000000010000000100000010000010000100010010"
var tests = [["1000", 2], ["0100", 2], ["0010", 2], ["0000", 2], [b, 0], [b, 1], [b, 2], [b, 3]]
for (test in tests) {
var bits = test[0]
var e = bits.count
var n = test[1]
System.print("n = %(n); Width e = %(e):")
System.print(" Input b: %(bits)")
bits = test[0].map { |c| c.bytes[0] - 48 }.toList
bits = setRightBits.call(bits, e, n)
System.print(" Result: %(bits.join())\n")
}</lang>
 
{{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>
9,485

edits