PHP Base bit operations

Source: Internet
Author: User
PHP bit arithmetic

$a & $b and (bitwise AND)

$a | $b or (bitwise OR)

$a ^ $b Xor (Bitwise XOR)

~ $a not (bitwise non)

$a << $b shift left

$a >> $b shift right

Detailed

$a & $b Bitwise and set the bit to 1 in both $ A and $b to 1;

Example: & 12 = 8

10 1010

12 1100

1000 8

$a | $b bitwise OR put a $ A or $b in one of 1 for the set to 1;

Example: 10 | 12 = 14

10 1010

12 1100

1110 14

$a ^ $b Bitwise XOR OR

Example: 10 ^ 12

10 1010

12 1100

0110 6

~a is set to 0 for the 0 of the per-digit

Example: ~ =

10 1010 1111111111111111111111111111111111111111111111111111111111110101-11

$a << $b left move the $b of $ A to the left (each move represents a multiply by 2);

Example: 1 << 10 = 1024

1 (1) shift left 10 bit 10000000000 (1024)

Equivalent to the 1*2 10 times, PHP is not a power operation is really depressed.

$a >> $b Right Move the $b in $ A to the right (each move is represented by 2);

Example: 1024x768 << 2 = 1256

10000000000 (1024) Shift right 2 bits is 100000000 (256)

PHP for Operation $ A & $b and (bitwise VS) $a | $b or (bitwise OR) $a ^ $b Xor (Bitwise XOR) ~ $a not (bitwise non) $a << $b shift left $a >> $b shift right

Detailed $ A & $b bitwise set to 1 for Bits 1 in $ A and $b; Example: & 12 = 810 101012 1100 1000 8

$a | $b bitwise OR put a $ A or $b in one of 1 for the set to 1; Example: 10 | 12 = 1410 101012 1100 1110 14

$a ^ $b Bitwise XOR: 10 ^ 1210 101012 1100 0110 6

~a is set to 0 for the 0 of the per-bit non-set of $ A: ~ = 10 1010 1111111111111111111111111111111111111111111111111111111111110101-11

$a << $b left move the $b in $ A to the left (each move is multiplied by 2), for example: 1 << 10 = 10241 (1) Shift left 10 bits 10000000000 (1024) is equivalent to 1*2 10 times, PHP is not a power operation is really depressed.

$a >> $b Right Move the $b in $ A to the right (each move is represented by 2); For example: 2 << 2 = 125610000000000 (1024) Right-shift to the left-hand side is 100000000 (256)

  • 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.