in reply to Bitwise and operator question

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

Replies are listed 'Best First'.
Re^2: Bitwise and operator question
by citromatik (Curate) on Aug 09, 2007 at 14:48 UTC

    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!
Re^2: Bitwise and operator question
by Severy (Initiate) on Aug 09, 2007 at 14:46 UTC
    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;