当前位置:网站首页>Shift operation of complement

Shift operation of complement

2022-07-05 08:38:00 Stay--hungry

Usually , In the computer Signed number With Complement code stored .
Deep understanding of complement

Shift the complement left : Removed bit complement 0.
The sign bit may also change , Thus, the positive and negative numbers change .
stay When there is no sign bit change , Moving one bit to the left is equivalent to × 2 \times 2 ×2. Special , When the number is negative , This property also holds :

Suppose the number in the computer 8bit Storage :

Complement code Original code Count
0111 10000111 1000120
1111 00001001 0000-16
1110 00001010 0000-32
1100 00001100 0000-64
1000 0000 Cannot be expressed in the original code -128
0000 00000000 00000

The limit value of left shift is 0.

Shift the complement right :

  • Yes just Integers ( Symbol bit 0) Move right : Removed bits repair 0
  • Yes negative Integers ( Symbol bit 1) Move right : Removed bits repair 1, The result is still negative

The limit value of the right shift of a positive integer is 0, The limit value of the right shift of negative integers is -1.

in fact , When it's negative , When moving right, the highest position is filled 0 Or make up 1 Depending on the rules of the compilation system .( repair 0 Of is called “ Logical shift right ”, repair 1 Of is called “ Arithmetic shift right ”.) therefore , Try not to shift negative numbers !
 Insert picture description here
Be careful :
Whether arithmetic shift right or logical shift right , The result of the operation Is the original number divided by 2. For unsigned numbers , Logical shift right is adopted ( repair 0); For signed Numbers , Using arithmetic shift right . When arithmetic moves right , The number of the left complement sign bit .

原网站

版权声明
本文为[Stay--hungry]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140541359426.html