位运算 - 算术运算 [英] Bitwise Operations -- Arithmetic Operations

查看:143
本文介绍了位运算 - 算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您解释一下下面的线,一些很好的例子。


  

由n个阿左算术移
  相当于乘以2 N
  (提供的值不
  溢出)。



  

一个正确的算术
  由二的补码值的n移位
  相当于除以2 N
  向负无穷大舍入。如果
  二进制数被视为那些'
  补充,那么同样右移
  运算结果除以2 N
  和向零舍入。



解决方案

我会解释,我们是比较熟悉的基础发生了什么:10

在基地10,假设你有一个数N = 123。现在,你SHIFT这个数字向左K = 3个位置,以0填充清空位数所以,你得到X = 123000。

注意,X = N * 10 K

与基地2的情况是相似的。

 例1(基数为10)|实施例2(基数为2)
                       |
 N = 123 | N = 110101(53 10为基数)
 K = 3 | K = 2(在基体10)
 N'LT;< K = 123000 | N'LT;< K = 11010100(以10为基数212)
                       |
 10 ^ K = 1000 | 2 ^ K = 100(在基体2;在基座10 4)
 N * 10 ^ K = 123000 | N * 2 ^ K = 11010100(53 * 4 = 10为基212)
                       |

与右移的情况下仅仅是这个过程的一面镜子,也是在基地10例如类似,如果我在基地10有123456,我转变右3个位置,我得到123这千分之十二万三千四百五十六(整数除法),其中1000 = 10 3

Can you please explain the below lines, with some good examples.

A left arithmetic shift by n is equivalent to multiplying by 2n (provided the value does not overflow).

And:

A right arithmetic shift by n of a two's complement value is equivalent to dividing by 2n and rounding toward negative infinity. If the binary number is treated as ones' complement, then the same right-shift operation results in division by 2n and rounding toward zero.

解决方案

I will explain what happens in a base that we're more familiar with: 10.

In base 10, let's say you have a number N=123. Now, you "shift" this number to the left k=3 positions, filling the emptied digits with 0. So you get X=123000.

Note that X = N * 10k.

The case with base 2 is analogous.

 Example 1 (base 10)   |  Example 2 (base 2)
                       |
 N        =    123     |  N       =   110101 (53 in base 10)
 k        =      3     |  k       =        2 (in base 10)
 N << k   = 123000     |  N << k  = 11010100 (212 in base 10)
                       |
 10^k     =   1000     |  2^k     =      100 (in base 2; 4 in base 10)
 N * 10^k = 123000     |  N * 2^k = 11010100 (53 * 4 = 212 in base 10)
                       |

The case with right shift is simply a mirror of the process, and is also analogous in base 10. For example, if I have 123456 in base 10, and I "shift" right 3 positions, I get 123. This is 123456 / 1000 (integer division), where 1000 = 103.

这篇关于位运算 - 算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆