PHP bitwise operation and Binary

Source: Internet
Author: User

? Binary

Binary is to use 0 and one to represent the computer language, 0,1 can represent any number, its every two in a

There are three important concepts in binary system

Bitwise arithmetic

Bitwise operator ARITHMETIC rules:

Bitwise AND &: Two bits are all 1 and the result is 1

Bitwise OR |: Two bits have one of 1 and result is 1

Bitwise XOR ^: One of two bits is 0, one is 1, and the result is 1

Bitwise REVERSE ~:0 1, 1 take 0

According to the rules, the steps to calculate a number:

For example calculation: ~2=?

1. Find out the complement of 2,"because computer operations are performed in a complementary way."

2 The original code: 00000000 00000000 00000000 00000010

the first number on the top of the string from left to right is 0, so it is a positive number, and there is a principle for positive numbers: the original code of positive number = inverse Code = complement, so 2 of the complement is the above string

2. Re-take Counter-

To reverse: 11111111 11111111 11111111 11111101

The string above gets the complement, and it's going to be a complement------and the original code to get the results we want.

3. Complement--and inverse code [(complement-1) is anti-code]

Reverse code: 11111111 11111111 11111111 11111100

4. Reverse code-to-source code [ sign bit unchanged, that is, the above string from left to right number of the first number unchanged]

Original code:10000000 00000000 00000000 00000011=1*2^0+1*2^1=1+2=-3//Front Red 1 indicates negative number, so the result is-3

^ Represents the second side, 2^0 represents 2 of the 0 square, from right to left to calculate: namely 1 (the number above this string from right to left number of the first 1) *2^0+1 (above this string number from right to left number of the second 1) *2^1

For example: 2&3=?

1. Find the complement of 2 and 3

2 Complement: 00000000 00000000 00000000 00000010

3 complement: 00000000 00000000 00000000 00000011

Bitwise AND & operations: Two bits are 1 and the result is 1

2&3= 00000000 00000000 00000000 00000010 //front string is complement, because its first number is 0, so it is positive, the positive complement = The original code, so the result is 2


Final: The step of calculating a number is the original code---------------------------to

? Displacement operations

In PHP, the shift operator has two:>> (shift right) and << (left)

Rules for Operations:

Shift right: low overflow, sign bit unchanged, and sign bit to fill overflow high point [popular points is the rightmost number overflow, with the leftmost number (number of symbols) fill overflow number, on the leftmost]

Shift left: Symbol bit unchanged, low 0 [Popular point is the leftmost number overflow, with the number of zeros fill overflow, put on the far right]

According to the rules, here are a few examples:

A. $a =1>>2;//move 1 to the right two-bit

1. Find out 1 of the complement

00000000 00000000 00000000 00000001

1>>2

00000000 00000000 00000000 00000000

$a =1>>2=0

B. $a =1<<2;//move 1 to the left two-bit

1. Find out 1 of the complement

00000000 00000000 00000000 00000001

1<<2

00000000 00000000 00000000 00000100

$a =1<<2=4

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.