1. OCAU Merchandise is available! Check out our 20th Anniversary Mugs, Classic Logo Shirts and much more! Discussion in this thread.
    Dismiss Notice

maths programming problem

Discussion in 'Programming & Software Development' started by insane, Aug 14, 2002.

(追記) (追記ここまで)
  1. insane Member

    Joined:
    Jun 27, 2001
    Messages:
    137
    Location:
    brisbane
    hope this is the right place to put this.

    i'm writing some software, and i have encountered a little maths problem that slowing me down atm.


    i have a field that is like a value in chmod or user level in HL adminmod, where a single number stores multiple states. the states are stored in the number as 2 to the power of the number option required.

    eg.

    2 = state 1
    4 = state 2
    8 = state 3
    16 = state 4
    32 = state 5
    ...
    2^x = state x


    to store the state 2 and 4 the single number whould equal 4 + 16 = 20

    later in the system, i need to recover what states where selected. This can be done by trying to subtract the greatest possible option first, and continue down until all values for x in 2^x are covered. And the set of x's that where 2^x could be subtracted successfully from the stored number, are the states that where selected.

    But i was wondering if there is a shortcut to this, to find if a particular state x (2^x value) is or isnt stored the single number.

    i have hunches that y ^ 1/x (where y=single number, x=state) but i become lost when the single number contains a sum of multiple 2^x. logs or something else must surely help somehow :(

    I have tried searching for an answer on the web, and finding it hard to isolate.

    any help would be great.


    i easily muddle myself in maths if i dont stay in regular practice (which i rarely do).
  2. mpot Member

    Joined:
    Jun 27, 2001
    Messages:
    5,381
    Location:
    Perth, WA
    The easiest way to retrieve the values is just to mask them with an AND:

    state 1 = value AND 2
    state 2 = value AND 4
    state 3 = value AND 8
    etc

    You didn't mention what language you're using, so you'll probably have to translate as appropriate.

    Cheers,
    Martin.
  3. mpot Member

    Joined:
    Jun 27, 2001
    Messages:
    5,381
    Location:
    Perth, WA
    Not quite....the Graphics and Programming forum is the appropriate place, so I'm moving it to there...

    Cheers,
    Martin.
  4. Tanus Member

    Joined:
    Jun 27, 2001
    Messages:
    923
    Location:
    Melbourne
    An elegant way to do this would be to use a bitmask for the state value.

    Code:
    int state1 = 0, state2 = 0, states=0;
    state1 |= (1 << 2);
    state2 |= (1 << 4);
    states = state1 | state2;
    printf("Mask = %d\n", states);
    
    So, that means that state1 is equal to 1 bitshifted left 2 places (or in binary, 0010) which is 4, state2 is bitshifted left 4 places (1000) which is 16, and state3 is the logical OR of those two (1010) which is 20. Now all you have to do to check what values you have in there is to do what mpot stated above.

    Code:
    if(states & state1) 
     //do some other code
    
    The word state was used way too much in that post.
  5. OP
    OP
    insane

    insane Member

    Joined:
    Jun 27, 2001
    Messages:
    137
    Location:
    brisbane
    thanks, that is prefect!

    ((im writing it in perl, saving to mysql (through dbi). and php will be the interface to the user. ))


    yeah, sorry i wrote it at about 4am i think.

    was trying to be clear between refering a state (or option) that was to be stored in the target number, and not to mixed them up in my explaination.


    thanks again.

Share This Page

Advertisement:
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /