I know this is a beginner question: This may be something I do not understand with computer architecture or registers, but what is happening when you bit shift and then use the | (inclusive OR) operator? I understand that using OR can turn on bits;
but the following gets a 'bit' confusing for me; for example I have seen code that converts Big endian data to Little endian by
int value = (buffer[i++] << 24) | (buffer[i++] << 16)
| (buffer[i++] << 8) | buffer[i++];
And in my own code I have converted byte streams by doing:
short a = (short)(buffter[0] | (buffer[1] << 8));
But these ORs do not make sense to me, isn't OR used to turn on bits?
0101
1100
Result:1101
If I was actually using OR in the way I understand it, then if byte[i] = 2 and byte[i+1] =3, then
0010 | 0011 = 0011 = 3.
Is the shift making the data in the byte overflow to some wider spot in memory before doing the inclusive OR? i.e.
0010 | 0011 << 4
equivalent to 00000010 | 00110000
?
If this is the case, then how big is too big of a shift since you can't pad 0s forever?