Bitweise Operatoren in PHP

王林
Freigeben: 2024-08-29 12:39:19
Original
297 Leute haben es durchsucht

Wie der Name schon sagt, führen die bitweisen Operatoren in PHP Operationen auf Bitebene an den Operanden aus, mit denen sie operieren sollen. Dies geschieht durch die Umwandlung dieser Operanden in ihre Bitebene, und anschließend wird die erforderliche Berechnung für sie durchgeführt. Für eine schnelle Verarbeitung können mehrere mathematische Operationen auf dieser Bitebene anstelle der booleschen Wertebene ausgeführt werden.

WERBUNG Beliebter Kurs in dieser Kategorie PHP-ENTWICKLER - Spezialisierung | 8-Kurs-Reihe | 3 Probetests

Starten Sie Ihren kostenlosen Softwareentwicklungskurs

Webentwicklung, Programmiersprachen, Softwaretests und andere

Die besten bitweisen Operatoren in PHP

Einige der bitweisen Operatoren in PHP sind unten aufgeführt:

1. Bitweises UND( & )

Binäre Operatoren arbeiten mit 2 Operanden. In PHP verwendet der bitweise UND-Operator zwei Zahlen als Eingabeoperanden und führt eine UND-Verknüpfung für jedes Bit dieser beiden Zahlen durch. Das Ergebnis ist boolesch und 1, wenn beide Bits 1 sind, bzw. 0, wenn kein anderer Fall vorliegt.

Syntax:

$first_op & $sec_op
Nach dem Login kopieren

& ist das Symbol, das zum Ausführen dieses Vorgangs verwendet wird.

Beispiel:

<?php
$a=61;
$b=32;
echo $a & $b;
?>
Nach dem Login kopieren

Ausgabe:

Bitweise Operatoren in PHP

Unten ist die binäre Darstellung von 61 und 32 in der Tabelle. Gemäß dem AND-Symbol wird nur dann 1 ausgegeben, wenn beide Bits 1 enthalten, andernfalls wird 0 zurückgegeben. Wie unten gezeigt, entspricht nur das 3. Bit der Bedingung, sodass die endgültige Ausgabe 32 ist.

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
Wert platzieren 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 Ausgabe 0 0 1 0 0 0 0 0 = 32

2. Bitwise OR( | )

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
Nach dem Login kopieren

| is the symbolical representation of bitwise OR.

Example:

<?php
$a=50;
$b=36;
echo $a | $b;
?>
Nach dem Login kopieren

Output:

Bitweise Operatoren in PHP

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

3. Bitwise XOR( ^ )

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
Nach dem Login kopieren

^ is the symbolical representation of bitwise XOR.

Example:

<?php
$a=22;
$b=31;
echo $a ^ $b;
?>
Nach dem Login kopieren

Output:

Bitweise Operatoren in PHP

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

4. Bitwise NOT( ~ )

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
Nach dem Login kopieren

~ is used to represent bitwise NOT.

Example:

<?php
$a=20;
$b=65;
echo $a & ~ $b;
?>
Nach dem Login kopieren

Output:

Bitweise Operatoren in PHP

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

5. Bit Shifting

This operation differs from the above operations and involves shifting bits. 2 types of shifting can be performed: left shift and right shift.

Left Shift( << )

Here the input bits are shifted to their left by the number of places as specified.

Syntax:

$first_op << $n
Nach dem Login kopieren

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="Bitweise Operatoren in 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
Nach dem Login kopieren

Where $n is the number of places it is to be shifted.

Example:

<?php
$a=7;
$b=2;
echo $a >> $b;
?>
Nach dem Login kopieren

Output:

Bitweise Operatoren in PHP

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

Conclusion

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.

Das obige ist der detaillierte Inhalt vonBitweise Operatoren in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
php
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!