Last active
March 7, 2019 00:51
-
-
Save yphastos/b107c11f56d70299ff86c69ddff720c6 to your computer and use it in GitHub Desktop.
different Rightmost Bit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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