Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How can I set a bit to 0 ?

by kcott (Archbishop)
on May 26, 2022 at 17:34 UTC ( [id://11144202]=note: print w/replies, xml ) Need Help??


in reply to How can I set a bit to 0 ?

G'day bartender1382,

"What I don't know how to do ... turn off a specific bit whether it is set or not."

Update: This wasn't a good solution (see reply below). The following would have been better:

#!/usr/bin/env perl use strict; use warnings; my $var1 = 0b00010100; my $var2 = 0b00000110; my $fmt = "%08b\n"; print "Initial:\n"; printf $fmt, $var1; printf $fmt, $var2; for my $bit (0, 1, 4) { print "Bit $bit off:\n"; $var1 &= ~(1 << $bit); printf $fmt, $var1; $var2 &= ~(1 << $bit); printf $fmt, $var2; }

I've stricken the original code; it's in the spoiler. The output is unchanged.

Output:

Initial: 00010100 00000110 Bit 0 off: 00010100 00000110 Bit 1 off: 00010100 00000100 Bit 4 off: 00000100 00000100

— Ken

Replies are listed 'Best First'.
Re^2: How can I set a bit to 0 ?
by GrandFather (Saint) on May 26, 2022 at 21:43 UTC

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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-28 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found