如何在php中实现位掩码? [英] How to implement a bitmask in php?

查看:119
本文介绍了如何在php中实现位掩码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定位掩码是否正确.让我解释一下:

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

在php中,可以通过多种方式调用error_reporting函数:

In php, the error_reporting function can be called multiple 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);

我从php.net页面此处

I got the term bitmask from the php.net page here

无论如何,我已经实现了一个名为ls的SIMPLE方法,该方法返回目录的内容.

Anyway the point of this is, I have implemented a SIMPLE method called ls which returns the contents of a directory.

此函数需要3个参数...($ include_hidden = false,$ return_absolute = false,$ ext = false)

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

因此,当我调用该函数时,我将设置所需的结果.是否要结果返回隐藏目录,是否只需要基名等等.

So when i call the function, i set how i want the results. Whether i want the results to return hidden directories, whether i want basenames only etc.

所以当我调用该函数时,我正在写

so when i call the function i'm writing

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 flag how i want the data returned?

类似:

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

等...

我将如何通过测试调用哪些标志来实现这一点?

How would i implement this in terms of testing which flags have been called?

推荐答案

实际上很简单.首先,用一些代码演示如何实现.如果您对这段代码的工作方式或工作方式一无所知,请随时在注释中提出其他问题:

It's quite simple actually. First a bit of code to demonstrate how it can be implemented. If you don't understand anything about what this code is doing 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);

演示

由于标志是整数,因此在32位平台上最多定义32个标志.在64位平台上,它是64.也可以将标志定义为字符串,在这种情况下,可用标志的数量或多或少是无限的(当然,在系统资源的范围内).这是二进制形式的工作原理(为简单起见,将其缩减为8位整数).

Because the flags are integers, on a 32-bit platform you define up to 32 flags. On a 64-bit platform, it's 64. It is also possible to define the flags as strings, in which case the number of available flags is more or less infinite (within the bounds of system resources, of course). Here's how it works in binary (cut down 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...

当您组合标志以将它们传递给函数时,可以将它们或在一起.让我们看一下通过FLAG_1 | FLAG_3

When you combine the flags to pass them to the function, you OR them together. Let's take a look at what happens when we pass FLAG_1 | FLAG_3

  00000001
| 00000100
= 00000101

当您想查看设置了哪些标志时,可以将位掩码与该标志相加.因此,让我们采用上面的结果,看看是否设置了FLAG_3:

And when you want to see which flags were set, you AND the bitmask with the flag. So, lets take the result above and see if FLAG_3 was set:

  00000101
& 00000100
= 00000100

...我们返回标志的值,这是一个非零整数-但是如果我们看到是否设置了FLAG_2:

...we get the value of the flag back, a non-zero integer - but if we see if FLAG_2 was set:

  00000101
& 00000010
= 00000000

...我们得到零.这意味着您可以在检查值是否通过时简单地将AND操作的结果评估为布尔值.

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

这篇关于如何在php中实现位掩码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆