Need some1 to help with mathmatics.



  •  I hv list of fetures to implement in code. List look like this.

     

     

    <colgroup><col style="mso-width-source:userset;mso-width-alt:2706;width:56pt" width="74">

    <col style="mso-width-source:userset;mso-width-alt:5339;width:110pt" width="146"> </colgroup>
    FeatureId FeatureName
    0 None
    1 A/C
    2 Multiple CD Changer
    4 Satellite Radio
    8 Chrome Wheels
    16 Inbuilt DVD
    32 Roof Rack
    64 Leather Interior
    128 Mp3 Player
    256 IPOD Connectivity
    512 Navigation System
    1024 Bluetooth
    2048 On Star
    4096 Hybrid
    8192 Remote Starter Kit

    In database table if someone check two fetures it store adition of fetureId values. So how to translete the values to 2 fetures?

     

































     



  • To store them in one integer, you add the different values. Say:
    A/C (1) + Roof rack (32) = 33

    To decode them:
    33 && 1 = true (A/C)
    33 && 32 = true (Roof rack)

    The same is/was done for Windows mouse button events. All buttons would be encoded in one byte, and you simply AND the byte with the different button constants (left button = 1, right button = 2, middle button = 3 etc) to find out which combination of buttons was being pressed.



  • C#

    [Flags]

    public enum CarFeatures

    {

      AC = 1,

      MultipleCDChanger = 2,

      SatelliteRadio = 4,

      ChromeWheels = 8

    }

    CarFeatures currentFeatures = MultipleCDChanger | SatelliteRadio;

    currentFeatures.HasValue(SatelliteRadio); //true

    currentFeatures.HasValue(ChromeWheels); //false

     

    Or if you're using a lesser language, go with what stapler said, or write functions.

     

    bool HasValue(enumType actualValue, enumType targetValue)

    {

      return actualValue & targetValue == targetValue;

    }

     

    Although he used && instead of &, which in most languages does not do a bitwise-or, which is what you want.

    (No preview, let's hope html works) edit: Nope



  •  Actualy, my problem is that given value of 36, i must decifer it to Roof Rack and Satleite Radio.



  • Hey, Nagesh, welcome back.
    Haven't seen you in a while.

    BTW, a fake Nagesh has been doing a very bad imitation of you in the frontpage comments.



  •  @Nagesh said:

     Actualy, my problem is that given value of 36, i must decifer it to Roof Rack and Satleite Radio.

    It's all to do with how each option is represented by a single bit in the binary conversion of the decimal number. 

    If we just use the options up to and including "128 MP3 Player" (to limit the binary number in our example to 8 bits) and use your example of '36' being the value in the database.

    36 represented as 8-bit binary is 00100010 because it's made up of...

    128   64   32   16   8   4   2   1
      0     0     1     0    0   1   0   0

    You can see that 36 is made of a '32' and '4' but none of the other numbers.

    To see which options have been selected you need to perform a bitwise AND on the number from the database and each power of 2...

    36 AND 128 = 0
    36 AND 64 = 0
    36 AND 32 = 1
    36 AND 16 = 0
    36 AND 8 = 0
    36 AND 4 = 1
    36 AND 2 = 0
    36 AND 1 = 0

    SeeWikipedia for an explanation of bitwise operations.




  • For languages that have no binary operators there's another way, too.

    You start with the highest defined constant (8192,
    "Remote Starter Kit") and go to the lowest. If the integer value is bigger than the current constant, you subtract the constant, and remember this piece as being present.

     Eg.

    if (FeatureId >= 8192) {
    FeatureId -= 8192;
    I_Have_a("Remote Starter Kit");
    }
    if (FeatureId >= 4096) {
    ....
    }



  • @flop said:

    For languages that have no binary operators
     

    WHY!?



  • @too_many_usernames said:

    @flop said:

    For languages that have no binary operators
     

    WHY!?

    I think he meant "bitwise operators." I don't know of any languages without binary operators, unless you count RPN calculators.

    I'd cite JavaScript as a language that doesn't need bitwise operators, but lo and behold it has them for some reason. That leaves... SQL? I guess? Nope... T-SQL has them too. Goddamned.



  • @Nagesh said:

     Actualy, my problem is that given value of 36, i must decifer it to Roof Rack and Satleite Radio.

    And we showed you how to do that, so you're set!



  • @Sutherlands said:

    @Nagesh said:
    ;Actualy, my problem is that given value of 36, i must decifer it to Roof Rack and Satleite Radio.
    And we showed you how to do that, so you're set!

    I'm usually the last person to pull the "oh programmers were so 1337 back in the day" but this is the ONE situation where my hacking on shitty MUD codebases in C actually paid off. BITFLAGS EVERYWHERE.



  • @AquaDuck said:

    Hey, Nagesh, welcome back.
    Haven't seen you in a while.

    BTW, a fake Nagesh has been doing a very bad imitation of you in the frontpage comments.

    Thx. Aquaduck. I stop making visit to front-page of tdwtf now.



  • @RTapeLoadingError said:

     @Nagesh said:

     Actualy, my problem is that given value of 36, i must decifer it to Roof Rack and Satleite Radio.

    It's all to do with how each option is represented by a single bit in the binary conversion of the decimal number. 

    If we just use the options up to and including "128 MP3 Player" (to limit the binary number in our example to 8 bits) and use your example of '36' being the value in the database.

    36 represented as 8-bit binary is 00100010 because it's made up of...

    128   64   32   16   8   4   2   1
      0     0     1     0    0   1   0   0

    You can see that 36 is made of a '32' and '4' but none of the other numbers.

    To see which options have been selected you need to perform a bitwise AND on the number from the database and each power of 2...

    36 AND 128 = 0
    36 AND 64 = 0
    36 AND 32 = 1
    36 AND 16 = 0
    36 AND 8 = 0
    36 AND 4 = 1
    36 AND 2 = 0
    36 AND 1 = 0

    SeeWikipedia for an explanation of bitwise operations.


    Very good explaning. I like it. Thx.



  • @Nagesh said:

     Actualy, my problem is that given value of 36, i must decifer it to Roof Rack and Satleite Radio.

    In C#, you could use Sutherland's suggestion and then call ToString().Split(',').Trim() on the value.  Then you would have the string representation of each value.



  • Thanks to all who posted. in sql server i am able to doing this with simple qery that uses the pipe | character.

    Now how is this become posible in Orcle Database? I apreciate all help here. Thanks again!



  • Found it.

    Pls mark this thred as resolved.



  •  This thread is resolved.  I haven't updated the Remedy ticket, but it's still resolved.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.