Brainfuck, 85 bytes
Brainfuck, 85 72 (84) bytes
>,[>-[-----<->]<++[>++++[-<++++>]+<[[<++++>]+<[[+],[[-]]>-<]]]>[<<+[-->]>[<],>-]<]<+.
to return numerically, or
,[>-[-----<->]<++[>++++[-<++++>]+<[[+],[[-]>-<]]]>[<<+[-->]>[<],>-]<]-[-----<+>]<--.
[Online version][1]for ASCII text. > may also be prefixed to avoid memory wrapping.
Loops over the input.
On 1, ends.
On "!", toggles bool a booleanstored as 0 or 255.
On "0", toggles if there is no trailing bit, then ends.
Memory labels | BOOL | INPUT | FLAG |
>, setup first input
[ # loop on INPUT
>-[-----<->]<++ subtract 49 == "1"
[ # case not "1"
>++++[-<++++>] add 16 since 49 take 16 == "!"
+ set FLAG
< move to INPUT
[ # case "0"
[+], clear and new INPUT
[ # trailingcase "0!"
[-]>-< clear INPUT and FLAG
]
]
]
> move to FLAG
[ # case "!" or "0" without tail
<<+[-->]>[<] not the BOOL
, take new INPUTinput
>- clear FLAG
]
< move to INPUT
]
+. return 0 or 1
Or for text response, replace the last line with
-[-----<+>]<--. add 49 for "0" or "1" conversion and return
I think some bytes might be saved by changing to a (not 1)>(not 0) structure to avoid the +16, but have so far found the extra flag bytes needed added too much.