in reply to Bitwise operations

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.

Replies are listed 'Best First'.
Re^2: Bitwise operations
by bangor (Monk) on Jan 08, 2014 at 22:23 UTC
    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.