How retrieve multiple information by a single number?

  • Context: Undergrad 
  • Thread starter Thread starter Pithikos
  • Start date Start date
  • Tags Tags
    Information Multiple
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
Pithikos
Messages
55
Reaction score
1
I was wondering how they did come up with the way of setting permissions using the linux program chmod by just using numbers. For example:
Code:
1 is for execute
2 is for write
4 is for read

Any sum of those give a unique permission to a file:
2+4=6 Let's you write and read it.
1+4=5 Let's you execute and read it.

So is there an algorithm behind this? Say for example that I have 5 different items and I want to be able to pass a single number and the other person to understand which combination of items I made.
 
Mathematics news on Phys.org
It becomes more apparent when you write the numbers in binary notation.
0001 is for execute
0010 is for write
0100 is for read
so each permission is indicated by a single bit.

If you want write and read, you add 0001 and 0010 and get 0011, which is 6 in decimal notation.
To check if you have write permission, you only have to check the second bit again. For a computer, that is very easy: you "AND" 0011 with the permission you want to check, e.g. 0010. If it is set, you will get 0010 back, if not, you will get 0000.
 
Thank you! That is indeed easy to grasp.
However what if we want to apply the same with base 10 numbers? Then it wouldn't be practical to write down the numbers as

0000
0001
0010
0100

Because that would yield the number 111 for just three objects. Or am I missing something?
 
Pithikos said:
Thank you! That is indeed easy to grasp.
However what if we want to apply the same with base 10 numbers? Then it wouldn't be practical to write down the numbers as

0000
0001
0010
0100

Because that would yield the number 111 for just three objects. Or am I missing something?

You are correct. You would waste lots more numbers using this technique in base 10 versus base 2. That's probably why it's not used in base 10 applications...
 
In principle, it is not a problem to "waste" so many numbers, if you are just going to write them down.

However, if you want to do this on a computer, eventually you will have to convert 11110 back to binary and you would get 11011112. So whereas you only needed 3 bits before, you now need 7 bits to store exactly the same information.