1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Rep Power

    Bits to byte conversion

    for my project, there is a part where i need to write an inner class called writeBuffer, and it is used to buffer bits, and it does for example, if you want to only write 3 bits at a time, it will store these 3 bits until you have at least 8 bits to write, and then output a byte. Have no idea how to do this at all, any pointers will be great, please and thank you!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Eastern Florida
    Rep Power
    Use the AND operator to isolate the bit, use the shift operator to move the bit to the slot where it goes and the OR operator to set the bit in a byte.
    There would need to be a variable that has the "index" (7-0) for where the next bit should be set. Assumes bits output high to low.

IMN logo majestic logo threadwatch logo seochat tools logo