Just taking the first of those problem lines should make it clear to you:

print ((128&$bin)/128);

This takes your number in $bin and bitwise-ANDs it with 128 (27 = 128). The result of that will be either 128 or 0. Divide that result by 128 and you get zero or one.

Repeat for 64 (26) down to 1 (20) and you have your binary number.

Take 123, for example

123 & 128 = 0 / 128 = 0
123 & 64 = 64 / 64 = 1
123 & 32 = 32 / 32 = 1
123 & 16 = 16 / 16 = 1
123 & 8 = 8 / 8 = 1
123 & 4 = 0 / 4 = 0
123 & 2 = 2 / 2 = 1
123 & 1 = 1 / 1 = 1
Read the binary, top to bottom: 12310 = 011110112

update: added the example


In reply to Re: Bitwise and operator question by FunkyMonk
in thread Bitwise and operator question by Severy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.