"be consistent" PerlMonks

Re^2: Operate with bits

by soonix (Canon)
 on Mar 11, 2020 at 14:45 UTC Need Help??

in reply to Re: Operate with bits

• 1.) would be correct for characters (or hex digits), but OP wants bits, so you would have to do split(//, sprintf("%b", \$address))
• 2.) additionally, for substr to count its offset from right, you have to use negative, so substr((sprintf "%b", \$address), -6, 1)

Replies are listed 'Best First'.
Re^3: Operate with bits
by rizzo (Chaplain) on Mar 12, 2020 at 04:18 UTC
Yep. Thanks for the correction.
Re^3: Operate with bits
by syedasadali95 (Acolyte) on Mar 12, 2020 at 06:18 UTC

Thanks rizzo & soonix. Actually substr((sprintf "%b", \$address), -7, 1) is serving my need as we are starting from right side of the bit stream (LSB) and starting to count from bit 0 to bit 5, so total of six bits and keeping -7 in the substr func as index makes us catch the 6th bit from the bit stream. Thanks again for the help guys

I don't understand why GrandFather's solution (bitwise-anding) would not give you the same result with a lot less wasted motion.

Give a man a fish:  <%-{-{-{-<

I assume that for most humans it is easier to see the "finding bit X" problem as one of dealing with strings, than as one of calculating (although, of course, the latter is much easier for the computer).

In other words:

sprintf '%b' converts to a string of bits, then I can take the 7th char from the right
is easier to understand (for most humans) than
If I have it as a number, I simply "AND" it with 26
Of course, it all depends: if there are millions of lines, the inefficiency will add up and become noticeable.

Maybe because syedasadali95 is actually dealing with hex digits (nibbles), not with bits as assumed by my post and stated by every one of syedasadali95's posts up until the reply to soonix.

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

Create A New User
Node Status?
node history
Node Type: note [id://11114129]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2020-06-02 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you really want to know if there is extraterrestrial life?

Results (13 votes). Check out past polls.

Notices?