Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

One, foreword

Learning Objectives:

  • Master the basic operation and conversion of source code inverse code complement
  • Proficiency in using and/or non – identical or different or applications
  • For the shift operation in the problem of skilled application, behind will be out of the problem operation

Second, the overview

The main function of the computer is to process information such as numerical value, text, sound, graphics and images.

Inside the computer, all kinds of information must be digitized and encoded before it can be transmitted, stored and processed. All the data is stored in the device in the form of binary, namely 0 and 1.

The operations (+, -, *, /) performed by the computer on binary data are called bitwise operations, such as the following calculation:

int a=74; int b=58; int c=a+b; A 74:1 0 0 1 0 1 0 b 55:1 1 1 0 1 0 C 132:1 0 0 0 0 1 0 0Copy the code

After converting a decimal digit to the underlying binary digit, the binary digits are added bit-by-bit to carry the full 2 into 1.

Third, the original code inverse code complement code

1. The original code

In computer operations, the computer can only do addition, subtraction, multiplication and division can not calculate. Source code is the simplest representation of machine number, with the highest bit representing the sign bit and the other bits storing the absolute binary value of that number.

The first 0 represents a positive number and the first 1 represents a negative number.

Features:

  • It is intuitive and easy to understand, and positive and negative numbers are clearly distinguished
  • The addition and subtraction operations are complicated, to determine whether the sign is a plus or minus sign, the same or the opposite

2. Radix-minus-one complement

The inverse of a positive number is the same as the source code, and the inverse of a negative number is the source code except for the sign bit.

Features:

  • The range of representation of the inverse code is the same as that of the original code
  • Inverse code representation is often used as the intermediate link of digital transformation in computers

3. The complement

The complement of a positive number is equal to its source, and the complement of a negative number is equal to the inverse +1.

Features:

  • In the computer operation, are in the way of complement operation, the following bit operation is also in the form of complement calculation

Four, basic operations

With 1.

Symbol: &

Operation rule: the value is 1 only when both bits are 1, otherwise, it is 0

Example: 1001 & 1111 = 1001

Or 2.

Symbol: |

Operation rule: the value is 0 only when both bits are 0, otherwise, it is 1

Example: 1001 & 1100 = 1101

3. The

Symbol: ~

Operation rule: 0 becomes 1,1 becomes 0

Example: ~1001 = 0110

4. With the or

Symbol: ~

Operation rule: the number is 1 at the same time, and 0 at the opposite time

Example: 1001-1100 = 1010

5. Xor

Symbol: ^

Operation rule: if two bits are opposite, they are 1, and if they are identical, they are 0

Example: 1001 ^ 0111 = 1110

5. Shift operation

1. Shift to the left

Symbol: < <

Operation rules: symbol bit unchanged, low fill 0

Example:

A <<b represents the decimal number a moved to the left by b carries/* Left shift: * left shift 1 bit, equivalent to the original value * 2 * left shift 2 bits, equivalent to the original value * 4 * left shift N bits, equivalent to the original value * 2^n */To calculate10 << 1
10The complement of:0000 1010----------------------- Result complement:0001 0100==> Positive, that is10*2=20To calculate10 << 2
10The complement of:0000 1010----------------------- Result complement:0010 1000==> Positive, that is10*2^2=40To calculate10 << 3
10The complement of:0000 1010----------------------- Result complement:0101 0000==> Positive, that is10*2^3=80To calculate12 << 1
12The complement of:0000 1100----------------------- Result complement:0001 1000==> Positive, that is12*2=24

Copy the code

2. Moves to the right

Symbol: > >

Operation rules: low overflow, the symbol bit is unchanged, and the symbol bit is used to fill the high overflow

Example:

A >>b represents the decimal number a moved to the right by b carries/* Move right: * move right 1 bit, equivalent to the original value / 2 * move right 2 bits, equivalent to the original value / 4 * move right 3 bits, equivalent to the original value / 2^n * Result has no decimal (round down) */To calculate80 >> 1
80The complement of:0101 0000----------------------- Result complement:0010 1000==> Positive, that is80/2=40To calculate80 >> 2
80The complement of:0101 0000----------------------- Result complement:0001 01000==> Positive, that is80/2^2=20To calculate80 >> 3
80The complement of:0101 0000----------------------- Result complement:0000 1010==> Positive, that is80/2^3=10To calculate24 >> 1
12The complement of:0001 1000----------------------- Result complement:0000 1100==> Positive, that is24/2=12

Copy the code

Source code inverse complement section reference picture: source code inverse complement