Katie Melosto
Katie Melosto

Reputation: 1177

How does bit shift reverse this unsigned short int

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.

Upvotes: 0

Views: 75

Answers (1)

Adi H
Adi H

Reputation: 738

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)

Upvotes: 1

Related Questions