Expanding shift register code

This is bitwise OR operator

To make it easier to understand, this for loop :

uint32_t optionSwitch = 0;
for ( int i = 24; i >= 0; i -= 8)
{
  optionSwitch |= ((uint32_t) ReadOne165()) << i;
}

Could be written in a single line, like this :

optionSwitch = ( ((uint32_t) ReadOne165()) << 24 ) | ( ((uint32_t) ReadOne165()) << 16 ) | ( ((uint32_t) ReadOne165()) << 8 ) | ( ((uint32_t) ReadOne165()) )

It reads 4 bytes from the shift registers and store each byte into optionSwitch which can hold 4 bytes (32 bits)

As an example, suppose ReadOne165() always gives result 11011011 (binary)

  11011011 << 24
= 11011011000000000000000000000000
| 11011011 << 16
= 11011011110110110000000000000000
| 11011011 << 8
= 11011011110110111101101100000000
| 11011011
= 11011011110110111101101111011011
1 Like