Bitwise operators in PHP (almost rarely used)

Source: Internet
Author: User
Tags bitwise bitwise operators php language sprintf

bit operators in the PHP language &, |, ^, ~, 〈〈, 〉〉
"&" Bitwise AND operation
The bitwise AND operator "&" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase. Only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion.
For example:9&5 can be written as      follows:00001001        (9 twos complement)     &00000101         (5 twos      complement)00000001        (1 twos complement)<?  9  5; Echo sprintf ("%b", $a &$b); >
"|" Bitwise OR OPERATION
Bitwise OR operator ' | ' is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase or. As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. The two numbers participating in the operation are in complement.

For example:9|5 The following can be written:00001001|00000101    00001101(decimal 13) Visible 9|5= -<?php$a=9; $b=5; Echo sprintf ("%b", $a | $b)."\ n";// BinaryEcho sprintf ("%d", $a | $b)."\ n";//decimal
"^" Bitwise XOR OR operation
The bitwise XOR operator "^" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary dissimilarity or, when the two corresponding binary differences, the result is 1. Participation in the arithmetic is still in complement, for example 9^5 can be written as follows:
     00001001^00000101       00001100(decimal)<?php$a=9; $b=5; Echo sprintf ("%b", $a ^ $b)."\ n";// BinaryEcho sprintf ("%d", $a ^ $b)."\ n";//decimal
"~" Negation operation
Negation operator ~ is a single-mesh operator with right-associative. Its function is to reverse the bitwise negation of the number of participating operations.
For example, the    operation of the ~ (0000000000001001) Result:1111111111110110<?  95; Echo sprintf ("%b", ~ $a). " \ n " //  Binary
"〈〈" left-shift operation
The left-shift operator "<<" is the binocular operator. Its function to the left of the "<<" operand of all the binary all left a number of bits, the "<<" to the right of the number to specify the number of bits moved, high drop, low 0.
For example,    a<<4 refers to moving the binary of a to the Left 4 bits. such as a=00000011<?  3= $a <<4; Echo sprintf ("%d" , $temp). " \ n " // decimal
">>" right shift operation
The right-shift operator ">>" is the binocular operator. The function is to shift all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move.

For example: Set  a= zero, a>>4 to move the 00110000 right to 00000011 (decimal 3). It should be stated that, for the signed number, when moving right, the symbol bit is moved along with it. When a positive number, the highest bit of 0, and negative, the sign bit is 1, the highest bit is 0 or a complement of 1 depends on the provisions of the compilation system <  = $a >> 4, echo sprintf ("%d" , $temp). " \ n " // decimal



Bitwise operators in PHP (almost rarely used)

Related Article

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.