Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: converting binary to decimal

by LanX (Saint)
on Jun 06, 2025 at 21:57 UTC ( [id://11165302]=note: print w/replies, xml ) Need Help??


in reply to Re^4: converting binary to decimal
in thread converting binary to decimal

Not my code, and far too tricky to maintain in my opinion.

Relying on the idiosyncracies of | is nothing I recommend.

I even have problems to reproduce my own explanation in the debugger.

The original code works tho when tested. 🤷🏻‍♀️

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^6: converting binary to decimal
by LanX (Saint) on Jun 07, 2025 at 17:35 UTC
    > I even have problems to reproduce my own explanation in the debugger.

    And the reason is that it's supposed to be a "bitstring" not "numeric bit" operation.

    see https://perldoc.perl.org/perlop#Bitwise-String-Operators

    hence replacing | with |. under use feature "bitwise" will stop doing hard to predict things, even after refactoring this golfing code.

    ugh...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      That's why the original code very intentionally |'s together two results of the (higher precedence) concatenation operator '.' - which always produces a "string" and not a "number".

      voodoo89 tybalt89

        guess what, refactoring fails,

        my $copy1b = '0' . $copy1; my $copy2b = $copy2 . $_; $n = ($copy1b | $copy2b);

        copying it literally into the debugger too

        DB<38> say (0 . "$_" | 1 . "1") for qw/0 2 4 6 8/ 11 11 15 15 11

        The only fix is using |. and use feature 'bitwise';

        DB<39> use feature 'bitwise'; say (0 . "$_" |. 1 . "1") for qw/0 2 4 + 6 8/ 11 13 15 17 19

        > voodoo89

        Yeah, now go back and sacrifice 89 more chickens to the god of golf ;-P

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2025-12-06 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (85 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.