1

so.., the result will print something like:

(line1) 00100

(line2) 00110

(line3) 01100

if you read it from top to bottom: line 1 vs line 2 then there is 1 switch, because in line 1 the 4th number -in this case a 0- , switches to a 1 in line 2

and there are 2 switches from line 2 to line 3 because the 0 in second place and the 1 in 4th place of line change from a resp. 0 to 1 and a 1 to 0

the thing is: there must only be 1 switch from line to the next line... how can i check that? and if it is so, then it may continue, and if it is not the case then it should do something else.

tijntest
  • 109
  • 7

3 Answers3

1

The fastest way, if you have them stored as integers, is to use some bitwise arithmetic.

If there is only one switch, then when you mask the binary values together using xor, the result will have just one 1 in it:

00011101 ^ 01011101 == 01000000

Now, if there's at most one 1 in the result, you can check like this. Suppose you have a number p, and you want to check that it has at most one 1 in its binary representation. You can just check

p & (p-1) == 0

If p has only one 1 set, then p-1 will have all the previous 1 entries set, but these won't collide with the 1 that's set in p. When you bitwise-and them together, you'll end up with zero.

If you want to check whether there's exactly one 1, i.e., you want to reject something that's zero throughout, then you also need to check whether p==0 and reject it first.

Here, I've used ^ for bitwise xor, and & for bitwise and. You'll need to check what you need, depending on the language you're using.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
1

I'm a bit (haha) closer now...

it's something in the neighbourhood of this code:

<?php



function _xor($text,$key){
    for($i=0; $i<strlen($text); $i++){
        $text[$i] = intval($text[$i])^intval($key[$i]);
    }
    return $text;
}

echo  _xor('01100001','01100010');
$new = _xor('01100001','01100010');


$new = str_split($new);
echo array_sum($new);


if ($new > 1) {
 echo " a is bigger than 1 b, begin opnieuw"; } elseif ($new == $new) {
 echo "a is equal to b ";
} else {
echo "a is smaller than b"; 
}
tijntest
  • 109
  • 7
0

If you XOR the two lines together, you will get a binary number with 1s in exactly those places that differ between the numbers. Then you just need to check the number of ones. In particular, you want to check that there is only one, meaning the number is a power of two. And this is a problem that has been solved before.

Community
  • 1
  • 1
CompuChip
  • 9,143
  • 4
  • 24
  • 48
  • How would that look like in php code? the 'solved before' link does not lead to what I am looking for... – tijntest Dec 08 '14 at 13:00
  • you should not see it as a SUM. But read it from top to bottom... if you compare the two lines, then from top to bottom there should be only 1 change (to the total of the whole line, it does not matter where it changes, but only 1 zero to 0 , or 0 to zero may change) – tijntest Dec 08 '14 at 15:14