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

shift in a sub shifts @_, the array that holds the sub's arguments.

Replies are listed 'Best First'.
Re^4: converting binary to decimal
by Anonymous Monk on Jun 06, 2025 at 15:00 UTC
    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.

      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).

      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!