Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How can I set a bit to 0 ?

by Marshall (Canon)
on May 27, 2022 at 00:19 UTC ( [id://11144214]=note: print w/replies, xml ) Need Help??


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

Your question is not at all silly. Good question.

More often than as a bit number, these binary bits are defined in a "bit mask".
It is important to understand the difference between just & (bit wise and) and && (high priority logical and).
Also important is the difference between ~ (bit inverse) and ! (logical not).

Complex binary manipulation can be difficult in the High Level Language (Perl or even C) because you don't know how many bits you are dealing with (16,32,64).
However, these simple idioms suffice for most common cases.

Some example code:

use strict; use warnings; use constant { upload => 1, # same as 0b1 getTicket => 2, # same as 0b10 downLoading => 4, # same as 0b100 whatEver => 8, # same as 0b1000 moreEver => 16,# same as 0b10000 }; my $stats = upload | getTicket | downLoading | whatEver | moreEver; print "numeric representation in decimal of all bit set is: $stats\n\n +"; # check if getTicket is on? print "checking if getTicket is on?\n"; $stats & getTicket ? print "getTicket is ON\n\n" : print "getTicket is + OFF\n\n"; # turn getTicket off print "turning getTicket off...\n"; # note: we need bitwise "and, which is &" and ~ instead of logical ! $stats = $stats & ~getTicket; # ~ means bit inverse of bits print "numeric decimal value = $stats\n"; # check if getTicket is off? print "check if getTicket turned off?\n"; $stats & getTicket ? print "getTicket is ON\n\n" : print "getTicket is + OFF\n\n"; # flip status of just getTicket print "flipping state of getTicket...\n"; $stats = $stats ^ getTicket; print "numeric decimal value = $stats\n"; $stats & getTicket ? print "getTicket is ON\n\n" : print "getTicket is + OFF\n\n"; print "flipping state of getTicket...\n"; $stats = $stats ^ getTicket; print "numeric decimal value = $stats\n"; $stats & getTicket ? print "getTicket is ON\n\n" : print "getTicket is + OFF\n\n"; __END__ numeric representation in decimal of all bit set is: 31 checking if getTicket is on? getTicket is ON turning getTicket off... numeric decimal value = 29 check if getTicket turned off? getTicket is OFF flipping state of getTicket... numeric decimal value = 31 getTicket is ON flipping state of getTicket... numeric decimal value = 29 getTicket is OFF
Update: I suppose that something like: use constant allBits => upload | getTicket | downLoading | whatEver | moreEver; is possible in favor of ~0 (which means turn all bits in the integer register ON). Often you don't want to cause any bits to be turned on which are not specified.

Replies are listed 'Best First'.
Re^2: How can I set a bit to 0 ?
by bartender1382 (Beadle) on May 27, 2022 at 16:29 UTC

    Your question is not at all silly.

    Silly in the sense that have worked with bit masks in the past, and if I ever knew how to "clear one" I forgot it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2024-04-16 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found