testbitshift.php - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of testbitshift.php.php

  1. <?php
  2. $val = 4;
  3. $places = 3;
  4. $res = $val << $places;
  5. p($res, $val, '<<', $places, 'sign bits get shifted out');
  6.  
  7.  
  8. function p($res, $val, $op, $places, $note = '') {
  9.     $format = '%0' . (PHP_INT_SIZE * 8) . "b\n";
  10.  
  11.     printf("Expression: %d = %d %s %d\n", $res, $val, $op, $places);
  12.  
  13.     echo " Decimal:\n";
  14.     printf("  val=%d\n", $val);
  15.     printf("  res=%d\n", $res);
  16.  
  17.     echo " Binary:\n";
  18.     printf('  val=' . $format, $val);
  19.     printf('  res=' . $format, $res);
  20.  
  21.     if ($note) {
  22.         echo " NOTE: $note\n";
  23.     }
  24.  
  25.     echo "\n";
  26. }
  27.  
  28. ?>
File Description
  • testbitshift.php
  • PHP Code
  • 06 Feb-2020
  • 549 Bytes
You can Share it: