user33682: Could I have some help getting the following code work?
My purpose is to turn on 4(or N) LEDs in a binary sequence with 4 (or N) bits, A,B,C,D. When bit=1, LED on, otherwise LED off,...
I will have 16 combinations, 2^4=16, so:

0000 => All leds off 
0001 => All leds off except the one on the right
...

Here's the code I came up with:
int LEDblu=4;
int LEDgiallo=2;
int LEDrosso=5;
int LEDverde=7;
int num_led;
String numeri="";
String output;
int i=0;
int p;

void setup() {
    // put your setup code here, to run once:
    pinMode(LEDblu,OUTPUT);
    pinMode(LEDgiallo,OUTPUT);
    pinMode(LEDrosso,OUTPUT);
    pinMode(LEDverde,OUTPUT);

    Serial.begin(9600);

    if(Serial.available())                //Dimmi numero LED
    {
        num_led=Serial.readString().toInt();
        Serial.print(num_led);
    }
}

void loop() {
    // put your main code here, to run repeatedly:
    int vect[num_led];                                  
    for(int i=num_led-1, c=0; i>=0; i--, c++)
    {
        int num=2^i;
        vect[c]=num;
    }

    output="";

    if(i<2^num_led)
    {
        p=i;

        for(int c=0; c<num_led; c++)     //Convert binary to decimal             
        {
            if(p>=vect[c])
            {
                output+="1";
                int d=vect[c];
                p-=d;
            }
            else
                output+="0";
        }

        Serial.println(output);

        if(output[0]=="1")
            digitalWrite(LEDrosso,HIGH);
        else
            digitalWrite(LEDrosso,LOW);

        if(output[1]=="1")
            digitalWrite(LEDverde,HIGH);
        else
            digitalWrite(LEDverde,LOW);

        if(output[2]=="1")
            digitalWrite(LEDgiallo,HIGH);
        else
            digitalWrite(LEDgiallo,LOW);

        if(output[3]=="1")
            digitalWrite(LEDblu,HIGH);
        else
            digitalWrite(LEDblu,LOW);
    }

    delay(1000);
    i++;    
}


user3917: I don't see any specific initialization of num_led; it should be set to some appropriate value, like 4.
As noted in some comments, the ^ operator is bitwise exclusive-Or.  For example, 2^2 is 0, 2^4 is 6, 2^7 is 5, etc.
To compute the nth power of 2, say 1<<n.
Your convert-binary-to-decimal code is clunky and unnecessary.  Instead, do something like the following, which repeatedly strips off the least significant bit of t:
t = p;
digitalWrite(LEDrosso,t&1);   t>>=1;
digitalWrite(LEDverde,t&1);   t>>=1;
digitalWrite(LEDgiallo,t&1);  t>>=1;
digitalWrite(LEDblu,t&1);

If you want the blue LED instead of the red one to represent the least significant bit, reverse the order of color names in the digitalWrite statements from rosso, verde, giallo, blu to blu, giallo, verde, rosso.
To print data out in binary, use the Streaming.h library, and say, for example,
Serial << _BIN(p) << endl;


user7508: Note that this is one of those situations where direct port access
just makes things easier. You did not specify what board you are using,
and I will assume you have some AVR-based board. On those boards, I/O
pins come in groups called ports, with up to eight pins per port. You
can update all the pins of a port at once by writing a number to the
port output register. And guess what? The number you write comes out the
pins in binary, jut like everything works in binary inside the
microcontroller.
For example, on an Arduino Uno, you could wire your LEDs to the first
four pins of port B: i.e. pins 8 (lest significant bit) through 11 (most
significant bit). Then you can output a number to the LEDs just by
writing PORTB = number, and your loop would become:
void loop()
{
    static uint8_t number;
    PORTB = number;              // output to pins 8..11
    Serial.println(number, 2);   // print the value in binary
    number = (number + 1) % 16;  // increment modulo 16
    delay(1000);
}


33682: <p>Could I have some help getting the following code work?</p>

<p>My purpose is to turn on 4(or N) LEDs in a binary sequence with 4 (or N) bits, A,B,C,D. When bit=1, LED on, otherwise LED off,...</p>

<p>I will have 16 combinations, 2^4=16, so:</p>

<ul>
<li>0000 => All leds off </li>
<li>0001 => All leds off except the one on the right</li>
<li>...</li>
</ul>

<p>Here's the code I came up with:</p>

<pre><code>int LEDblu=4;
int LEDgiallo=2;
int LEDrosso=5;
int LEDverde=7;
int num_led;
String numeri="";
String output;
int i=0;
int p;

void setup() {
    // put your setup code here, to run once:
    pinMode(LEDblu,OUTPUT);
    pinMode(LEDgiallo,OUTPUT);
    pinMode(LEDrosso,OUTPUT);
    pinMode(LEDverde,OUTPUT);

    Serial.begin(9600);

    if(Serial.available())                //Dimmi numero LED
    {
        num_led=Serial.readString().toInt();
        Serial.print(num_led);
    }
}

void loop() {
    // put your main code here, to run repeatedly:
    int vect[num_led];                                  
    for(int i=num_led-1, c=0; i&gt;=0; i--, c++)
    {
        int num=2^i;
        vect[c]=num;
    }

    output="";

    if(i&lt;2^num_led)
    {
        p=i;

        for(int c=0; c&lt;num_led; c++)     //Convert binary to decimal             
        {
            if(p&gt;=vect[c])
            {
                output+="1";
                int d=vect[c];
                p-=d;
            }
            else
                output+="0";
        }

        Serial.println(output);

        if(output[0]=="1")
            digitalWrite(LEDrosso,HIGH);
        else
            digitalWrite(LEDrosso,LOW);

        if(output[1]=="1")
            digitalWrite(LEDverde,HIGH);
        else
            digitalWrite(LEDverde,LOW);

        if(output[2]=="1")
            digitalWrite(LEDgiallo,HIGH);
        else
            digitalWrite(LEDgiallo,LOW);

        if(output[3]=="1")
            digitalWrite(LEDblu,HIGH);
        else
            digitalWrite(LEDblu,LOW);
    }

    delay(1000);
    i++;    
}
</code></pre>
 3917: <p>I don't see any specific initialization of <code>num_led</code>; it should be set to some appropriate value, like 4.</p>

<p>As noted in some comments, the <code>^</code> operator is bitwise exclusive-Or.  For example, 2^2 is 0, 2^4 is 6, 2^7 is 5, etc.</p>

<p>To compute the n<sup>th</sup> power of 2, say <code>1&lt;&lt;n</code>.</p>

<p>Your convert-binary-to-decimal code is clunky and unnecessary.  Instead, do something like the following, which repeatedly strips off the least significant bit of <code>t</code>:</p>

<pre><code>t = p;
digitalWrite(LEDrosso,t&amp;1);   t&gt;&gt;=1;
digitalWrite(LEDverde,t&amp;1);   t&gt;&gt;=1;
digitalWrite(LEDgiallo,t&amp;1);  t&gt;&gt;=1;
digitalWrite(LEDblu,t&amp;1);
</code></pre>

<p>If you want the blue LED instead of the red one to represent the least significant bit, reverse the order of color names in the <code>digitalWrite</code> statements from rosso, verde, giallo, blu to blu, giallo, verde, rosso.</p>

<p>To print data out in binary, use the <a href="http://arduiniana.org/libraries/streaming/" rel="nofollow noreferrer"><code>Streaming.h</code></a> library, and say, for example,</p>

<pre><code>Serial &lt;&lt; _BIN(p) &lt;&lt; endl;
</code></pre>
 7508: <p>Note that this is one of those situations where <a href="https://www.arduino.cc/en/Reference/PortManipulation" rel="nofollow noreferrer">direct port access</a>
just makes things easier. You did not specify what board you are using,
and I will assume you have some AVR-based board. On those boards, I/O
pins come in groups called <em>ports</em>, with up to eight pins per port. You
can update all the pins of a port at once by writing a number to the
port output register. And guess what? The number you write comes out the
pins in binary, jut like everything works in binary inside the
microcontroller.</p>

<p>For example, on an Arduino Uno, you could wire your LEDs to the first
four pins of port B: i.e. pins 8 (lest significant bit) through 11 (most
significant bit). Then you can output a number to the LEDs just by
writing <code>PORTB = number</code>, and your loop would become:</p>

<pre class="lang-c++ prettyprint-override"><code>void loop()
{
    static uint8_t number;
    PORTB = number;              // output to pins 8..11
    Serial.println(number, 2);   // print the value in binary
    number = (number + 1) % 16;  // increment modulo 16
    delay(1000);
}
</code></pre>
