Set right-adjacent bits: Difference between revisions

Content added Content deleted
Line 114: Line 114:
=={{header|Phix}}==
=={{header|Phix}}==
The basic idea is to create a mask of n 1s on the right and either shift that left or (a copy of) the input right, and use it to determine which bits of the result need to be set, in addition to the originals. The string version actually uses a count of 1s instead of an actual mask, and obviously the odd() and even() functions work just as well on the characters '0' and '1' as they do the numbers/bits 0 and 1.
The basic idea is to create a mask of n 1s on the right and either shift that left or (a copy of) the input right, and use it to determine which bits of the result need to be set, in addition to the originals. The string version actually uses a count of 1s instead of an actual mask, and obviously the odd() and even() functions work just as well on the characters '0' and '1' as they do the numbers/bits 0 and 1.
Note that both the string and mpz versions propagate any number of bits in just the one single pass.
Note that both the string and mpz versions propagate any number of bits in a single pass, in other words explicitly iterating down all the input bits as opposed to implicitly setting all those bits n times, albeit the latter is probably a smidge faster.
===string===
===string===
<!--<lang Phix>(phixonline)-->
<!--<lang Phix>(phixonline)-->