If operator next to current operand is AND .
if ( ( $ i + 1 ) < $ n && $ s [ $ i + 1 ] == ' A ' ) { if ( $ s [ $ i + 2 ] == '0' $ s [ $ i ] == '0' ) $ s [ $ i + 2 ] = '0' ; else $ s [ $ i + 2 ] = '1' ; }