Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve multiple information by a single number?

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.

like image 982
Pithikos Avatar asked Oct 10 '22 20:10

Pithikos


1 Answers

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.

like image 99
phimuemue Avatar answered Oct 13 '22 11:10

phimuemue