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

Oh oh, I see!! So, you get the value of $_[0] using shift, then you split the incoming string into individual characters, then you iterate through each character one by one performing this Chinese transform on each digit:

$n = (0 . $n =~ tr/1-9/246802468/r | $n =~ tr/1-9/00001/r . $_) =~
    s/^0+(?=.)//r

I have no idea what I'm looking at, but I think, if I can ever come up with code like this, I will have become a Perl hacker. Haha for example, I don't understand how transforming digits from 1-9 into 246802468 is going to end up as a decimal in the end. So, I don't understand any of this. But I find it fascinating/amazing.

I'm Harangzsolt33, but somehow I got logged out.

Replies are listed 'Best First'.
Re^5: converting binary to decimal
by tybalt89 (Monsignor) on Jun 06, 2025 at 15:28 UTC

    The first tr/// converts each digit to the least significant digit you get when you multiply that digit by 2. The second tr/// produces the carry bit for each digit product. The bitwise 'or' adds the carry to the next higher digit (because of the concatenation of the new bit).

    It's sort of like how you were doing the multiply by 2, but with a better handling of the carry. Because we are only multiplying by 2 there never will be a chained carry (where a carry causes another carry, similar to 4999+1).

Re^5: converting binary to decimal
by afoken (Chancellor) on Jun 06, 2025 at 15:52 UTC
    I'm Harangzsolt33, but somehow I got logged out.

    You can claim your posts. There is a link with the text "Is this your post?" on each post by Anonymous Monk.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Oh, I see. Thank you!
        Oh, I see.

        See. Understand. Act.

        I am missing at least one of these here.

        Greetings,
        🐻

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$