-3

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?

Goku
  • 1,565
  • 1
  • 29
  • 62
  • Pick a language: bit shifting has all sorts of edge-conditions and language dependencies. – Richard Critten Aug 29 '18 at 14:59
  • This is C#, but I'm interested in all c languages. I'll adjust edit my question. – Goku Aug 29 '18 at 15:00
  • 3
    Probably related, because I do not trust those multiple `i++` : https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Yunnosch Aug 29 '18 at 15:00
  • 1
    You don't have `0010 | 0011`. The shift is extending the value so you have something like `0010 | 00110000` – NathanOliver Aug 29 '18 at 15:00
  • Your code is undefined in C and C++, so best to remove the tags. – Bathsheba Aug 29 '18 at 15:00
  • C++ development was begun on top of C. C# development was surely strongly inspired by C++. However, all these languages are developed independently though there might be influences to each other. So, I wouldn't dare to say something like "all C languages" - as you have seen the community is very critical about this. :-) – Scheff's Cat Aug 29 '18 at 15:04
  • @Scheff gotcha. I would have though bit shifting would behave similarly but I guess I'll have to learn the other languages another time – Goku Aug 29 '18 at 15:06
  • @Goku; they almost do but the far worse problem is that in C and C++, your multiple i++ between non-sequenced steps puts your entire program into an undefined state. – Bathsheba Aug 29 '18 at 15:09
  • @Bathsheba wow. would that mean I would have to store the result of each shift in a new line of code before going to the next? – Goku Aug 29 '18 at 15:12
  • 1
    @Goku, nah, you just use i, i + 1, i + 2, i + 3, then i +=4 as a separate statement. And that's probably better anyway as it's arguably more parallelisable. – Bathsheba Aug 29 '18 at 15:14

1 Answers1

2

Your understanding of how bitwise OR works is correct. If either corresponding bit is set then the resulting bit is set.

When you perform a shift however those bits move. So the binary value 0011 shifted left 4 bits gives you 00110000. If you then OR that with 0010 you get:

  00000010
| 00110000
  --------
  00110010

As far as how big a shift is too big, that depends entirely on the type.

dbush
  • 205,898
  • 23
  • 218
  • 273