Numbers to score and identify a situation
By "numbers to score and identify a situation" I mean, for example, file permissions. 4, 2, 1, and 0 allow very unique and specific sum combinations that identify each specific situation and can be translated into one single number.
0 (0+0+0) – No permission. 1 (0+0+1) – Only execute permission. 2 (0+2+0) – Only write permission. 3 (0+2+1) – Write and execute permissions. 4 (4+0+0) – Only read permission. 5 (4+0+1) – Read and execute permission. 6 (4+2+0) – Read and write permissions. 7 (4+2+1) – Read, write, and execute permission. Each sum is unique. I don't know if there is a name for that specifically in math. Is there? Anyway, how far can I go and how am I supposed to find other numbers? In my code, zero is not possible because I iterate through a list of conditions and add a value to a variable whenever a condition is true. So I'm using 1, 2, 3, and 7: 1+2=3 1+3=4 1+7=8 2+3=5 2+7=9 3+7=10 (*) 1+2+3=6 1+2+7=10 (*collision!) 2+3+7=12 1+3+7=11 So 7 is not good because it may cause a collision. Thinking "manually," I conclude that I should replace it with 13, merely because it is the smallest number that is not the sum of any two or three of the smaller numbers, i.e. it is out of reach of collisions. But no. 1+2=3 1+3=4 1+13=14 2+3=5 2+13=15 3+13=16 (*) 1+2+3=6 1+2+13=16 (*collision!) 1+3+13=17 2+3+13=18 1+2+3+13=22 OK, I'm learning a lesson here. 7 is not the problem. The real problem is I can't have 1, 2, and 3 because 1+2=3 and that will always be trouble further ahead. One more try: 2, 3, 6, 10: 2+3=5 2+6=8 2+10=12 3+6=9 3+10=13 6+10=16 2+3+6=11 2+3+10=15 2+6+10=18 3+6+10=19 2+3+6+10=21 Good. No collisions. But finding 6 and 10 was tedious. What is the correct logic for this? Is there some way to calculate it? If I need more numbers (and I do), what would the next numbers be? How am I supposed to find them? TIA |
linux permissions "numbering system" is an octal number, i.e. only numbers from 0-7. The 4,2,1 represents the bit value of the permissions as a binary number.
7 octal = 111 binary So RWX means: R = If bit 3 is a 1 then you have read permissions W = If bit 2 is a 1 then you have write permissions X = If bit 1 is a 1 then you have execute permissions. A file with read write permissions would be 110 binary (4+2+0) = 6 octal. You lost me on your calculations... |
Numbers to score and identify a situation
It works for permissions because as you noted, there are exactly 8 possible combinations.
I don't think you could extend it to apply in other circumstances. |
As already pointed out, what you want to do requires base 8 (octal) or base 2 (binary) math...you can’t do it in base 10 (decimal), which all of the examples in your OP are.
|
To point it out for a third time... ;)
This will work to any size number, but if and only if each of the numbers you sum together are unique powers of two. That is what others meant above when they say the numbers must be octal or binary. They must be powers of two, which correspond to the value of successive bits in binary representation. For file permissions the values being octal simply means the largest number is what will fit in the rightmost three bits where the bit values are (from the right) 1, 2 and 4, and the largest sum that you can produce by unique selections from that set is 7, 4+2+1. But you can extend that indefinitely by using more bits. For example, using 8 bits the unique values are: Code:
1111 1111 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 Code:
0000 1001 = 8 + 1 = 9 By the way, one extra advantage of octal such as is used in file permissions is that this allows each single digit, 0-7, to correspond to a single subset of the permission scheme, r-w-x for each of owner, group and other. If you add one more power of two, one more bit, then each subset may be one or two digits long and chaos would ensue! But the math itself (as opposed to the visual representation) works for any number represented as a sum of unique powers of two. |
Quote:
Thank you!!! |
As we are in “programming”, this kind of problem is usually solved with “binary operations” where constants are defined as this
Code:
SOMETHING_1 = 1 << 0 /*same as 1*/ Code:
CURRENT = SOMETHING_2 + ERROR_1 Edit: Test the presence of something in this kind of variable with binary '&' like in Code:
(CURRENT & ERROR_2) != 0 /* FALSE */ |
Note: for non-binary data other multipliers can be used, for example if you have a (year,month,day,hour,min,sec) record, you can identify it with the following number:
Code:
N:=((((((year-1)*12+(month-1))*31+(day-1))*24+hour)*60+min)*60+sec) |
Quote:
|
Code:
tmp := N # tmp=64935261140 |
I don't understand that at all.
What are tmp and mod? How come min and sec are the same? |
|
Quote:
|
(Mind you, with the year there is an off-by-one error.)
|
Quote:
|
All times are GMT -5. The time now is 07:08 PM. |