Skip to content

Instantly share code, notes, and snippets.

@yphastos
Last active March 7, 2019 00:51
Show Gist options
  • Save yphastos/b107c11f56d70299ff86c69ddff720c6 to your computer and use it in GitHub Desktop.
Save yphastos/b107c11f56d70299ff86c69ddff720c6 to your computer and use it in GitHub Desktop.
different Rightmost Bit
// DDD:
function differentRightmostBit($n, $m) {
return call_user_func(function($n,$m){
$na = str_split(strrev(str_pad(decbin($n),32,'0',STR_PAD_LEFT)));
$ma = str_split(strrev(str_pad(decbin($m),32,'0',STR_PAD_LEFT)));
for($i = 0; $i <32; $i++){
if($na[$i] != $ma[$i]){
return pow(2,$i);
}
}
}, $n,$m); ;
}
function differentRightmostBit($n, $m) {
return pow(2, strspn(strrev(sprintf('%032b', $n)) ^ strrev(sprintf('%032b', $m)), "\0")) ;
}
function differentRightmostBit($n, $m) {
return (($n ^$m) &-($n ^$m));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment