Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by kcott (Archbishop)
on May 27, 2022 at 02:33 UTC ( #11144217=note: print w/replies, xml ) Need Help??


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

G'day GrandFather,

++ Thanks for your thoughtful reply.

I'll need to study this in more detail. I have an inkling of where I've cocked-up but I have to rush off to get my final Covid booster shot. I'll get back to this later today (side-effects permitting).

— Ken

Replies are listed 'Best First'.
Re^4: How can I set a bit to 0 ?
by GrandFather (Saint) on May 27, 2022 at 04:22 UTC

    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

      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
        "... 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

        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://11144217]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2022-08-10 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?