Storing 144 true/false values in mySQL
#1

Hi. I've never used binary or whatever before, it's completely new to me. I get how it works, just not sure how to use it.

I need to save 144 true/false (0/1) variables to mySQL and don't want to have 144 fields. What would be the best way to do that? A string with 0's and 1's? Or some sort of binary number thing?

If I use a string I can just loop through the characters and if string[i] == '1' it's true - just not sure how efficient this is.
Reply
#2

Your string theory took 1ms to execute, so I wouldn't stress too much about efficiency. I'm not a professional or anything, but it's worth trying.

pawn Код:
main() {
    new
        string[144],
        i=0,
        time = GetTickCount();

    while(i != sizeof(string)) {
        switch(random(2)) {
            case 0: {
                strcat(string, "0");
            }
            case 1: {
                strcat(string, "1");
            }
        }
        i++;
    }
    print(string);
    printf("Took %dms to execute", GetTickCount()-time);
}
Reply
#3

A single variable can hold up to 31 bits, but it requires that you assign each bit.

pawn Код:
new bit;
bit+=truefalse*floatround(floatpower(10,n));
'n' ranging from 0-31.
Reply
#4

No idea what you mean, Joe. I need 144, not 31/32.

Thanks Vincent, I'll use that.
Reply
#5

Quote:
Originally Posted by MP2
Посмотреть сообщение
No idea what you mean, Joe. I need 144, not 31/32.

Thanks Vincent, I'll use that.
He mean's 1 variable, can have so many amount of bits usable, but to use them all, I believe he is trying to say you'd have to assign each and everyone of them. But you need 144, not 31 bits.

I don't fully understand either, but I think he means, one variable can have 31 of true/false values.
Reply
#6

Yes I know that, pawn variables are 32-bits. I also need to store it in mySQL though. Strings seem to be the easiest option - so I'll use that. It will not get called often anyway. Every 5 mins or so (when a player's stats are saved).
Reply
#7

Here's what I mean.
pawn Код:
new bits[5]; //31 values per increment
stock SetBit(bit,truefalse)
{
    new cell=bit/31;
    new adj=bit%31;
    new value=floatround(floatpower(10,adj));
    if(!truefalse)if(bits[cell]&value)bits[cell]-=value;
    else if(!(bits[cell]&value))bits[cell]+=value;
}
stock GetBit(bit)
{
    new cell=bit/31;
    new value=floatround(floatpower(10,bit%31));
    return (bits[cell]&value);
}
This would allow you to save 155 bits into only 5 32-bit variables (20 bytes) versus 144 32-bit variables (576 bytes)

To save it, you would just save either bit[0] as an integer, or the whole thing as a string.


The reason I choose 31 bits over 32, is because when dealing with '0b10000000000000000000000000000000' or '0x80000000'. It returns "--" in a print log and sometimes something else.
try.
pawn Код:
printf("%d",0b10000000000000000000000000000000); //32 bits
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)