Created
September 8, 2019 04:13
-
-
Save jasonrm/27e25f84a31ffc506c4daab47241f208 to your computer and use it in GitHub Desktop.
Filp a random bit in a string with PHP
This file contains hidden or 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
<?php | |
function flip_random_bit(string $str) | |
{ | |
$charIndex = random_int(0, strlen($str) - 1); | |
$bitIndex = random_int(0, 8); | |
$str[$charIndex] = chr(ord($str[$charIndex]) ^ 1 << $bitIndex); | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment