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

Re^4: How can I set a bit to 0 ?

by GrandFather (Saint)
on May 27, 2022 at 04:22 UTC ( [id://11144218]=note: print w/replies, xml ) Need Help??


in reply to Re^3: How can I set a bit to 0 ?
in thread How can I set a bit to 0 ?

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

Replies are listed 'Best First'.
Re^5: How can I set a bit to 0 ?
by kcott (Archbishop) on May 27, 2022 at 05:13 UTC

    I thought about what you had written whilst traveling to and from my appointment. On returning, I modified my original code, tested it and posted an update.

    There were no replies when I started to compose my post. I got distracted midway and, when I did post, I saw ikegami had replied; however, I had to rush off for $work and didn't really look at it closely.

    Thanks for your further explanation. Unfortunately, the vaccine is already taking effect and the code is just spinning around before me. Looks like another weekend of being completely ga-ga: probably best to revisit this in a few days.

    — Ken

      There were no replies when I started to compose my post. I got distracted midway and, when I did post, I saw ikegami had replied

      Yes - I have a similar policy. If there's no replies when I start composing one, I just send my post off anyway, as soon as it's finished.
      It's a policy that has been known to result in egg on face - but egg is good for the complexion ... so it's "Win-Win!!".

      The interesting thing with this thread is that I can't see how any of the replies (including GF's diversion) relate to the original post.

      bartender1382 posts some nonsensical snippet that doesn't even compile ... and everyone has a ready made solution !!
      GrandFather is even able to provide some evaluation of the different approaches !!

      I am clearly unfit to sit at the same table ;-)

      Cheers,
      Rob
        > bartender1382 posts some nonsensical snippet that doesn't even compile ... and everyone has a ready made solution !!

        you seem to miss that he's using constants, like so often in bit operations.

        DB<44> use constant {upload =>0b1, getTicket =>0b10, downLoading=>0b +100}; DB<45> use warnings; use strict; say my $stats = upload | getTicket +| downLoading; 7 DB<46>

        > The interesting thing with this thread is that I can't see how any of the replies (including GF's diversion) relate to the original post.

        Now I'd be interested to know how I failed to answer exactly the question given in a concise way, especially given the level of the OP.

        Plus explanation most others were lacking including the correct terminology and multiple further read-more links.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      "... the vaccine is already taking effect ... probably best to revisit this in a few days."

      Well, that certainly knocked me for six. I originally posted early Friday morning; it's now late Thursday afternoon and I'm only just starting to return to normality.

      It certainly wasn't my intention to start such a long subthread. :-)

      — Ken

        Mine is a long and a sad tale!” said the Mouse, turning to Alice, and sighing.
        “It is a long tail, certainly,” said Alice, looking down with wonder at the Mouse’s tail; “but why do you call it sad?
        (Alice in Wonderland, chapter 3)
      Ken you did nothing wrong, except suffering from a raging cranky old man disease.

      Your answer compiled and you didn't deserve those reactions partly due to herd instinct.

      It's a Perl forum, not a obfuscated C board and TIMTOWTDI.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found