I have a number:
11100111
I want an operation to change a specific bit of my choice to 0.
So, if I wanted it to be :
10100111
for the 7th bit what operation would I use, for, say:
$x = 6;
$y = "11100111";
It's going to look something like:
$z = $y & $x
But, I know that's wrong. I know I could subtract 2^$x from the value, but that seems inelegant.
CodePudding user response:
I think that your use of a string is the wrong way to go. You should use integers. If we convert the binary number to a decimal number we get:
11100111 binary = 231 decimal
We can create a number, that only has the 7th bit set, this way:
1 << 6
And we can create a number, that has all the bits set, except the 7th:
~(1 << 6)
With that in mind, we can mask the 7th bit:
my $x = 6;
my $y = 231;
my $z = $y & ~(1 << $x);
print "$z\n"
This outputs the decimal 167
which is 10100111
in binary.
CodePudding user response:
Since your binary number is really a string, you can use substr to simply replace a 1 with a 0:
use warnings;
use strict;
my $x = 6;
my $y = '11100111';
print "$y\n";
substr($y, (7 - $x), 1) = '0';
print "$y\n";
Prints:
11100111
10100111
CodePudding user response:
My Bit::Manip distribution does this kind of thing.
use strict;
use warnings;
use Bit::Manip qw(:all);
my $num = 0b11100111;
my $bit_position = 6;
my $flipped = bit_off($num, $bit_position);
printf("orig: %b\n", $num);
printf("flipped: %b\n", $flipped);
Output:
orig: 11100111
flipped: 10100111
See the documentation for everything bitwise the library can do.