user7728: I have this code, in which I try to "glue" four bytes in order to construct an unsigned int. I shift left the bytes and "add" them using the OR operator.
unsigned long number1;   
unsigned long number2;
unsigned long number3=0;

byte byte0 = 1;
byte byte1 = 40;
byte byte2 = 107;
byte byte3 = 238;

void setup(){
  Serial.begin(9600);
}

void loop(){
  number1 = byte1 <<8 | byte0 ;  //(=10.241 O.K.)

  //(should be 4.000.000.001, it produces 10.241)
  number2 = byte3 <<24 | byte2 <<16 | byte1 <<8 | byte0;  

  //(should be 4.000.000.001, it produces 4.016.777.217)
  number3 = number3 | byte3;
  number3 = number3<<8;
  number3 = number3 | byte2;
  number3 = number3<<8;
  number3 = number3 | byte1;
  number3 = number3<<8;
  number3 = number3 | byte0;  

  Serial.print("number1 = ");Serial.println(number1);  
  Serial.print("number2 = ");Serial.println(number2);   
  Serial.print("number3 = ");Serial.println(number3);       
  Serial.println(" ");  
}

The problem is that the OR operator applies only to byte1 <<8 | byte0 and ignores the rest. That is number1 and number2 appear to have the same value (10241). 
I also tried "adding" them up separately, as shown with number3, but it also produces a wrong result as well.
I tried the same code in python, and it works fine.
byte3, byte2, byte1, byte0 = 238, 107, 40, 1
print byte1 <<8 | byte0
print byte3 <<24 | byte2 <<16 | byte1 <<8 | byte0

number3 = 0;
number3 = number3 | byte3;
number3 = number3<<8;
number3 = number3 | byte2;
number3 = number3<<8;
number3 = number3 | byte1;
number3 = number3<<8;
number3 = number3 | byte0;
print number3

So, the question is:
Why both number2 and number3 produce a false result?

user23841: Correction from another answer for number2:
number2 = (unsigned long)byte3 <<24 | (unsigned long)byte2 <<16 | byte1 <<8 | byte0; // = 4.000.000.001

And in the first round of loop number3 is correct, all others are wrong as number3 holds a result from previous loop.
  number3 = byte3; // all magic lays here
  number3 <<= 8;
  number3 |= byte2;
  number3 <<= 8;
  number3 |= byte1;
  number3 <<= 8;
  number3 |= byte0;


7728: <p>I have this code, in which I try to "glue" four bytes in order to construct an unsigned int. I shift left the bytes and "add" them using the OR operator.</p>

<pre><code>unsigned long number1;   
unsigned long number2;
unsigned long number3=0;

byte byte0 = 1;
byte byte1 = 40;
byte byte2 = 107;
byte byte3 = 238;

void setup(){
  Serial.begin(9600);
}

void loop(){
  number1 = byte1 &lt;&lt;8 | byte0 ;  //(=10.241 O.K.)

  //(should be 4.000.000.001, it produces 10.241)
  number2 = byte3 &lt;&lt;24 | byte2 &lt;&lt;16 | byte1 &lt;&lt;8 | byte0;  

  //(should be 4.000.000.001, it produces 4.016.777.217)
  number3 = number3 | byte3;
  number3 = number3&lt;&lt;8;
  number3 = number3 | byte2;
  number3 = number3&lt;&lt;8;
  number3 = number3 | byte1;
  number3 = number3&lt;&lt;8;
  number3 = number3 | byte0;  

  Serial.print("number1 = ");Serial.println(number1);  
  Serial.print("number2 = ");Serial.println(number2);   
  Serial.print("number3 = ");Serial.println(number3);       
  Serial.println(" ");  
}
</code></pre>

<p>The problem is that the OR operator applies only to <code>byte1 &lt;&lt;8 | byte0</code> and ignores the rest. That is number1 and number2 appear to have the same value <em>(10241)</em>. </p>

<p>I also tried "adding" them up separately, as shown with number3, but it also produces a wrong result as well.</p>

<p>I tried the same code in python, and it works fine.</p>

<pre><code>byte3, byte2, byte1, byte0 = 238, 107, 40, 1
print byte1 &lt;&lt;8 | byte0
print byte3 &lt;&lt;24 | byte2 &lt;&lt;16 | byte1 &lt;&lt;8 | byte0

number3 = 0;
number3 = number3 | byte3;
number3 = number3&lt;&lt;8;
number3 = number3 | byte2;
number3 = number3&lt;&lt;8;
number3 = number3 | byte1;
number3 = number3&lt;&lt;8;
number3 = number3 | byte0;
print number3
</code></pre>

<p>So, the question is:</p>

<p>Why both number2 and number3 produce a false result?</p>
 23841: <p>Correction from another answer for number2:</p>

<pre><code>number2 = (unsigned long)byte3 &lt;&lt;24 | (unsigned long)byte2 &lt;&lt;16 | byte1 &lt;&lt;8 | byte0; // = 4.000.000.001
</code></pre>

<p>And in the first round of loop number3 is correct, all others are wrong as number3 holds a result from previous loop.</p>

<pre><code>  number3 = byte3; // all magic lays here
  number3 &lt;&lt;= 8;
  number3 |= byte2;
  number3 &lt;&lt;= 8;
  number3 |= byte1;
  number3 &lt;&lt;= 8;
  number3 |= byte0;
</code></pre>
