Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: How can I set a bit to 0 ? by Marshall
in thread How can I set a bit to 0 ? by bartender1382

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found