1

I have the following Python code snippet and I am trying to convert this to PHP. This actually generates Gray Code.

Python:

x,n=0,1<<int(sys.argv[1])
while n>x:print bin(n+x^x/2)[3:];x+=1

PHP

<?php

fscanf(STDIN, "%d\n", $number);
$x=0; $n=1<<$number

while($n > $x) {
    print decbin($n+$x^$x/2);
    x++;
}

Now, in the Python code there is a code at 2nd line inside the while loop:

bin(n+x^x/2)[3:];

I have converted this line to:

decbin($n+$x^$x/2);

but, what should be this: [3:] in PHP? And am I right for as far as I converted?

  • 2
    It perhaps is http://php.net/substr - Most likely you have not much clue at all what the python code in concrete does? I would start with understanding it first, so it's more easy for you to just look it up in the PHP manual. (Disclaimer: I'm a Python NOOB) – hakre May 25 '14 at 10:00
  • I have edited my question. This python code generates [Gray Code](http://en.wikipedia.org/wiki/Gray_code#Constructing_an_n-bit_Gray_code). – Omar Faruk Sharif May 25 '14 at 10:04
  • So that language construct is for substring access. PHP has strings and string functions, too, they are explained in the PHP manual: http://php.net/string and http://php.net/strings – hakre May 25 '14 at 10:06
  • Please note the title is misleading. You are not asking about the `while` loop. – RandomSeed May 25 '14 at 10:44

0 Answers0