You have to do a bit of bit manipulation here,
$string = '010101010001111';
$decVal = bindec($string);
$variable = 3;
$result = decbin(($decVal >> $variable) | (($decVal << (strlen($string) - $variable)) & ~(~0 << strlen($string))));
echo $result;
Live Demo: https://3v4l.org/lSCLq
The overall idea is like this:
-
Convert binary string to decimal value.
$decVal = bindec($string); // 010101010001111 --> 10895
-
Right shift $decVal
value by $variable
positions.
$decVal >> $variable // 010101010001111 --> 000010101010001
-
Left shift $decVal
value by strlen($string) - $variable
positions
$decVal << (strlen($string) - $variable) // 010101010001111 --> 010101010001111000000000000
-
AND
the output of step# 3 with ...0000000000111111111111111
(($decVal << (strlen($string) - $variable)) & ~(~0 << strlen($string))) // 010101010001111000000000000 & ...0000000000111111111111111 --> ...0000000000111000000000000
-
OR
the output of step# 2 and step# 4 to achieve the desired result
($decVal >> $variable) | (($decVal << (strlen($string) - $variable)) & ~(($decVal | ~0) << strlen($string))) // 000010101010001 | ...0000000000111000000000000--> 111010101010001
-
Finally, display the output of step# 5 in binary string representation.
$result = decbin(...);
echo $result;