How retrieve multiple information by a single number?

In summary, the Linux program chmod uses numbers to represent different permissions: 1 for execute, 2 for write, and 4 for read. These numbers can be combined to create unique permissions for a file. There is an algorithm behind this, which involves adding the numbers in binary notation. However, using this technique in base 10 would result in a waste of numbers and is not practical for computer use.
  • #1
Pithikos
55
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
  • #2
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.
 
  • #3
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?
 
  • #4
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...
 
  • #5
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.
 

1. How can I retrieve multiple pieces of information using just one number?

One way to do this is by using a database query. You can create a table in the database that has fields for each piece of information you want to retrieve, and then use the number as a unique identifier to retrieve the corresponding information from the database.

2. Is there a more efficient way to retrieve multiple pieces of information by a single number?

Yes, you can also use an array to store the information and use the number as the index to access the corresponding information. This can be more efficient than using a database query, especially if the information is already stored in memory.

3. Can I retrieve different types of information using just one number?

Yes, you can. As long as you have a way to identify each type of information (such as using different fields in a database table or different indexes in an array), you can retrieve different types of information using just one number.

4. How do I ensure that the retrieved information is accurate and matches the given number?

You can add validation checks to your code to ensure that the retrieved information matches the given number. For example, if you are using a database query, you can check that the number is a valid ID before retrieving the information.

5. Can I use this method to retrieve information from multiple sources?

Yes, you can. As long as you have a way to identify the information and link it to the given number, you can use this method to retrieve information from multiple sources, such as databases, arrays, or even external APIs.

Similar threads

Replies
1
Views
176
Replies
3
Views
238
Replies
3
Views
1K
Replies
8
Views
1K
  • General Math
Replies
3
Views
547
Replies
6
Views
1K
Replies
5
Views
2K
Replies
4
Views
175
  • General Math
Replies
5
Views
1K
  • General Math
Replies
2
Views
1K
Back
Top