PHP source code, reverse code, and complement introduction

Source: Internet
Author: User
PHP source code, reverse code, and complement introduction PHP source code, reverse code, and complement introduction


Original code:

It is the original binary code, which is actually an escape code we can understand after converting to binary in decimal format. The original code is understandable in numbers. However, in the actual computer operation process, the original code is often not used, so there is a problem of reverse code and complement code.

On this basis, the rule is as follows: the first bit of a binary number is a sign bit, 0 indicates a positive number, and 1 indicates a negative number.


Example: (8 digits are used as an example ):

7: 00000111

-7: 10000111


Anti-code:

The inverse code of positive numbers is itself.

A negative number is used to reverse each of the unsigned parts of the original code ..

Example:

7: 00000111

-7: 11111000


Complement:

The positive complement is its own

The negative number is the inverse code, and the symbol bit remains unchanged. after each of the other digits is reversed, 1 is added as a whole.

Example:

7: 00000111

-7: 11111000 + 1 = 11111001


Addition operation:

Subtraction:


Summary:

1: Data is computed by the complement code inside the CPU.

2: The conversion process of the original code, reverse code, and complement code is internal work of the system, and the symbol bit remains unchanged during conversion.

3: when the complement operation is used, the symbol bit is involved in the operation (that is, the operation is performed as a normal binary digit)


Manage the switch status of a group of things (data)

Switch status: it is a type of data. it has only two values available: true, false, on, and off...

A group of Things refers to any possible status of multiple data.

We assume that there are five lights to be managed:

1: determine the status of each light bulb: on, off;

2: You can specify to open any light bulb.

3: You can specify to close any light bulb.

Prerequisite: The five bulbs have a clear sequence. we will set five variables (the constant is actually OK)

Assume that the positions of 1-5 bulbs are from right to left-the digits of the corresponding number.

First light bulb: $ d1 = 1; // 00000001

Second light bulb: $ d2 = 2; // 00000010

Third light bulb: $ d3 = 4; // 00000100

Fourth light bulb: $ d4 = 8; // 00001000

Fifth bulb: $ d5 = 16; // 00010000

Then, you can use a variable to describe the overall status of all bulbs:

If $ state = 7; // 00000111, the first, second, and third bulbs are on.

If $ state = 9; // 00001001, it indicates the first and four bulbs are on.

If $ state = 13; // 00001101, it indicates that the first, second, and four bulbs are on.


Determine the switch of a light bulb:

$ S1 = $ state & $ dn // Here n represents any one between 1 and 5

If $ s1> 0, the light bulb is on. Otherwise, the light bulb is off.

Open the specified light bulb:

$ State = $ state | $ dn; // Here n represents any one between 1 and 5

Turn off the specified light bulb:

$ State = $ state &~ $ Dn; // Here n represents any one between 1 and 5

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.