C language shift operator

1. Introduction

Regarding the shift operator, we are divided into two categories, one is the left shift operator <<   , and the other is the right shift operator >>

And both operators are closely related to two's complement .

Please see http://t.csdn.cn/9W84t for details on complement code

Warning: For shift operators, do not shift negative bits, this is undefined by the standard. 

Two, << left shift operator

左移操作符: 左边丢弃,右边补0

例如:

int a = 7

// 0000 0000 0000 0000 0000 0000 0000 0111  - 7的原码,也是7的补码

a = a << 1  

//将7的补码向左移动一位,随后在右边补上一个数字0 
//因为int的内存之中只能存放32位bit,所以从左往右数32位数留下,多出的数字剔除
//而在此运算中,最左边移动的哪一位数字,就是被剔除的
  
向左移动的一位
   ↓
// 0 0000 0000 0000 0000 0000 0000 0000 1110
                                           ↑
                                          向右补上的数字0

// 0000 0000 0000 0000 0000 0000 0000 1110
 
//因为是正数,补码原码是一致的,所以可以直接进行计算,最后得出的结果是14

Of course, the above is for operations with positive numbers, and the same is true for negative numbers, but negative numbers must first be converted into complement form, and then shifted.

int b = -7

// 1000 0000 0000 0000 0000 0000 0000 0111 - -7的原码

// 1111 1111 1111 1111 1111 1111 1111 1000 - -7的反码

// 1111 1111 1111 1111 1111 1111 1111 1001 - -7的补码

b = b << 1 

//进行左边移动一位,右边补上一位数字0

 左边移动一位
   ↓
// 1111 1111 1111 1111 1111 1111 1111 1001 
                                          ↑
                                          右边需要补上一位数字0
 左边移动一位
   ↓
// 1 1111 1111 1111 1111 1111 1111 1111 0010
                                           ↑
                                          右边补上一位数字0

//整理得到:

// 1111 1111 1111 1111 1111 1111 1111 0010 

最后得到结果,需要变回原码 

// 1000 0000 0000 0000 0000 0000 0000 1101 -  -7 << 1 的反码

// 1000 0000 0000 0000 0000 0000 0000 1110 -  -7 << 1 的原码

最后经过计算得知 -7<<1 的结果是 -14 

Through the above results, we know that the left shift operator << has the effect of multiplying by 2.

 7 << 1  = 14
 7 << 2  = 28
 7 << 3  = 56
 7 << 4  = 112

-7 << 1  = -14
-7 << 2  = -28
-7 << 3  = -56
-7 << 4  = -112

从而得出结论:
a<<1       a×2
a<<2      a×2×2
a<<3      a×2×2×2

Three, the right shift operator>>

Right shift operators are divided into logical right shift and arithmetic right shift .

1. Logical right shift

逻辑右移:左边用0填充,右边丢弃

例如:

int a = -1 

// 1111 1111 1111 1111 1111 1111 1111 1111  - -1的补码

a >> 1 

左边补一个数字0
   ↓
// 01111 1111 1111 1111 1111 1111 1111 1111
                                          ↑
                                        右边丢弃一位
左边补一个数字0
   ↓
// 0111 1111 1111 1111 1111 1111 1111 1111  - -1 >> 1 的结果
                                          ↑
                                        右边丢弃一位

There is a disadvantage of logical right shift. As shown above, if a negative number needs to be shifted to the right, logical right shift is used, then the negative number will become a positive number.

Therefore, most compilers do not use logical right shift, but arithmetic right shift.

2. Arithmetic right shift

算术右移:右边丢弃,若是负数左边补充1,若是正数左边补充0

例如:

int b = -1 

// 1111 1111 1111 1111 1111 1111 1111 1111  - -1的补码

b >> 1 

左边补一个数字1
   ↓
// 11111 1111 1111 1111 1111 1111 1111 1111
                                          ↑
                                        右边丢弃一位
左边补一个数字1
   ↓
// 1111 1111 1111 1111 1111 1111 1111 1111  - -1 >> 1 的结果
                                          ↑
                                        右边丢弃一位

3. Matters needing attention

The operands of shift operators can only be integers.

float f = 4.5f

Guess you like

Origin blog.csdn.net/2301_76445610/article/details/132121579