Set right-adjacent bits: Difference between revisions

m
Formatting text.
m (Formatting text.)
Line 297:
=={{header|C++}}==
<syntaxhighlight lang="c++">
 
#include <iostream>
#include <string>
 
void setRightAdjacent(std::string text, int32_t number) {
std::cout << "n = " << number << ", Width = " << text.size() << ", Input: " << text << std::endl;
 
std::string result = text;
for ( uint32_t i = 0; i < result.size(); i++ ) {
if ( text[i] == '1' ) {
for ( uint32_t j = i + 1; j <= i + number && j < result.size(); j++ ) {
result[j] = '1';
}
}
}
 
std::cout << std::string(16 + std::to_string(text.size()).size(), ' ') << "Result: " + result << "\n" << std::endl;
}
 
int main() {
setRightAdjacent("1000", 2);
setRightAdjacent("0100", 2);
setRightAdjacent("0010", 2);
setRightAdjacent("0000", 2);
 
std::string test = "010000000000100000000010000000010000000100000010000010000100010010";
setRightAdjacent(test, 0);
setRightAdjacent(test, 1);
setRightAdjacent(test, 2);
setRightAdjacent(test, 3);
}
 
</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>
 
908

edits