Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How retrieve multiple information by a single number?

  1. Oct 5, 2011 #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 (Text):
    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 Lets 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.
     
  2. jcsd
  3. Oct 5, 2011 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  4. Oct 5, 2011 #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?
     
  5. Oct 5, 2011 #4

    berkeman

    User Avatar

    Staff: Mentor

    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....
     
  6. Oct 6, 2011 #5

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: How retrieve multiple information by a single number?
Loading...