Home » Php » PHP decbin function and strrev – why?

PHP decbin function and strrev – why?

Posted by: admin February 25, 2020 Leave a comment


Could someone please explain to me why the positions are immediately reduced by 1 and why the string is reversed in the question/answer below. I want to understand why the solution isn’t just to compare the positions on the binary string as given… thank you!

Write a PHP program to check if the bits of the two given positions of a number are same or not.
112 – > 01110000
Test 2nd and 3rd position
Result: True
Test 4th and 5th position
Result: False

Here is the solution: –

     function test_bit_position($num, $pos1, $pos2) {
       $bin_num = strrev(decbin($num));
       if ($bin_num[$pos1] == $bin_num[$pos2]) {
          return "true";
       } else {
       return "false";
     echo test_bit_position(112,5,6)."\n";
How to&Answers: