Thread: How to convert to left justified bits?

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Subsonics View Post
    Having thought this over I think that what is refered to as unused bits would be to the right of 1 only, otherwise I canīt understand how the data could be understood by the recieving end. In this case the value 1 would be encoded as: 00000000 00000000 00100000 (16bit). I have discussed this earlier here perhaps the older solution was the correct one. That involved copying the short to a byte array and performing right shifts to spread out the data over three bytes.
    Based on the specs, I would agree with you. I don't know that you have to use a byte array for the original data really; masking would seem to be the way to go. (Although a byte array for the output would work.)

  2. #17
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I think my confusion came from that I wanīt to encode 12bit samples. Right now I can only assume that itīs encoded just like 16bit but with the highest legal sample value being 4095. I guess that I will have to try and see. :-) I donīt remember exactly how I ended up solving it the last time but, I think masking was used to get the value of the last byte with the the 2 least significant bits.

    If it happens to be encoded into two bytes I think I could use something like this:

    Where 00001111 11111111 will become 01111111 01111100
    Code:
    	UInt16 sample = 0xfff;
    	UInt8   outputBuf[2];
    	sample <<= 3;
    	memcpy(&outputBuf[0], &sample, 2);
    	outputBuf[0] >>= 1;
    Last edited by Subsonics; 02-18-2009 at 04:32 PM.

  3. #18
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Subsonics, based on the spec for MIDI you posted, it looks like you can't just shift 16-bits... Instead you need to split up into groups of 7 bits because the spec calls for the high bit to be zero in each byte. Only 7 bits are used. So not only shifting the data left but spreading it out some as you cross the byte boundaries.

  4. #19
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by nonoob View Post
    Subsonics, based on the spec for MIDI you posted, it looks like you can't just shift 16-bits... Instead you need to split up into groups of 7 bits because the spec calls for the high bit to be zero in each byte. Only 7 bits are used. So not only shifting the data left but spreading it out some as you cross the byte boundaries.
    Iīm not sure what you mean now. In the post above I have done that, the MSB is padded with zero in both bytes. First I shift three bits to the left which leaves me with one leading zero, then I copy the short across two bytes in the output buffer and make a right shift to the last byte. If you refer to the original post then thatīs only one part in solving this that I couldnīt figure out how to do. But I donīt think I will need it now as Iīm pretty sure that the left justifying that is mentioned in the doc only is relevant for the least significant byte.
    Last edited by Subsonics; 02-18-2009 at 07:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. using bitwise & to convert to octal
    By dbzx in forum C Programming
    Replies: 1
    Last Post: 04-28-2009, 02:17 AM
  3. Working with bits and SHA-1
    By Desolation in forum C++ Programming
    Replies: 6
    Last Post: 12-28-2008, 01:34 AM
  4. merging bits
    By Drac in forum C++ Programming
    Replies: 8
    Last Post: 12-21-2008, 06:13 PM
  5. Help Please...bits
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 01-24-2002, 01:43 PM