Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How can I set a bit to 0 ?

by stevieb (Canon)
on May 26, 2022 at 20:56 UTC ( [id://11144206]=note: print w/replies, xml ) Need Help??


in reply to How can I set a bit to 0 ?

I prototype a lot of my C microcontroller code in Perl, and for that purpose so I, and others might benefit, I wrote Bit::Manip. That contains code that has to be compiled, so for a Pure Perl version, there's Bit::Manip::PP.

It does all the things that we who have to do bitwise operations from time-to-time but not as a profession (or often enough to remember how) we need to do.

Here's an example of turning 'off' a bit:

Code:

use strict; use warnings; use Bit::Manip qw(:all); my $num = 0b11100111; # or literal 231 my $bit_position = 6; my $flipped = bit_off($num, $bit_position); printf("orig: %b\n", $num); printf("flipped: %b\n", $flipped);

Output:

steve@maezi ~/scratch >perl bit.pl orig: 11100111 flipped: 10100111

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144206]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found