• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How do you determine multiple states from sum value?

Childs

Lifer
I'm not sure how to phrase this question, so any pointers is appreciated.

Say you are given an integer which represents the state of multiple conditions. As easy analogy is something like setting chmod on a file. 7 is rwx, 5 is wx, 1 is x, etc. How would you do determine the state in code without doing a bunch of if/else tests for all possible values? In my specific case there are only 3 states with values of 1, 2 or 4, so I know I could do:

Code:
def isA(i):
     if i in [1, 3, 5, 7]:
          return True
     else:
          return False

def isB(i):
     if i in [2, 3, 6, 7]:
          return True
     else:
          return False

def isC(i):
     if i in [4, 5, 6, 7]:
          return True
     else:
          return False

But it seems like there should be a better method for doing something like this since this convention appears to be commonly used, I just dont know what to look up. I'm writing this in python (using objc objects), but any general pointers will probably get me in the right place.
 
I believe chmod works something like this:

It is doing a octal to binary conversion.

octal - binary
7 - 111
5 - 101
1 - 001

First bit is w. Second is r. Third is x.


rwx:
4+2+1 = 7
100 + 010 + 001 = 111

wx:
4+1 = 5
100 + 001 = 101

I think you get the idea 🙂
 
Chmod is just doing bitmasks. Read up on that topic.

Blue meanie did a decent top level summary. . Chmod just uses the integer format of the bits.
 
You need something more like.
Int arg = 5; // 0x101
Boolean w = 0x100&arg>0

Read up on bitwise operators if the single & confuses you.
 
Last edited:
Back
Top