J'ai un comportement inattendu avec l'opérateur ^ (ou exclusif). Voici ce que me ressortent les var_dump :
Et voici le code incriminé :string '0 : 0 ^ 0 = 0' (length=13)
string '1 : 1 ^ 10 = 1' (length=14)
string '2 : 10 ^ 100 = 11' (length=17)
string '3 : 11 ^ 110 = 10' (length=17)
string '4 : 100 ^ 1000 = 90' (length=19)
string '5 : 101 ^ 1010 = 91' (length=19)
string '6 : 110 ^ 1100 = 105' (length=20)
string '7 : 111 ^ 1110 = 108' (length=20)
string '8 : 1000 ^ 10000 = 946' (length=22)
string '9 : 1001 ^ 10010 = 945' (length=22)
string '10 : 1010 ^ 10100 = 935' (length=23)
string '11 : 1011 ^ 10110 = 935' (length=23)
string '12 : 1100 ^ 11000 = 1195' (length=24)
string '13 : 1101 ^ 11010 = 1211' (length=24)
string '14 : 1110 ^ 11100 = 1204' (length=24)
string '15 : 1111 ^ 11110 = 1208' (length=24)
function codeGray($decimal, $longueur) { // http://fr.wikipedia.org/wiki/Code_de_Gray
$b = intval(decbin($decimal));
$c = intval($b.'0');
$b = intval('0'.$b);
$s = $b^$c;
$s = floor($s/10);
var_dump($decimal.' : '.$b.' ^ '.$c.' = '.$s);
$s = str_pad($s, $longueur, 0, STR_PAD_LEFT);
return $s;
}
Cette fonction prend un decimal, une longueur (de bits) et doit retourner ce decimal en binaire, selon le code de Gray.Vous remarquerez le comportement inattendu à partir du décimal 4.
Merci de m'avoir lu !