Set right-adjacent bits: Difference between revisions

Added 11l
m (Formatting text.)
(Added 11l)
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}}==
1,454

edits