PHP bitwise operator

Source: Internet
Author: User
Tags bitwise bitwise operators php tutorial

Bitwise operators

Bitwise operators allow placement of the bits specified in the integer number. If both the left and right arguments are strings, the bitwise operator will manipulate the ASCII value of the character.

Do not move more than 32 bits to the right on 32-bit systems. Do not move left when the result may be more than 32 digits.

Example name result
$a & $b and (bitwise) will set the $a and the bits of the $b 1 to 1.
$a | $b or (bitwise OR) sets the bit 1 in the $a or $b to 1.
$a ^ $b Xor (bitwise exclusive OR) sets the different bits in the $a and $b to 1.
~ $a not (bitwise not) sets the bit of 0 in the $a to 1, and vice versa.
$a << $b shift left (move right) moves the bits in the $a to the left $b times (each time a move is "multiplied by 2").
$a >> $b shift Right (move) moves the bits in the $a to the right $b times (each move is "divided by 2").

<?php Tutorial

$a = 1;
$b = 1;
echo "<br/>and (bitwise AND)"; will set the $a and the $b 1 in the bit to 1
echo $a & $b; Display value 1

echo "<br/>or (bitwise OR)"; will set the $a or the $b to 1 of the bit to 1
echo $a | $b; Display value 1

echo "<br/> Xor (bitwise exclusive OR)"//will set a different bit of $a and $b to 1
echo $a ^ $b; Display value 0

echo "<br/>not (bitwise NOT)"; Set the bit to 0 in the $a to 1, and vice versa
echo ~ $a; Display Value-2

echo "<br>shift left";//Moves the bits in the $a to the left $b times (each time a move is "multiplied by 2")
echo $a << $b; Display Value 2

echo "<br/>shift right";//Moves the bits in the $a to the right $b times (each move represents "divided by 2").
echo $a >> $b; Display value 0

?>

Bitwise operators allow placement of the bits specified in the integer number. If both the left and right arguments are strings, the bitwise operator will manipulate the ASCII value of the character.


<?php
echo 12 ^ 9; Output is ' 5 '

echo "12" ^ "9"; Output fallback characters (ASCII 8)
(' 1 ' (ASCII)) ^ (' 9 ' (ASCII)) = #8

echo "Hallo" ^ "Hello"; Output ASCII value #0 #4 #0 #0 #0
' A ' ^ ' e ' = #4

Echo 2 ^ "3"; Output 1
2 ^ ((int) "3") = = 1

Echo "2" ^ 3;//Output 1
             //((int) 2 ") ^ 3 = = 1                            
?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.