Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi Ken, it's not broken code, just sub-optimum technique. Bitwise operators are fundamental to most processors so they are generally very efficient at a processor level. In particular there should be no need for a test and conditional set (using xor or otherwise). There is a short comming with the code presented so far though. In the OP's context of setting flags in a bit set it's fine, but a common requirement is to update the value of a field of bits (1 to many adjacent bits). Again there is no need for a test. You just:

use strict; use warnings; my $fieldIndex = 3; my $fieldMask = 0b111 << $fieldIndex; for my $fieldValue (0b000, 0b001, 0b100, 0b111) { my $fieldBits = $fieldMask & ($fieldValue << $fieldIndex); printf "With field value 0b%08b: \n ", $fieldBits; for my $pattern (0x00, 0xff) { my $value = ($pattern & ~$fieldMask) | $fieldBits; printf "0b%08b --> 0b%08b ", $pattern, $value; } print "\n"; }

Prints:

With field value 0b00000000: 0b00000000 --> 0b00000000 0b11111111 --> 0b11000111 With field value 0b00001000: 0b00000000 --> 0b00001000 0b11111111 --> 0b11001111 With field value 0b00100000: 0b00000000 --> 0b00100000 0b11111111 --> 0b11100111 With field value 0b00111000: 0b00000000 --> 0b00111000 0b11111111 --> 0b11111111

In this case the field is three bits wide (see $fieldMask). The important lines are the generation of $fieldBits which gets the field value in the right place and the generation of $value which inserts the field bits into the target bit pattern. This is bread and butter stuff in the embedded world, and is common when twiddling bit for things like network packets or in binary files.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re^4: How can I set a bit to 0 ? by GrandFather
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 examining the Monastery: (3)
As of 2024-04-19 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found