fixes to bit manipluation; complete sort