Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by GrandFather (Saint)
on May 26, 2022 at 21:43 UTC ( [id://11144208]=note: print w/replies, xml ) Need Help??


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

Usually you are right on the money with excellent replies and advice. In this case you've missed by a country mile, especially in light of ikegami's excellent reply an hour earlier.

Like stevieb, my experience writing embedded C++ firmware informs my familiarity with bit manipulation so I construct appropriate bit manipulation expressions almost without thought. The thought of using a sub to do what amounts to a handful of processor instructions even on RISC machines makes me shudder. I apologize for my reaction, but given the reply is from such an esteemed (at least by me) monk I felt the need to ensure the OP and other seekers of enlightenment shift their gaze to ikegami's reply.

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

Replies are listed 'Best First'.
Re^3: How can I set a bit to 0 ?
by stevieb (Canon) on May 26, 2022 at 21:59 UTC
    I apologize for my reaction

    Most certainly not needed here, especially with the complete diligence and respect shown throughout the entire post (and all of your posts).

    I, for one, am in complete agreement with what you've said. ikegami's posts here and wide regarding bitwise operations show pure expertise, and, in fact, the software I posted about below was partly based on his posts here, and outside of this site.

Re^3: How can I set a bit to 0 ?
by kcott (Archbishop) on May 27, 2022 at 02:33 UTC

    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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found