Visit Jeremy's Blog.
 LinuxQuestions.org [SOLVED] Numbers to score and identify a situation
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 04-30-2021, 04:42 PM #1 lucmove Senior Member   Registered: Aug 2005 Location: Brazil Distribution: Debian Stretch Posts: 1,104 Rep: 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 Last edited by lucmove; 04-30-2021 at 04:43 PM.
 04-30-2021, 05:17 PM #2 michaelk Moderator   Registered: Aug 2002 Posts: 21,492 Rep: 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... Last edited by michaelk; 04-30-2021 at 06:36 PM. 3 members found this post helpful.
 04-30-2021, 06:36 PM #3 rkelsen Senior Member   Registered: Sep 2004 Distribution: slackware Posts: 2,936 Blog Entries: 3 Rep: 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. 1 members found this post helpful.
 04-30-2021, 06:57 PM #4 scasey LQ Veteran   Registered: Feb 2013 Location: Tucson, AZ, USA Distribution: CentOS 7.8.2003 Posts: 5,425 Rep: 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. 1 members found this post helpful.
 04-30-2021, 07:43 PM #5 astrogeek Moderator   Registered: Oct 2008 Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1} Posts: 5,616 Blog Entries: 11 Rep: 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. Last edited by astrogeek; 04-30-2021 at 07:57 PM. 1 members found this post helpful.
04-30-2021, 08:32 PM   #6
lucmove
Senior Member

Registered: Aug 2005
Location: Brazil
Distribution: Debian Stretch
Posts: 1,104

Original Poster
Rep:
Quote:
 Originally Posted by astrogeek 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!!!

 05-01-2021, 06:18 AM #7 Michael Uplawski Senior Member   Registered: Dec 2015 Location: Apples Distribution: Apple-selling shops, markets and direct marketing Posts: 1,139 Blog Entries: 29 Rep: 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 2 members found this post helpful.
 05-01-2021, 07:49 AM #8 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 4,170 Blog Entries: 1 Rep: 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)` 1 members found this post helpful.
05-01-2021, 08:26 PM   #9
lucmove
Senior Member

Registered: Aug 2005
Location: Brazil
Distribution: Debian Stretch
Posts: 1,104

Original Poster
Rep:
Quote:
 Originally Posted by NevemTeve 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?

 05-01-2021, 11:46 PM #10 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 4,170 Blog Entries: 1 Rep: 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. Last edited by NevemTeve; 05-02-2021 at 05:42 AM. 3 members found this post helpful.
 05-02-2021, 03:52 AM #11 lucmove Senior Member   Registered: Aug 2005 Location: Brazil Distribution: Debian Stretch Posts: 1,104 Original Poster Rep: I don't understand that at all. What are tmp and mod? How come min and sec are the same?
 05-02-2021, 04:25 AM #12 shruggy Senior Member   Registered: Mar 2020 Posts: 2,321 Rep: 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? Last edited by shruggy; 05-02-2021 at 04:30 AM. 3 members found this post helpful.
05-03-2021, 01:00 AM   #13
Michael Uplawski
Senior Member

Registered: Dec 2015
Location: Apples
Distribution: Apple-selling shops, markets and direct marketing
Posts: 1,139
Blog Entries: 29

Rep:
Quote:
 Originally Posted by NevemTeve Code: ` # so it is 2021-05-02 12:32:20`
Sometimes you scare me...

1 members found this post helpful.
 05-03-2021, 01:48 AM #14 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 4,170 Blog Entries: 1 Rep: (Mind you, with the year there is an off-by-one error.) 1 members found this post helpful.
05-03-2021, 02:10 AM   #15
pan64
LQ Guru

Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,490

Rep:
Quote:
 Originally Posted by lucmove 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

1 members found this post helpful.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post bkumar82 Linux - Newbie 2 01-28-2019 01:08 AM LXer Syndicated Linux News 0 08-20-2014 07:21 AM Trd300 Linux - Newbie 27 03-11-2012 05:58 AM iav Linux - Server 0 01-27-2009 01:13 PM Cruger Programming 1 03-22-2004 09:18 AM

LinuxQuestions.org

All times are GMT -5. The time now is 01:38 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -