I was wondering how they did come up with the way of setting permissions using chmod by just using numbers. For example:
1 is for execute
2 is for write
4 is for read
Any sum of those give a unique permission:
2+4 = 6 lets you write and read.
1+4 = 5 lets you execute and read
1+2+4 = 7 lets you execute, read and write
Is there an algorithm to this? Say for example I have 10 items and I want to give to a person a number and by just that number the person can tell which items I have chosen.
Binary system. I.e. you represent 1, 2, 4, 8, 16, and so on with a 0-or-1-digit each. The last digit stands for 2^0=1
, the second-last digit stands for 2^1=2
, the next digit for 2^2=4
, the next for 2^3=8
and so on.
Now, you associate an action (read/ex/write) with each digit.
A (more or less) surprising fact is the following: If you don't have just two options (i.e. if you do not just have true or false), but if you have more, you can adapt this pattern to the ternary system. Moreover, you can adapt this pattern for any base. The human system works for base 10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With