Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Operate with bits

by rizzo (Curate)
on Mar 11, 2020 at 11:04 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found