이름에서 알 수 있듯이 PHP의 비트 연산자는 연산 대상 피연산자에 대해 비트 수준에서 연산을 수행합니다. 이는 이러한 피연산자를 비트 수준으로 변환하여 수행되며 이후에 필요한 계산이 수행됩니다. 빠른 처리를 위해 부울 값 수준이 아닌 이 비트 수준에서 여러 수학 연산을 수행할 수 있습니다.
광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사무료 소프트웨어 개발 과정 시작
웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등
PHP의 일부 비트 연산자는 다음과 같습니다.
이진 연산자는 2개의 피연산자에 대해 작업합니다. PHP에서 비트별 AND 연산자는 두 숫자를 입력 피연산자로 사용하고 이 두 숫자의 각 비트에 대해 AND를 수행합니다. 결과는 부울이고 두 비트가 모두 1이면 1이고 다른 경우에는 0입니다.
구문:
$first_op & $sec_op
&는 이 작업을 수행하는 데 사용되는 기호입니다.
예:
<?php $a=61; $b=32; echo $a & $b; ?>
출력:
아래는 표의 61과 32를 이진수로 표현한 것입니다. AND 기호에 따라 두 비트 모두 1을 포함하는 경우에만 1의 출력을 제공하고 다른 경우에는 0을 반환합니다. 그래서 아래와 같이 3번째 비트만 조건에 일치하므로 최종 출력은 32가 됩니다.
Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$a | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | = | 61 |
$b | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | = | 32 |
Output | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | = | 32 |
Similar to Binary AND, the bitwise OR operator takes two numbers as input operands and performs OR on each bit of these two numbers, and the result is a Boolean. It returns 1 if either of the bits or both the bits are 1. Hence the result will be 0 only if both digits are 0.
Syntax:
$first_op | $sec_op
| is the symbolical representation of bitwise OR.
Example:
<?php $a=50; $b=36; echo $a | $b; ?>
Output:
Below is the binary representation of 50 and 36, respectively, as shown in the table. As per the OR operation, we can see that in the 2nd, 3rd, 5th, and 6th bit, there are digits that are 1; hence the respective position below will also be 1, and the remaining digits are filled by 0 since they do not satisfy the condition. Hence the final output we get is 54.
Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$a | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | = | 50 |
$b | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | = | 36 |
Output | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | = | 54 |
This binary operator takes the input of two numbers as operands and performs an XOR operation on every bit. The result of these two numbers will be true if either of the bits is true, and the output will be false if both bits are true and both bits are false. The below table can be used for reference to the same.
a | b | OUTPUT |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Syntax:
$first_op ^ $sec_op
^ is the symbolical representation of bitwise XOR.
Example:
<?php $a=22; $b=31; echo $a ^ $b; ?>
Output:
Below is the binary representation of 22 and 31, respectively, shown in the table. In the below table, we can see that in the 5th and 8th bits, one of the bits is 1; hence in the output, those bits are 1, and the remaining are 0. The resulting output is 9 when converted to decimal.
Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$a | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | = | 22 |
$b | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | = | 31 |
Output | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | = | 9 |
Unlike above all operators, this is a unary operator; hence it performs a bitwise NOT on a single operand taken as its input. As the name suggests, bitwise NOT output is the opposite of its input.
Syntax:
~ $first_op
~ is used to represent bitwise NOT.
Example:
<?php $a=20; $b=65; echo $a & ~ $b; ?>
Output:
In the above code, we first perform bitwise NOT on the second operator and then combine it with a bitwise AND with the first operator to get the output. The table below shows the result after NOT is performed on $y and the final output, which is 20 in decimal.
Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$a | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | = | 20 |
$b | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | = | 65 |
~$b | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | = | 190 |
Output | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | = | 20 |
This operation differs from the above operations and involves shifting bits. 2 types of shifting can be performed: left shift and right shift.
Here the input bits are shifted to their left by the number of places as specified.
Syntax:
$first_op << $n
Where $n refers to the number of places by which bits must be shifted.
Example:
<?php $a=4; $b=3; echo $a << $b; ?> </h5> <p><strong>Output:</strong></p> <p><img src="https://img.php.cn/upload/article/000/000/000/172490636633998.png" alt="PHP의 비트 연산자" ></p> <p>Here we are specifying to shift binary 4 by 3 digits to its left. Hence the resulting output we get is 32.</p> <table> <tbody> <tr> <td width="58"><strong>Place value</strong></td> <td width="58"><strong>128</strong></td> <td width="58"><strong>64</strong></td> <td width="58"><strong>32</strong></td> <td width="58"><strong>16</strong></td> <td width="58"><strong>8</strong></td> <td width="58"><strong>4</strong></td> <td width="58"><strong>2</strong></td> <td width="58"><strong>1</strong></td> <td width="58"></td> <td width="58"></td> </tr> <tr> <td width="58"><strong>$a</strong></td> <td width="58">0</td> <td width="58">0</td> <td width="58">0</td> <td width="58">0</td> <td width="58">0</td> <td width="58">1</td> <td width="58">0</td> <td width="58">0</td> <td width="58">=</td> <td width="58">4</td> </tr> <tr> <td width="58"><strong>Output</strong></td> <td width="58">0</td> <td width="58">0</td> <td width="58">1</td> <td width="58">0</td> <td width="58">0</td> <td width="58">0</td> <td width="58">0</td> <td width="58">0</td> <td width="58">=</td> <td width="58">32</td> </tr> </tbody> </table> <h5>Right Shift( >> )</h5> <p>Here we are shifting the input bits to their right by the number of places as specified.</p> <p><strong>Syntax</strong>:</p> <pre class="brush:php;toolbar:false">$first_op >> $n
Where $n is the number of places it is to be shifted.
Example:
<?php $a=7; $b=2; echo $a >> $b; ?>
Output:
Here we are shifting binary 7 by two bits to its right. Hence the resulting output we get is 1.
Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||
$a | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | = | 7 |
output | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | = | 1 |
Above, we have covered all the basic bitwise operations used in PHP. As their name suggests, they can operate only on each bit of its input and not on whole numbers. They are still used in modern coding as they have a few advantages over the present join since data storing and fetching is relatively lesser and boosts performance too.
위 내용은 PHP의 비트 연산자의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!