bartender1382 has asked for the wisdom of the Perl Monks concerning the following question:
I feel silly asking this, but...
I know how to set bits, and check for them. What I don't know how to do, nor find on Google,is turn off a specific bit whether it is set or not.
my $stats = 0; $stats = upload | getTicket | downLoading; printCLine($stats);
Yes, I can reset every bit minus the one I want, but that feels kludgy
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How can I set a bit to 0 ?
by ikegami (Patriarch) on May 26, 2022 at 16:37 UTC | |
Set:
Clear:
Toggle:
Read (returns a true value or a false value):
It looks like you have constants that are already of the form 1 << $bit_num, so
| [reply] [d/l] [select] |
Re: How can I set a bit to 0 ?
by stevieb (Canon) on May 26, 2022 at 20:56 UTC | |
I prototype a lot of my C microcontroller code in Perl, and for that purpose so I, and others might benefit, I wrote Bit::Manip. That contains code that has to be compiled, so for a Pure Perl version, there's Bit::Manip::PP. It does all the things that we who have to do bitwise operations from time-to-time but not as a profession (or often enough to remember how) we need to do. Here's an example of turning 'off' a bit: Code:
Output:
| [reply] [d/l] [select] |
Re: How can I set a bit to 0 ?
by Marshall (Canon) on May 27, 2022 at 00:19 UTC | |
More often than as a bit number, these binary bits are defined in a "bit mask".
Complex binary manipulation can be difficult in the High Level Language (Perl or even C) because you don't know how many bits you are dealing with (16,32,64). Some example code: Update: I suppose that something like: use constant allBits => upload | getTicket | downLoading | whatEver | moreEver; is possible in favor of ~0 (which means turn all bits in the integer register ON). Often you don't want to cause any bits to be turned on which are not specified. | [reply] [d/l] [select] |
by bartender1382 (Beadle) on May 27, 2022 at 16:29 UTC | |
Silly in the sense that have worked with bit masks in the past, and if I ever knew how to "clear one" I forgot it. | [reply] |
Re: How can I set a bit to 0 ? (Add Two Numbers Using Only Bitwise Operators)
by eyepopslikeamosquito (Archbishop) on May 28, 2022 at 10:18 UTC | |
Poor old bartender1382 asking the simple and naive question "How can I set a bit to 0?" seems to have got more than he bargained for. Thanks Grumpy Gramps! :) For cheap thrills, let's try to steer this thread further off course with a couple of example programs to add numbers using only bitwise operators. Improvements, alternative implementations welcome. Note: this was just for fun, I've never had a genuine need to do this, interested to hear from folks who have. Perl Program to Add Two Numbers Using Only Bitwise Operators
Example run:
C++ Program to Add Two Numbers Using Only Bitwise Operators
Example run:
See Also
| [reply] [d/l] [select] |
by AnomalousMonk (Archbishop) on May 28, 2022 at 13:42 UTC | |
Note that integer has lexical scope and so could be used within the definition of a subroutine to limit the effects (and side effects) of the pragma to operations within the subroutine. Give a man a fish: <%-{-{-{-< | [reply] [d/l] |
Re: How can I set a bit to 0 ?
by kcott (Archbishop) on May 26, 2022 at 17:34 UTC | |
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:
I've stricken the original code; it's in the spoiler. The output is unchanged. <Reveal this spoiler or all in this thread>
Output:
— Ken | [reply] [d/l] [select] |
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
| [reply] |
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. | [reply] |
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 | [reply] |
by GrandFather (Saint) on May 27, 2022 at 04:22 UTC | |
by kcott (Archbishop) on May 27, 2022 at 05:13 UTC | |
| |
Re: How can I set a bit to 0 ?
by afoken (Chancellor) on May 26, 2022 at 23:28 UTC | |
Using butterflies, of course! Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] |
Re: How can I set a bit to 0 ?
by BillKSmith (Monsignor) on May 27, 2022 at 13:44 UTC | |
Bill
| [reply] |
by LanX (Saint) on May 27, 2022 at 15:05 UTC | |
> Use pack/unpack The OP seems on a beginner level and you didn't demonstrate how this would be done. Even I can't remember ever using vec and pack/unpack force me to reread their perldocs again and again. So I'd appreciate if you could shown some example code solving the OP's question this way, please. I really don't know by myself and am always eager to learn.
Cheers Rolf | [reply] |
by BillKSmith (Monsignor) on May 28, 2022 at 22:46 UTC | |
I created a disc file which contains nothing but a 16-bit status word and verified the exact contents of that file with the utility "xxd" (came packaged with vim). My example reads that status word, makes a few changes, and writes it back out to another file. Again I use xxd to verify that I made the changes that I intended. OK, I admit that lines 11 thru 19 and 25 thru 33 were developed empirically for this use of vec on my 64-bit intel machine. They are not portable to other environments but should work, without change, on any application on this or similar machine.
Here is the content of the two data files (displayed by xxd) after this example runs
Note: There is no guarantee that traditional bit masking will eliminate this problem in I/O. It might, but testing is still required.
Bill
| [reply] [d/l] [select] |
Re: How can I set a bit to 0 ?
by LanX (Saint) on May 26, 2022 at 22:36 UTC | |
you "mask" (using & the "bitwise-and") with a bitvector which is only 0 if and only if you want to turn off.
for more see Mask (computing)
Cheers Rolf | [reply] [d/l] |
Re: How can I set a bit to 0 ?
by harangzsolt33 (Chaplain) on May 28, 2022 at 02:19 UTC | |
For example, 0000 ^ 0000 will simply leave all the bits zero.
The AND operator turns specific bits OFF. It takes two input numbers. The bits in input number 1 determine which bits in input number will be turned off. The ones that are zero will be turned off. Here is an example: 1001 ^ 0110 = 0000
See, this is really simple. Here is a tiny example program:
| [reply] [d/l] |
by AnomalousMonk (Archbishop) on May 28, 2022 at 04:49 UTC | |
Input number 1 determines which bits will be negated in input number 2 ... Just a note of caution: This phrasing suggests that the order of the operands affects the result of the ^ (bitwise-xor) operator, but it does not. The order is irrelevant: And likewise for the | (bitwise-or) and & (bitwise-and) operators. Update: Slight change to phrasing of first sentence for clarity. Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |