Set right-adjacent bits: Difference between revisions

New post.
(Created Nim solution.)
(New post.)
Line 491:
<code>b</code> converts from character list to bit list (and its obverse converts back to character list, for easy viewing).
<code>task</code> uses b on the right argument to <code>smearright</code> and its obverse on the result, which it provides with the original value (again, for easy viewing).
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
public final class SetRightAdjacentBits {
 
public static void main(String[] aArgs) {
setRightAdjacent("1000", 2);
setRightAdjacent("0100", 2);
setRightAdjacent("0010", 2);
setRightAdjacent("0000", 2);
String test = "010000000000100000000010000000010000000100000010000010000100010010";
setRightAdjacent(test, 0);
setRightAdjacent(test, 1);
setRightAdjacent(test, 2);
setRightAdjacent(test, 3);
}
private static void setRightAdjacent(String aText, int aNumber) {
System.out.print("n = " + aNumber + ", Width = " + aText.length());
System.out.print(", Input: ");
System.out.println(aText);
char[] text = aText.toCharArray();
char[] result = aText.toCharArray();
for ( int i = 0; i < result.length; i++ ) {
if ( text[i] == '1' ) {
for ( int j = i + 1; j <= i + aNumber && j < result.length; j++ ) {
result[j] = '1';
}
}
}
String spaces = " ".repeat(16 + String.valueOf(aText.length()).length());
System.out.print(spaces + "Result: ");
System.out.println( new String(result) + System.lineSeparator());
}
 
}
</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|Julia}}==
908

edits