LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Numbers to score and identify a situation (https://www.linuxquestions.org/questions/programming-9/numbers-to-score-and-identify-a-situation-4175694546/)

lucmove 04-30-2021 04:42 PM

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

michaelk 04-30-2021 05:17 PM

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...

rkelsen 04-30-2021 06:36 PM

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.

scasey 04-30-2021 06:57 PM

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.

astrogeek 04-30-2021 07:43 PM

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
... from which combinations you can produce unique values such as:

Code:

0000 1001 = 8 + 1 = 9
0101 0101 = 64 + 16 + 4 + 1 = 85

Assign what meaning you will to each bit, then each combination is uniquely represented by the numeric value, which is why computing exists as we know it!

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.

lucmove 04-30-2021 08:32 PM

Quote:

Originally Posted by astrogeek (Post 6246732)
Code:

1111 1111 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255

This is an excellent solution. Very different from what I was expecting (I found 2 3 6 10 20 32 46), but very effective, efficient and grounded on math. Also very easy to remember, extend, read and decode.

Thank you!!!

Michael Uplawski 05-01-2021 06:18 AM

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*/
SOMETHING_2 = 1 << 1 /*same as 2*/
ERROR_1 = 1 << 2 /*same as 4*/
ERROR_2 = 1 << 3 /*same as 8*/

This may look like overhead, it is though quite comfortable to program. In C-like languages you can benefit from constructs that work well with consecutive integers and however .., when needed, push them together into 1 single variable, describing a complete, complex situation:

Code:

CURRENT = SOMETHING_2 + ERROR_1
You have only 1 Integer to communicate and still convey 2 distinct facts.

Edit: Test the presence of something in this kind of variable with binary '&' like in

Code:

(CURRENT & ERROR_2) != 0 /* FALSE */
However.., when I did this in Java, the “Beans-people” could not follow. :(

NevemTeve 05-01-2021 07:49 AM

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)

lucmove 05-01-2021 08:26 PM

Quote:

Originally Posted by NevemTeve (Post 6246873)
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)

Looks very complicated. And how do you decode it?

NevemTeve 05-01-2021 11:46 PM

Code:

tmp := N                # tmp=64935261140

sec := tmp mod 60      # sec=20
tmp := tmp/60          # tmp=1082254352

min := tmp mod 60      # min=32
tmp := tmp/60          # tmp=18037572

hour := tmp mod 24      # hour=12
tmp := tmp/24          # tmp=751565

day := (tmp mod 31)+1  # day=2
tmp := tmp/31          # tmp=24244

month := (tmp mod 12)+1 # month=5
year := tmp/12          # year=2021
                        # so it is 2021-05-02 12:32:20

Edit: Big thank you to Shruggy for explaining the details; now I've added a numeric example.

lucmove 05-02-2021 03:52 AM

I don't understand that at all.
What are tmp and mod?
How come min and sec are the same?

shruggy 05-02-2021 04:25 AM

tmp is just a name of a variable that holds an interim, temporary value when doing calculations.

mod means modulo, i.e. the remainder of a division.

1 hour = 60 min, 1 min = 60 sec: they are in the same relation to each other.

BTW, have you ever heard of RADIX 50 or similar character encodings?

Michael Uplawski 05-03-2021 01:00 AM

Quote:

Originally Posted by NevemTeve (Post 6247100)
Code:

# so it is 2021-05-02 12:32:20

Sometimes you scare me... ;)

NevemTeve 05-03-2021 01:48 AM

(Mind you, with the year there is an off-by-one error.)

pan64 05-03-2021 02:10 AM

Quote:

Originally Posted by lucmove (Post 6246745)
This is an excellent solution. Very different from what I was expecting (I found 2 3 6 10 20 32 46), but very effective, efficient and grounded on math. Also very easy to remember, extend, read and decode.

Thank you!!!

I guess you can open a calculator on your desktop (like gnome-calculator in programming mode) and you can check how this binary<->octal<->decimal<->hexadecimal conversion works. Or you can try it here too: https://www.rapidtables.com/convert/...converter.html


All times are GMT -5. The time now is 07:08 PM.