This is common for the C-family of languages ββand works identically in C, C ++ and Objective-C. Unlike Java, Pascal, and similar languages, the C listing is not limited to the values ββgiven to it; it is actually an integral type of size that can represent all named values, and you can set an enumeration type variable to an arithmetic expression in enumeration members. As a rule, bit shifts are used so that the values ββare 2, and bit-logical operations are used to combine the values.
typedef enum { read = 1 << 2, // 4 write = 1 << 1, // 2 execute = 1 << 0 // 1 } permission; // A miniature version of UNIX file-permission masks
Again, bit shift operations are all from C.
Now you can write:
permission all = read | write | execute;
You can even include this line in the permission declaration itself:
typedef enum { read = 1 << 2, // 4 write = 1 << 1, // 2 execute = 1 << 0, // 1 all = read | write | execute // 7 } permission; // Version 2
How to enable execute for a file?
filePermission |= execute;
Please note that this is dangerous:
filePermission += execute;
This will change something with an all value of 8 , which makes no sense.
source share