Thread: How to convert to left justified bits?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    How to convert to left justified bits?

    What would be the best solution to convert to left justified bits within a word?
    Letīs say I have a 12bit full value in a short like this:

    00001111 11111111

    it would be converted into:

    11111111 11110000

    I have been trying different bitwise operations but havenīt come up with one that always works regardless of value.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you have to decide what you mean by "left justified". Do you mean you want the first bit to be a one, no matter what, or do you want your smaller-than-sixteen-bit-sized-thing to live at the left rather than the right?

    If the latter, then you would shift 16-size; if the former, you would shift until your number goes negative (assuming you're dealing with a signed datatype) or bigger than 2^(n-1) (if not).

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by Subsonics
    I have been trying different bitwise operations but havenīt come up with one that always works regardless of value.
    shift left by one until the left most bit is set.
    Code:
    unsigned short left_justify(unsigned short x)
    {
        while ((x & 0x8000) == 0) x <<= 1;
        return x;
    }
    Quote Originally Posted by tabstop
    Well, you have to decide what you mean by "left justified".
    he did decide, and explained it perfectly too. you're just overcomplicating things.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by tabstop View Post
    , or do you want your smaller-than-sixteen-bit-sized-thing to live at the left rather than the right?
    Yes, thatīs what Iīm after, any unused bits should be on the right side.

    Quote Originally Posted by tabstop View Post
    If the latter, then you would shift 16-size;
    Ok, that seems solid but how do I get the bit size?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Meldreth View Post
    shift left by one until the left most bit is set.
    Code:
    unsigned short left_justify(unsigned short x)
    {
        while ((x & 0x8000) == 0) x <<= 1;
        return x;
    }
    Thanks for that Meldreth,
    Last edited by Subsonics; 02-18-2009 at 01:48 PM.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    A humble question:

    What could this be good for?

  7. #7
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    A tough question:

    How can you solve this in constant time? ;-)
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    an easy answer:

    don't bother because you can almost always get better results optimizing other things.

  9. #9
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    don't bother because you can almost always get better results optimizing other things.
    For most practical purposes, you are right: optimizing without profiling is stupid. In addition to that, I was completely mistaken: I know a nice and clever way to get the number of trailing zeros in the binary representation of an integer making use of de Bruijn sequences and exploiting features of Two's Complement representation. I'd be happy to show it here if anyone is interested.

    But unfortunately, the trick doesn't apply to leading zeros. It's still possible to do it in constant time, but it looks quite ugly.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    How can you solve this in constant time? ;-)
    do a BSR (bit scan reverse), and shift left by this amount (assuming BSR is constant time).

    Or, in the case of 16-bits, a BSR table can be constructed.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Iīm going to use it for de-coding linear pcm audio data to SDS in preparation for sending it over midi. According to the spec (which is from 86 and sparse), the MSB should be padded and all bits be left justified. I might have misunderstood the docs though because I canīt grasp how the recieving end could interpret a 1 from say 16.

    This is what the doc I found says about it:

    "The packet number is followed by 120 bytes of data, which form
    60, 40, or 30 words (MSB first for multiword samples), depending on
    the length of a single data sample.
    Each data byte hold seven bits, with the msb in each byte set to
    0, in order to conform to the requirements of MIDI data transmission.
    Information is left justified within the 7-bit bytes, and unused bits
    are filled with 0.
    Example: Assume a data point in the memory of a 16-bit sampler,
    with the value 87E5. In binary, that would be

    1000 0111 1110 0101

    and would be encoded as the following MIDI data stream:

    01000011 01111001 00100000"


    The problem here is that the example is a full 16 bit value, Iīm not exactly sure what happens with smaller values right now.

  12. #12
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by cyberfish View Post
    do a BSR (bit scan reverse), and shift left by this amount (assuming BSR is constant time).
    Assuming that the machine supports the BSR instruction (x86 since i386 I think), this is right. And as we've already left portability way behind us: gcc has the builtin function __builtin_clz() which computes the number of leading zeros, too.

    Kids, don't use that! ;-)
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    yeah, __builtin_clz() is probably a better idea, in terms of portability (to different architectures, but still using GCC). GCC will use whatever equivalent instruction the CPU has to implement that function, and provide a software implementation if there is none. That's usually what we want.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Meldreth View Post
    shift left by one until the left most bit is set.
    Code:
    unsigned short left_justify(unsigned short x)
    {
        while ((x & 0x8000) == 0) x <<= 1;
        return x;
    }
    he did decide, and explained it perfectly too. you're just overcomplicating things.
    Given the example below, where the left-most bit is not always set, I'd say that the question seems justified.

    Anyway, based on what I'm reading from your specs, either you have 16-bit, 24-bit, or 32-bit sampling and those are your choices. I don't know whether you can try to cram smaller things in there; that depends on whether the program that's reading from the other side would know what to do with it.

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.
    Last edited by Subsonics; 02-18-2009 at 03:11 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