0

I'm confused about this bitwise operation including the symbol | and how the shift left and shift right work in this example of code to reverse the int below:

uint16_t swap_uint16( uint16_t val ) 
{
    return (val << 8) | (val >> 8 );
}

So what I understand is the << shifts the int left and >> shifts it right. I'm not sure how the | works with these two shift operations.

halfer
  • 19,824
  • 17
  • 99
  • 186
Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
  • Does this answer your question? [What are bitwise shift (bit-shift) operators and how do they work?](https://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work) – phuclv Apr 15 '20 at 01:41
  • [C OR '|' operator](https://stackoverflow.com/q/33536187/995714) – phuclv Apr 15 '20 at 01:41

1 Answers1

1

You didn't specify the language but in most languages | means bit-wise OR so you are most likely looking at OR'ing each respective bit

example random bytes
A: 11001010
B: 00101011
-----------
   11101011  (A and B bit-wise OR'ed)
Adi H
  • 668
  • 7
  • 9