Switching from Uno to Mega: warning: left shift count >= width of type

Yes.

40-8 is 32 and shifting a 32-bit number by 32 bits will clear it completely.

41-8 is 33 and shifting a 32-bit number by 33 bits will clear it completely.

You switched processors without understanding the hardware-specific parts of the code. You can't just copy Direct Port Access code to a different processor.

You will need to re-write all parts the reference PORTB or PORTD.

You need to pick three pins that are in the same PORT. You need to know the bit within the PORT (the '-8' trick only works on PORTB on an UNO or Nano for Pins 8 through Pin 13).

1 Like