I'm building a gallery. Each image is a row with name and src and order etc.
I want to have 3 more options per image: Featured, Visible, Disabled.
Should I have those as columns?
Or have just 1 column settings and store a 3 binary numbers?
For example:
111 = Featured, Visible, Disabled
110 = Featured, Visible
010 = Visible
001 = Disabled
Or I can even convert that to DEC and simply store a 0 to 7 (like CHMOD style)
What is the best way to do this?
Thanks!
depends. are only programmers with pocket-protectors looking at data and app code? i am all for complexity but i would say spring for a little overhead. also, despite valiant efforts, i have failed to do bit-wise searching in mysql
Problems with bit-wise searching, see this.
If you are asking how to store/manipulate in MySQL...
SET ('Featured', 'Visible', 'Disabled')
TINYINT UNSIGNED and do bit arithmetic; see 1 << 2 give you 4; 0x4 is a way to express literal 4 in hex; a | b is bitwise OR, etc.
Related
Would it be beneficial for my performance/storage space to save related preferences (all booleans) in an integer as if each preference is one bit of the correspondent binary value of that preference? If so, significantly?
I have a bunch of user preferences of boolean type on my website that are related to each other, so I thought it might be beneficial to group them all into one int, as if each preference was a bit of the number, like so:
if I have 5 preferences, each one would have a power of 2 worth from 2^0 til 2^4. If the preference is true I add that value to the int column on the database and if it's not I don't add anything
first preference value: 1
second preference value: 2
third preference value: 4
fourth preference value: 8
fifth preference value: 16
if only first, third and fourth preferences are activated, my total sum would be 13 (1+4+8), and I could check if each one is there by checking
(total_sum % 2^(i)) >= 2^(i-1)
where i is for the i-th preference.
for the example:
13 % 2^(1) >= 2^(1-1) -> 13%2 >= 1 -> True, so 1st preference is on
13 % 2^(2) >= 2^(2-1) -> 13%4 >= 2 -> False, so 2nd preference is off
and so on...
The questions I would ask are:
Is disk cost an issue for you? Mostly no. So keep multiple columns.
Are you frequently running queries on the preference from various places? Then do not use complex logic and keep it complex. Also anyone reading the column should know the mapping to bit position.
If you have a big team, over time people will mess these bit positions
Keep things simple. Do not complicate unless its unavoidable.
Researching the possibility of using bitwise comparison to assess what options have been selected out a possible 100 options.
now as an integer the selection of all options would require storage of an integer of 2 to the power of 99 (6E29). way beyond the limit of circa 9E18.
just as with dir permissions ( 1 = read, 2= write, 4 = execute) 1+2+4 = 7 = full access.
I would like to know which of the 100 options have been chosen by the same method.
Any advice/tips much appreciated.
NB storage will be mysql
-- EDIT --
The end goal here is to simplify a check as to what currencies a user can be paid in.
assigning values to currency like so:
Currency OptVal
GBP 1
USD 2
EUR 4
AUD 8
CAD 16
ZAR 32
and so on (there are many many currencies and more will arise through crypto currencies I'm sure)
it would then be convenient to check which currencies a user has using bitwise operators...
so if a user had currency setting of 3 only GBP and USD.
5 GBP & EUR
63 GBP,USD,EUR,AUD,CAD,ZAR
and so on - hope this clarifies the goal.
The issue is to do this in its most simplistic form of storing that integer when you have > 100 currencies. you need a value 2E(n-1) for each option and for large n this number is very large and not storable as an integer (BIGINT Max value is 18446744073709551615)
You want advice. Don't do it this way.
MySQL offers the boolean data type, which is convenient for flags. Each value does occupy one byte, so the storage will be larger than using bits.
MySQL also offers the bit() data type, where you can put together up to 64 bits. You can read about them here.
Using built-in data types is simply the right way to go. They protect your from changes of server, from upgrades on the OS, and the possibility that your application and server have different endian-ness (if you don't know what this is, then you definitely should not be thinking about bit fiddling).
The good news is that there are data types for what you want to do.
Bit of an unusual question, but I have setup a field inside a MySQL table that is of the datatype "DECIMAL(5,2)". As far as I understand this, what I have done is to only allow numbers from -999.99 up to 999.99 to be inserted into this field.
However, when I insert an integer to the value of 26 (which is valid) I am shown this inside the database as 26 not 26.00 - is this normal MySQL behaviour? I (perhaps naively) thought that because I have set the scale to 2, my numbers would always be shown with 2 decimal places?
My question is - do integers inside MySQL "DECIMAL" datatypes always display without any decimals places? Or is this my database manager tool formatting 26.00 to 26 for me?
This may seem like a bit of a weird question but I am still trying to get my head around MySQL DECIMALs. Thanks.
I have found the answer out, it seems my MySQL database manager is helpfully (or not so helpfully) hiding the decimals from me. For anyone who finds this vaguely useful I am using EMS SQL Manager 2010 on Windows 7. Credit due to Cylindric who prompted me to check my tools!
In answer to my original question - yes, even integers inside a "DECIMAL" datatype will display the decimal places. For example:
DECIMAL(3,1)
42 = 42.0
531.2 = 99.9
5 = 5.0
Hope this helps someone somewhere!
Lately, I have been studying 6502 microprocessor and came across the fact that binary and voltage relate. 0 for 0volts and 1 for 5 volts.
Now I just recently learned about endian-ness as well. So trying to learn more about both of these topics I was wondering if someone could explain the relation of binary/voltage and the little or big endian.
If there really isn't a difference because 00000001 would only use 5 volts and 10000000 would only used 5 volts as well. Then I am sorry for asking a useless topic. Now if that is the case, please share some more interesting knowledge about endian-ness, binary
and/or Voltage.
Unfortunately I don't have university experience so i am unsure if this is common knowledge, but thanks for any information that you provide.
They're not very related.
When you have a voltmeter and you read a single bit, a 0-volt would correspond to a 0, and a 5-volt would correspond to a 1. Or you could say "high voltage is 1, and low voltage is 0".
Now, to represent a number, let's simply say that we use powers of 2:
1 = 001
2 = 010
3 = 011
4 = 100
5 = 101
And so on. However, what I just used is little-endian: the end bit (the one on the right) is small, it represents 1 (if it's 1) or 0 (if it's 0), as opposed to the bit on the left (4 if it's 1, 0 if it's 0). If we flipped the order around, that would be big-endian.
You could think of each bit (each 0 or 1) as a different wire with either 0 or 5 volts on it.
I'm going to make a computer in Minecraft. I understand how to build a computer where it can make binary operations but I want the outputs to be displayed as standard integer numbers. How you "convert" the binaries into standard digits? Is there any chart for that? And the digits will be shown like in old calculators; with 7 lines.
--
| |
--
| |
--
In electronics, what you need is called a "binary to binary coded decimal" converter. "Binary coded decimal" is the set of bits needed to produce a number on a 7 segment display. Here's a PDF describing how one of these chips works. Page 3 of the PDF shows the truth table needed to do the conversion as well as a picture of all of the NAND gates that implement it in hardware. You can use the truth table to build the set of boolean expressions needed in your program.
0 = 0
1 = 1
10 = 2
11 = 3
100 = 4
101 = 5
110 = 6
111 = 7
...
Do you see the pattern? Here's the formula:
number = 2^0 * (rightmost digit)
+ 2^1 * (rightmost-but-1 digit
+ 2^2 * (rightmost-but-2 digit) + ...
Maybe what you are looking for is called BCD or Binary Coded Decimal. There is a chart and a karnaugh map for it that has been used for decades. a quick Google search for it gave me this technical page
http://circuitscan.homestead.com/files/digelec/bcdto7seg.htm
How are you trying to build the computer?
Maybe that key word can at least help you find what you need. :)
Your problem has two parts:
Convert a binary number into digits, that is do a binary to BCD conversion.
Convert a digit into a set of segments to activate.
For the latter you can use a table that assigns the bitmap of active segments to each digit.
I think's that's two different questions.
There isn't a "binary string of 0/1" to integer conversion built in - you would normally just write your own to loop over the string and detect each power of 2.
YOu can also write your own 7segment LED display - it's a little tricky because it's on multiple lines, but would be an interesting excersize.
Alternatively most GUIs have an LCD font,Qt certainly does