How to implement bit mask in php?

I'm not sure bitmask is the right term. Let me explain:

In php, the error_reporting function can be called in several ways:

 // Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Reporting E_NOTICE can be good too (to report uninitialized // variables or catch variable name misspellings ...) error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); // Report all errors except E_NOTICE // This is the default value set in php.ini error_reporting(E_ALL ^ E_NOTICE); 

I got the term bitmask from php.net page here

Anyway, this is what I implemented a SIMPLE method called ls that returns the contents of a directory.

This function takes 3 arguments ... ($ include_hidden = false, $ return_absolute = false, $ ext = false)

Therefore, when I call a function, I set how I need the results. I want the results to return hidden directories, do I want only base names, etc.

so when i call the function i write

 ls(true, false, true) ls(false, false, true) ls(true, true, true) etc... 

I thought it would be much more readable if I could just indicate how I want to return the data?

so something like:

 ls( INCLUDE_HIDDEN | HIDE_EXTS ); ls( SHOW_ABSOLUTE_PATHS | HIDE_EXTS ); 

etc...

How do I implement this in terms of testing, which flags were called?

+54
php bitmask
Aug 09 2018-12-12T00:
source share
3 answers

It is pretty simple. First, some code to demonstrate how it can be implemented. If you don’t understand anything about what this code does or how it works, feel free to ask additional questions in the comments:

 const FLAG_1 = 0b0001; // 1 const FLAG_2 = 0b0010; // 2 const FLAG_3 = 0b0100; // 4 const FLAG_4 = 0b1000; // 8 // Can you see the pattern? ;-) function show_flags ($flags) { if ($flags & FLAG_1) { echo "You passed flag 1!<br>\n"; } if ($flags & FLAG_2) { echo "You passed flag 2!<br>\n"; } if ($flags & FLAG_3) { echo "You passed flag 3!<br>\n"; } if ($flags & FLAG_4) { echo "You passed flag 4!<br>\n"; } } show_flags(FLAG_1 | FLAG_3); 

Demo




Since flags are integers, on a 32-bit platform you define up to 32 flags. On a 64-bit platform 64. It is also possible to define flags as strings, in which case the number of available flags will be more or less infinite (within system resources, of course). Here's how it works in binary format (shortened to 8-bit integers for simplicity).

 FLAG_1 Dec: 1 Binary: 00000001 FLAG_2 Dec: 2 Binary: 00000010 FLAG_3 Dec: 4 Binary: 00000100 // And so on... 

When you combine flags to convey their functions, you OR them together. Let's see what happens when we pass FLAG_1 | FLAG_3 FLAG_1 | FLAG_3

  00000001 | 00000100 = 00000101 

And when you want to see which flags were set, you and the bitmask with the flag. So, let's look at the result above and see if FLAG_3 is FLAG_3 :

  00000101 & 00000100 = 00000100 

... we get the flag value back, a nonzero integer, but if we see if FLAG_2 was set:

  00000101 & 00000010 = 00000000 

... we get zero. This means that you can simply evaluate the result of the AND operation as a boolean when checking whether the value was passed.

+116
Aug 09 '12 at 9:30
source share
 define( "INCLUDE_HIDDEN", 0x1 ); define( "HIDE_EXTS", 0x2 ); define( "SHOW_ABSOLUTE_PATHS", 0x4 ); //And so on, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800 etc.. 

Then you can check individual flags in your ls function:

 if( $flags & INCLUDE_HIDDEN ) { //<-- note just a single &, bitwise and //$flags have INCLUDE_HIDDEN } 
+14
Aug 09 2018-12-12T00:
source share

Others offered good suggestions, but these days are much more common in associative arrays instead of bitmasks. It is much more readable and allows you to pass other variables, except for only true / false values. Something like that:

 myFunction(['includeHidden' => true, 'fileExts' => false, 'string' => 'Xyz']); function myFunction($options) { // Set the default options $options += [ 'includeHidden' => false, 'fileExts' => true, 'string' => 'Abc', ]; if ($options['includeHidden']) { ... } ... } 
+3
Mar 17 '15 at 1:21
source share



All Articles