Thread: How do I get the lower 12 bits of a 32 bit int?

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158

    How do I get the lower 12 bits of a 32 bit int?

    How do I get the lower 12 bits of a 32 bit int?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It probably depends on how you want it. Perhaps you could use:
    Code:
    std::bitset<12> bits(num);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Yarin View Post
    How do I get the lower 12 bits of a 32 bit int?
    val & 0xFFF

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    __asm AND val , 0x00000FFF

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    abachler, why use your solution when brewbuck's suggestion works fine (assuming Yarin wants the bits in that way) and is more portable?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    >> assuming Yarin wants the bits in that way

    Here is why I'm trying to get the 16 bits.
    The channel handle :
    The return value is reference counted. This stops the user from updating a stolen channel.
    Basically it means the only sound you can change the attributes (ie volume/pan/frequency/3d position) for are the one you specifically called playsound for. If another sound steals that channel, and you keep trying to change its attributes (ie volume/pan/frequency/3d position), it will do nothing.
    This is great if you have sounds being updated from tasks and you just forget about it.
    You can keep updating the sound attributes and if another task steals that channel, your original task wont change the attributes of the new sound!!!
    The lower 12 bits contain the channel number. (yes this means a 4096 channel limit for FMOD
    The upper 19 bits contain the reference count.
    The top 1 bit is the sign bit.
    ie
    S RRRRRRRRRRRRRRRRRRR CCCCCCCCCCCC
    So will 0xFFF work fine in doing that job?

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Yarin View Post
    >> assuming Yarin wants the bits in that way

    Here is why I'm trying to get the 16 bits.

    So will 0xFFF work fine in doing that job?
    Yes.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want to extract a certain number of bits from a string, then the general method is this:
    Open your calculator, switch to binary mode, enter n 1 then convert to decimal or hexdecimal.
    Then simply do a binary AND with the mask on the original number and you have extracted n bits.
    (Binary 1111 1111 1111 is hex 0xFFF)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Right, okay it's working, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Can't figure out why?
    By kwigibo in forum C Programming
    Replies: 10
    Last Post: 10-14-2001, 10:58 PM