PHP에서 비트 마스크를 구현하는 방법은 무엇입니까?
비트 마스크가 올바른 용어인지 확실하지 않습니다. 설명하겠습니다.
PHP에서 error_reporting함수는 여러 방법으로 호출 될 수 있습니다.
// 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 페이지 에서 비트 마스크라는 용어가 있습니다.
어쨌든 이것의 요점은 ls디렉토리의 내용을 반환 하는 SIMPLE 메서드를 구현했습니다 .
이 함수는 3 개의 인수를 취합니다 ... ($ include_hidden = false, $ return_absolute = false, $ ext = false)
그래서 함수를 호출 할 때 원하는 결과를 설정했습니다. 결과가 숨겨진 디렉토리를 반환할지 여부, 기본 이름 만 원하는지 여부 등
그래서 내가 쓰고있는 함수를 호출 할 때
ls(true, false, true)
ls(false, false, true)
ls(true, true, true)
etc...
데이터를 반환하는 방법에 플래그를 지정할 수 있다면 훨씬 더 읽기 쉬울 것이라고 생각했습니다.
그래서 다음과 같습니다.
ls( INCLUDE_HIDDEN | HIDE_EXTS );
ls( SHOW_ABSOLUTE_PATHS | HIDE_EXTS );
기타...
어떤 플래그가 호출되었는지 테스트 측면에서 어떻게 구현합니까?
사실 아주 간단합니다. 먼저 구현 방법을 보여주는 코드입니다. 이 코드가 수행하는 작업이나 작동 방식에 대해 이해하지 못하는 경우 의견에 추가 질문을 자유롭게 물어보십시오.
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 비트 정수로 줄임).
FLAG_1
Dec: 1
Binary: 00000001
FLAG_2
Dec: 2
Binary: 00000010
FLAG_3
Dec: 4
Binary: 00000100
// And so on...
플래그를 결합하여 함수에 전달할 때 함께 OR합니다. 우리가 지나갈 때 어떤 일이 일어나는지 살펴 보자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, lets take the result above and see if FLAG_3 was set:
00000101
& 00000100
= 00000100
...we get the value of the flag back, a non-zero 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 if the value was passed.
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..
You can then check for individual flags in your ls function:
if( $flags & INCLUDE_HIDDEN ) { //<-- note just a single &, bitwise and
//$flags have INCLUDE_HIDDEN
}
The others have offered good suggestions, but these days it's much more common to pass in associative arrays instead of bitmasks. It's much more readable and allows you to pass in other variables other than just true/false values. Something like this:
myFunction(['includeHidden' => true, 'fileExts' => false, 'string' => 'Xyz']);
function myFunction($options) {
// Set the default options
$options += [
'includeHidden' => false,
'fileExts' => true,
'string' => 'Abc',
];
if ($options['includeHidden']) {
...
}
...
}
참고URL : https://stackoverflow.com/questions/11880360/how-to-implement-a-bitmask-in-php
'IT TIP' 카테고리의 다른 글
| DisplayAttribute.Description 특성 값을 어떻게 표시합니까? (0) | 2020.11.03 |
|---|---|
| ArrayList 인쇄 (0) | 2020.11.03 |
| ActionBar 위쪽 탐색은 onResume 대신 부모 활동을 다시 만듭니다. (0) | 2020.11.03 |
| 더블은 정말 돈에 부적합합니까? (0) | 2020.11.03 |
| int의 크기는 컴파일러 및 / 또는 프로세서에 따라 달라 집니까? (0) | 2020.11.03 |