What I'm wondering is if there's a way to change several bits at a time. Instead of the last bit, what if I wanted to go from 10001010 to say, 10001101? (from 10 to 13 in the last four bits). Is there an easy way to do that
If you know the input is going to be (or: when it is) 10001010, that you want to change it to 10001101; simply assign the latter value to it.
Another interpretation of your requirement is that given ?????010 you want to change it to ?????101; then:
sub changeIt { my $old = shift; return ( $old & 0b11111000 ) | 0b00000101; }
In reply to Re: Modifying multiple bits in a byte - bitwise operators
by BrowserUk
in thread Modifying multiple bits in a byte - bitwise operators
by stevieb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |