Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Operate with bits

by rizzo (Deacon)
on Mar 11, 2020 at 11:04 UTC ( #11114119=note: print w/replies, xml ) Need Help??

in reply to Operate with bits

Two ways:

1.) use split('', $address)

2.) use substr($address,6)

Replies are listed 'Best First'.
Re^2: Operate with bits
by soonix (Canon) on Mar 11, 2020 at 14:45 UTC
    • 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)
      Yep. Thanks for the correction.

      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:  <%-{-{-{-<

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2022-10-06 07:17 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (26 votes). Check out past polls.