mustyoshi
|
  |
 |
| Joined: 27 Dec 2007 |
| Total Posts: 41651 |
|
|
| 10 Aug 2011 07:24 PM |
How do I store different combos in a single byte? Let's say I have five distinct values. How can I use a single byte to store any combo of them? Even all five at once?
~Monica ~dis has ben a publik anouncmen on da fariy brodkastin netwrok |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 10 Aug 2011 07:25 PM |
| One can't be greater than 4 bits. As long as they add up to 8 bits, you can store them all. |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2011 07:44 PM |
I don't know if Lua is capable.
In C... http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c
|
|
|
| Report Abuse |
|
|
mustyoshi
|
  |
 |
| Joined: 27 Dec 2007 |
| Total Posts: 41651 |
|
|
| 10 Aug 2011 07:46 PM |
This is for MySQL and PHP. It's for usergroups on my forum.
~Monica ~dis has ben a publik anouncmen on da fariy brodkastin netwrok |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2011 08:03 PM |
Okay. You'll want to use an UNSIGNED TINYINT as the datatype in MySQL.
Here's some example code.
function combo($flag1 = 0, $flag2 = 0, $flag3 = 0, $flag4 = 0, $flag5 = 0) { $ret = 0; $ret |= $flag1 << 0; $ret |= $flag2 << 1; $ret |= $flag3 << 2; $ret |= $flag4 << 3; $ret |= $flag5 << 4; return $ret; }
echo combo(0, 1, 1, 0, 1);
Yes, PHP has no single byte data type except the character, but that gets converted to an int by the bitwise operations. Just let MySQL handle the truncation. |
|
|
| Report Abuse |
|
|
Emess
|
  |
| Joined: 01 Apr 2010 |
| Total Posts: 13331 |
|
| |
|
| |
|
|
| 10 Aug 2011 08:14 PM |
| If you're concerned about portability then you should do an endian-ness check. |
|
|
| Report Abuse |
|
|
mustyoshi
|
  |
 |
| Joined: 27 Dec 2007 |
| Total Posts: 41651 |
|
|
| 10 Aug 2011 08:56 PM |
It's just gonna be server side, it will send the client the information in text form.
~Monica ~dis has ben a publik anouncmen on da fariy brodkastin netwrok |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2011 09:31 PM |
| If those combos are actually booleans, just convert the binary number to decimal. I would advise that you convert all values to boolean, then string 'em together, and convert to decimal. |
|
|
| Report Abuse |
|
|
mustyoshi
|
  |
 |
| Joined: 27 Dec 2007 |
| Total Posts: 41651 |
|
|
| 10 Aug 2011 09:33 PM |
So each bit represents a value? Cause it is just "Is the user in that group or not"
~Monica ~dis has ben a publik anouncmen on da fariy brodkastin netwrok |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2011 09:35 PM |
You could store 8 bools in a byte. But really, don't do this.
-NecroBumpist, Master of Lua, Writer of Wikis |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2011 09:37 PM |
Yeah, and in this case, they would be booleans.
I've posted toDecimal functions on this site before...
function toDec(t) local x=0; for i,v in pairs(t) do if v then x=x+2^(i-1); end end return x; end
vals = {true, true, false, true, false};
print(toDec(vals)); --> Should be 11
|
|
|
| Report Abuse |
|
|
mustyoshi
|
  |
 |
| Joined: 27 Dec 2007 |
| Total Posts: 41651 |
|
|
| 10 Aug 2011 09:42 PM |
What would you recommend me doing? For efficency and space.
~Monica ~dis has ben a publik anouncmen on da fariy brodkastin netwrok |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2011 09:46 PM |
And you probably want the other way around, too.
function toTable(x) local t = {}; for i = math.floor(math.log(x) / math.log(2)) + 1, 1, -1 do if x>=2^(i - 1) then x = x - 2 ^ (i - 1); t[i] = true; else t[i] = false; end end return t; end print(toTable(11)); --> Well, if you created a decent print function for tables, it would appear something like "true, true, false, true, false" |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2011 09:48 PM |
For a group system it'd be better to do a relational model in your DB.
For example
user_id, group_id 1, 1 1, 2 1, 3 1, 4 1, 5 2, 1
|
|
|
| Report Abuse |
|
|
|
| 10 Aug 2011 09:51 PM |
| Btw, someone should bookmark this thread, because this isn't the first time I've made functions like this. |
|
|
| Report Abuse |
|
|
mustyoshi
|
  |
 |
| Joined: 27 Dec 2007 |
| Total Posts: 41651 |
|
|
| 10 Aug 2011 09:51 PM |
Hmm, that sounds like it would use a lot of rows.
~Monica ~dis has ben a publik anouncmen on da fariy brodkastin netwrok |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 10 Aug 2011 09:55 PM |
We have 1.7 million rows in one table.
You'll live. |
|
|
| Report Abuse |
|
|
mustyoshi
|
  |
 |
| Joined: 27 Dec 2007 |
| Total Posts: 41651 |
|
|
| 10 Aug 2011 09:56 PM |
@NVI Who is we?
~Monica ~dis has ben a publik anouncmen on da fariy brodkastin netwrok |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 10 Aug 2011 09:57 PM |
| we = eyeontheprizeREBOOT + NVI |
|
|
| Report Abuse |
|
|
mustyoshi
|
  |
 |
| Joined: 27 Dec 2007 |
| Total Posts: 41651 |
|
|
| 10 Aug 2011 10:03 PM |
I meaned what site ;)
~Monica ~dis has ben a publik anouncmen on da fariy brodkastin netwrok |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 10 Aug 2011 10:04 PM |
| I obviously can't post it here. You should already know anyway. |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
| |
|
Roundel
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 469 |
|
|
| 10 Aug 2011 10:13 PM |
@blobbyblob
I know this isn't really what the conversation is about, but here's my any radix to any radix converter I just made right now:
function RadixConv(number,radA,radB) number = tonumber(tostring(number),radA) local char,newN,i,sub="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ","",0 while number>0 do i=i+1 number,sub=math.floor(number/radB),number%radB+1 newN=string.sub(char,sub,sub)..newN end return newN end
print(RadixConv("100",10,16))
Numbers can be input as either strings or numbers in base 10 or lower, but they must be strings in base 11+ unless you only use decimal digits. It returns a string of the new radix.
Also; tonumber("number",radix) returns 'number' from base 'radix' in decimal, so your toDecimal function isn't particularly useful. :/
/Roundel's inner obsession with radices |
|
|
| Report Abuse |
|
|