For example, I have a list of rights that are encoded in a bitmask:

const int R0 = 1 << 0; const int R1 = 1 << 1; const int R2 = 1 << 2; ... const int R31 = 1 << 31; 

As you can see, 32 is the maximum number of rights that can be encoded (provided that INT can occupy no more than 32 bits). But what if I need to add more rights - how then can I expand the mask?

  • one
    Use the second DWORD to store 32 more fields? - Vladimir Martyanov
  • @ Vladimir Martyanov, No. For c ++ you can, and if I have php? - XYZ
  • one
    Well, if you can not use another variable - no way. - Vladimir Martyanov
  • one
    What's the difference what language. Make an array of the required number of int. divide the bit number in the mask by 32 - get the int number in the array, then from this int you take the bit with the bit number & 31 (it's the remainder of dividing by 32) - Mike

2 answers 2

First, take an unsigned int :)

Then - if there is a wider unsigned long or unsigned long long .

You can, in the end, just take an array of unsigned int .

    If you have PHP, it is still easier. Here bit operations work with strings and you can operate with masks of any length (the main thing is that there is enough RAM). It will turn out somewhat more difficult with masks, but quite clearly.

    Defining masks:

     define('R0', "\x01"); define('R1', "\x02"); define('R2', "\x04"); define('R3', "\x08"); // ... define('R7', "\x80"); // ... define('R22', "\x00\x00\x40"); define('R23', "\x00\x00\x80"); // ... define('R78', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40"); define('R79', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80"); define('R80', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80"); // ... 

    Combination of masks:

     $flags = R1 | R23 | R78; 

    Check mask (a bit of distortion 18+):

     if (($flags & R23) === R78) { // будет выполнено } if (($flags & R79) === R79) { // НЕ будет выполнено } 

    Note that the “high byte” is at the end of the line.