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

I am attempting to implement the Marching Squares algorithm in perl. One of the steps involves building a binary index composed from the four corners of a cell:
Compose the 4 bits at the corners of the cell to build a binary index: walk around the cell in a clockwise direction appending the bit to the index, using bitwise OR and left-shift, from most significant bit at the top left, to least significant bit at the bottom left. The resulting 4-bit index can have 16 possible values in the range 0-15.
I can see in the docs that the relevant operators are "|" and "<<" but I've no idea how to apply them. If I have four corners with the values 1,0,0,0 how would I go about building the index - for these values the result should be 7.

As an aside, if anyone can point me to a perl implementation of this algorithm that would be most useful.

Thanks for any help.

Replies are listed 'Best First'.
Re: Bitwise operations
by BrowserUk (Patriarch) on Jan 08, 2014 at 19:44 UTC
    If I have four corners with the values 1,0,0,0 how would I go about building the index - for these values the result should be 7.

    Erm. Binary 1000 is 8 not 7. Unless you invert the bits, but I don't see any call for that in a quick scan of the page you linked.

    If you have an 4 element array containing the 1s & 0s, you could build the numeric value this way:

    @b = qw[ 1 0 0 0 ];; $n = 0; $n <<= 1, $n |= $_ for @b; print $n;; 8 @b = qw[ 0 1 1 1 ];; $n = 0; $n <<= 1, $n |= $_ for @b; print $n;; 7

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi BrowserUK, thanks for your reply. The Wikipedia page is confusing me more now - if you have a chance to look at it again you will see in the second grid diagram with the green lines each cell has a number which I believe corresponds to the binary index of the 4 corners.

      The top row has cells with the corners
      0 0 1 0
      0 0 1 1
      0 0 1 1
      0 0 0 1
      and the values in the diagram are 13,12,12,14

      But when I apply the bitwise operations I get 2,3,3,1

      If I flip the values of the corners before applying the operation I get the correct results, so maybe the algorithm is missing a step or, more likely, I am completely misreading it.

        If I flip the values of the corners before applying the operation I get the correct results, so maybe the algorithm is missing a step or, more likely, I am completely misreading it.

        I see the problem. I didn't write the article :) (It is unfortunately quite typical.)

        (Perhaps the explanation is the annotation that reads: "Give every cell a number based on which corners are true/false.")

        You can 'flip' the values after you've constructed them using:

        @b = qw[ 1 0 0 0 ];; $n = 0; $n <<= 1, $n |= $_ for @b; $n = ~$n & 0xf; ## Additional step to bitwise-not +and mask (flip) values. print $n;; 7

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Bitwise operations
by SuicideJunkie (Vicar) on Jan 08, 2014 at 20:05 UTC

    Easiest way to see what it does is to do it with lots of print statements.

    use strict; use warnings; my $val = 0; p('Zero'); $val |= 1; p('Or-ed 1'); $val <<= 1; p('shift 1'); $val |= 0; p('Or-ed 0'); $val <<= 1; p('shift 1'); $val |= 1; p('Or-ed 1'); $val <<= 1; p('shift 1'); $val |= 0; p('Or-ed 0'); #Prints in binary with lowest bit to the right side. sub p { printf '%20s : %4d => ', shift, $val; print reverse split '', unpack'b*', pack'c', $val; print "\n"; }
    C:\>perl test.pl Zero : 0 => 00000000 Or-ed 1 : 1 => 00000001 shift 1 : 2 => 00000010 Or-ed 0 : 2 => 00000010 shift 1 : 4 => 00000100 Or-ed 1 : 5 => 00000101 shift 1 : 10 => 00001010 Or-ed 0 : 10 => 00001010
      See also: %b
      #Prints in binary with lowest bit to the right side. sub p { printf "%20s : %4d => %08b\n", shift, $val, $val; }
Re: Bitwise operations
by hdb (Monsignor) on Jan 08, 2014 at 19:54 UTC

    Looking at the Wikipedia page it should be straightforward. That's what I thought until I saw your example. Why should the result be 7? Should it not be 8? I would think you should do

    my @corners = ( 1, 0, 0, 0 ); my $index = 0; ( $index <<= 1 ) |= $_ for @corners; print "$index\n";

    which is nothing but translating binary 1000 into decimal (and which can be achieved in a zillion ways in Perl...