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

This is a discussion on How do I get the lower 12 bits of a 32 bit int? within the C++ Programming forums, part of the General Programming Boards category; 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
    1,609

    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?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    It probably depends on how you want it. Perhaps you could use:
    Code:
    std::bitset<12> bits(num);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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,189
    __asm AND val , 0x00000FFF
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    abachler, why use your solution when brewbuck's suggestion works fine (assuming Yarin wants the bits in that way) and is more portable?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    1,609
    >> 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?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    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
    1,609
    Right, okay it's working, thanks.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

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, 07: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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21