helper java
better bit handling code

The transition detection needs some work though