I asked a similar question on how to work with Uint8Array and read and write bits from it, and got a wonderful answer showing this as the final result: