Severy has asked for the wisdom of the Perl Monks concerning the following question:

hi all..,

I have a question.

I am currently trying to learn perl with the book: beginning perl.

I have done the first chapter and im working on the exercises of the second chapter.

In the exercises i am expected to use things that are not explained.

chomp was the first one i had to use but is not explained.

INT is the second i had to use but is not explained.

And the bitwise & operator was explained but not enough to understand the usage of it when trying to convert decimal to binary

Here is my question:

This is the Ex2_3.plx solution code to my litlle decimal to binary conversion problem:
#!usr/bin/perl #Ex2_3.plx use warnings; print "Please enter the value(less than 256) you wish to be converted +into binary : \n"; my $bin = <STDIN>; chomp ($bin); print "The binary value of $bin is : ", "\n"; #Use the bitwise and operator to determine the binary value: print ((128&$bin)/128); print ((64&$bin)/64); print ((32&$bin)/32); print ((16&$bin)/16); print ((8&$bin)/8); print ((4&$bin)/4); print ((2&$bin)/2); print ((1&$bin)/1); print "\n";
I understand everything except this part:
print ((128&$bin)/128); print ((64&$bin)/64); print ((32&$bin)/32); print ((16&$bin)/16); print ((8&$bin)/8); print ((4&$bin)/4); print ((2&$bin)/2); print ((1&$bin)/1); print "\n";
In the book it is not explained how this works.. I know that it works but i just cant figure out how. Can someone please explain to me how this works? I like it if i can get something to work but i love it when i get something to work and know how it works.

Thx in advance

Replies are listed 'Best First'.
Re: Bitwise and operator question
by FunkyMonk (Bishop) on Aug 09, 2007 at 14:13 UTC
    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

      A little more of explanation: Realize the following decimal to binary correspondence:

      128 = 10000000 64 = 01000000 32 = 00100000 16 = 00010000 8 = 00001000 4 = 00000100 2 = 00000010 1 = 00000001

      A binary AND (&) compares bit to bit 2 numbers. If you have the number 123 (in binary: 01111011):

      123 & 128 = 01111011 (123) & 10000000 (128) ----------- 00000000 (0) = 0 123 & 64 01111011 (123) & 01000000 (64) ----------- 01000000 (64) = 64 123 & 32 01111011 (123) & 00100000 (32) ----------- 00100000 (32) = 32 And so on...

      Hope this helps to clarify the script

      citromatik

        it does.. thank you very much!
      ok.. I allmost get it

      why is 123&4 = 0 /4 =0? is there a calculation formula?

      thx for reply
        Because...

        Decimal Binary 123 01111011 4 00000100 ======== 00000000 when ANDed together

        The answer comes from ANDing the bits in the columns together. Bit 2 (third from the right) has a decimal value of 4 (because 22 = 4).

        Is that any clearer?

        Because it's a bitwise AND.

        123 = 1111011 4 = 0000100 ^

        The operator '&' returns a value where each bit of one value is is ANDed against each bit of the other value.

        The marked digit is 1 for the value 4, and 0 for value 123. The rest of the digits are 1 for value 123 and 0 for 4. When you do an AND of each bit in this case, there are no bits which are 1 in both values. That's why you get a value with no bits set to 1, which is 0.

        It'll help if you stop thinking of '&' and '|' as mathematical operators and thinking of them as boolean logical tests against vectors of bits, which is essentially what they are.

        From perlop:

        Bitwise And

        Binary "&" returns its operands ANDed together bit by bit. (See also "Integer Arithmetic" and "Bitwise String Operators".)

        Note that "&" has lower priority than relational operators, so for example the brackets are essential in a test like

        print "Even\n" if ($x & 1) == 0;

        Bitwise Or and Exclusive Or

        Binary "|" returns its operands ORed together bit by bit. (See also "Integer Arithmetic" and "Bitwise String Operators".)

        Binary "^" returns its operands XORed together bit by bit. (See also "Integer Arithmetic" and "Bitwise String Operators".)

        Note that "|" and "^" have lower priority than relational operators, so for example the brackets are essential in a test like

        print "false\n" if (8 | 2) != 10;
Re: Bitwise and operator question
by perrin (Chancellor) on Aug 09, 2007 at 14:13 UTC
Re: Bitwise and operator question
by johnlawrence (Monk) on Aug 09, 2007 at 15:04 UTC

    The 'bitwise AND' compares each bit of the operands and if the corresponding bits both have the value 1, the corresponding bit of the result is set to 1. Otherwise, the corresponding bit of the result is set to 0.

    Looking at an example, if $bin = 58, it's binary representation is 00111010. So, going through the lines you mention do the following.

    10000000 & 00111010 = 00000000 = 0 .. /128 = 0 01000000 & 00111010 = 00000000 = 0 .. /64 = 0 00100000 & 00111010 = 00100000 = 32 .. /32 = 1 00010000 & 00111010 = 00010000 = 16 .. /16 = 1 00001000 & 00111010 = 00001000 = 8 .. /8 = 1 00000100 & 00111010 = 00000000 = 0 .. /4 = 0 00000010 & 00111010 = 00000010 = 2 .. /2 = 1 00000001 & 00111010 = 00000000 = 0 .. /1 = 0
    And the numbers down the right hand side are printed out, giving the desired binary represenation.
Re: Bitwise and operator question
by ikegami (Patriarch) on Aug 09, 2007 at 14:47 UTC
    Perl functions such as chomp and int are documented in perlfunc. You can use the perldoc tool to view this documentation offline. (e.g. perldoc -f chomp)