Returning numbers included in a number

The title is not clear what is my problem. I have one number which is the sum of (total) some numbers and I want to know what numbers are included in that number. I have some permissions (as you see below) and I just get one number and I need to return permissions a user has.

1:  "Permission One",
2:  "permission Two",
4:  "permission Three",
8:  "permission Four",
16: "permission Five"

I just get one number (from the server-side) and I need a function that returns what permissions this user has.

For example, when I get 9, it means:

8+1

Or when I get 7 it should return:

4+2+1

I am not familiar with math language, please guide me with a step-by-step answer


EDIT:

Here is my current algorithm:
I already wrote a function, but I need to have a better solution.

  1. The number is x
  2. I have an array of permissions (like m=[1, 2, 4, 8, 16, ...])
  3. Add the first number which is smaller the x from m array. (if x is 10, then 8 will be added to permission array)

  4. Try the next smaller value of m (In this case, I add 4+m which is bigger that x, then we will try with next smaller number)

  5. If x==m that is it! Finish and return x array. else, go back to 6
  6. If x==all m children from the current number, finish it. else go to 7
  7. if x!=m go to 4

live demo of my algorithm


I am looking for a better, faster and also really mathematical solution. Any idea?

Thanks

Solutions Collecting From Web of "Returning numbers included in a number"

It can be easily done by bitwise AND, your code snippet was in JavaScript, JavaScript bitwise AND operator symbol is &, and here is how you can do it:

function checkRoles(roleToCheck, rolesChecksum){
   return roleToCheck == (roleToCheck & rolesChecksum);
}
//For example, rolesChecksum is 10:
checkRoles(1, 10) //Returns false
checkRoles(2, 10) //Returns true
checkRoles(8, 10) //Returns true

Thanks for users who commented under the question and made this answer possible