Skip to content

Instantly share code, notes, and snippets.

@jasonrm
Created September 8, 2019 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrm/27e25f84a31ffc506c4daab47241f208 to your computer and use it in GitHub Desktop.
Save jasonrm/27e25f84a31ffc506c4daab47241f208 to your computer and use it in GitHub Desktop.
Filp a random bit in a string with PHP
<?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