Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Suppose I have a string in perl.
$a = "001010";
After manipulating a bitwise not operator, the result should be
~$a = "110101";
How can I do this in perl?
perldoc -f pack
Or just using tr///:
tr///
$str =~ tr/01/10/;
Add a comment
One solution is using a regex:
$a = "001010"; $a =~ s/([01])/1-$1/eg;
You can use oct and sprintf:
oct
sprintf
$a = sprintf("%0".(length($a))."b", (~oct("0b".$a)) & (1 << length($a)) - 1);
There Is More Than One Way To Do It:
$str = join '' => map 0+!$_, split //, $str;
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
perldoc -f pack) for details on doing those conversions.