[SOLVED] Numbers to score and identify a situation

ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.

Exclusive for LQ members, get up to 45% off per month. Click here for more info.

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:

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.

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:

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.

As already pointed out, what you want to do requires base 8 (octal) or base 2 (binary) math...you cant do it in base 10 (decimal), which all of the examples in your OP are.

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:

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.

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.

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.

Last edited by Michael Uplawski; 05-01-2021 at 06:29 AM.
Reason: corrected 8 against 5 (I am dumb) / Clearer and code-tags

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:

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:

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

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.