C语言 谁能解释为什么在 C 代码中“>>2”移位意味着“除以 4”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13577174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:35:24  来源:igfitidea点击:

Can anyone explain why '>>2' shift means 'divided by 4' in C codes?

cshiftdivide

提问by user573566

I know and understand the result.

我知道并理解结果。

For example:

例如:

<br>
 7 (decimal) = 00000111 (binary) <br>

and 7 >> 2 = 00000001 (binary) <br>
00000001 (binary) is same as 7 / 4 = 1 <br>
So 7 >> 2 = 7 / 4 <br>
<br>

But I'd like to know how this logic is created.
Can anyone elaborate on this logic ?
(Maybe it's just popped up in a genius head ?)

And is there any other similar logics like this ?

但我想知道这个逻辑是如何创建的。
任何人都可以详细说明这个逻辑吗?
(也许它只是在天才头脑中突然出现?)

还有其他类似的逻辑吗?

回答by Aniket Inge

It didn't "pop-up" in a genius' head. Right shifting binary numbers would divide a number by 2 and left shifting the numbers would multiply it by 2. This is because 10is 2 in binary. Multiplying a number by 10(be it binary or decimal or hexadecimal) appends a 0to the number(which is effectively left shifting). Similarly, dividing by 10(or 2) removes a binary digit from the number(effectively right shifting). This is how the logic really works.

它没有在天才的头脑中“弹出”。右移二进制数会将数字除以 2,左移数字会将其乘以 2。这是因为102 在二进制中。将一个数字乘以10(无论是二进制、十进制还是十六进制)会将 a 附加0到该数字(这实际上是左移)。类似地,除以10(或 2)从数字中删除一个二进制数字(有效地右移)。这就是逻辑真正起作用的方式。

There are plenty of such bit-twiddlery(a word I invented a minute ago) in computer world.

bit-twiddlery在计算机世界中有很多这样的(我一分钟前发明的一个词)。

http://graphics.stanford.edu/~seander/bithacks.htmlHere is for the starters.

http://graphics.stanford.edu/~seander/bithacks.html这是初学者。

This is my favorite book: http://www.amazon.com/Hackers-Delight-Edition-Henry-Warren/dp/0321842685/ref=dp_ob_image_bkon bit-twiddlery.

这是我最喜欢的书:http: //www.amazon.com/Hackers-Delight-Edition-Henry-Warren/dp/0321842685/ref=dp_ob_image_bk on bit-twiddlery。

回答by Dietrich Epp

It is actually defined that way in the C standard.

它实际上在 C 标准中是这样定义的。

From section 6.5.7:

来自第 6.5.7 节:

The result of E1 >> E2 is E1 right-shifted E2 bit positions. [...] the value of the result is the integral part of the quotient of E1 / 2E2

E1 >> E2 的结果是 E1 右移 E2 位位置。[...] 结果的值是 E1 / 2 E2商的整数部分

On most architectures, x >> 2is only equal to x / 4for non-negative numbers. For negative numbers, it usually rounds the opposite direction.

在大多数架构上,x >> 2仅等于x / 4非负数。对于负数,它通常向相反的方向舍入。

Compilers have always been able to optimize x / 4into x >> 2. This technique is called "strength reduction", and even the oldest compilers can do this. So there is no benefit to writing x / 4as x >> 2.

编译器一直能够优化x / 4x >> 2. 这种技术被称为“强度降低”,即使是最老的编译器也能做到这一点。所以写x / 4为没有任何好处x >> 2

回答by Kevin Fegan

I think you are confused by the "2"in:

我认为您对以下内容感到困惑"2"

7 >> 2

and are thinking it should divide by 2.

并认为它应该除以 2。

The "2"here means shift the number ("7"in this case) "2"bit positions to the right.

"2"这里装置移位(数"7"在这种情况下)"2"位的位置到右侧。

Shifting a number "1"bit position to the right will have the effect of dividing by 2:

将数字"1"位位置向右移动将产生除以 2 的效果:

8 >> 1 = 4    // In binary: (00001000) >> 1 = (00000100)

and shifting a number "2"bit positions to the right will have the effect of dividing by 4:

"2"向右移动一个数字位将产生除以 4 的效果:

8 >> 2 = 2    // In binary: (00001000) >> 2 = (00000010)

回答by Shishir Gupta

Elaborating on Aniket Inge'sanswer:

详细说明Aniket Inge 的回答:

Number: 30710= 1001100112

数字:307 10= 100110011 2

How multiply by 10 works in decimal system

在十进制系统中乘以 10 的工作原理

10 * (30710)

10 * (307 10)

= 10 * (3*102+ 7*100)

= 10 * (3*10 2+ 7*10 0)

= 3*102+1+ 7*100+1

= 3*10 2+1+ 7*10 0+1

= 3*103+ 7*101

= 3*10 3+ 7*10 1

= 307010

= 3070 10

= 30710<< 1

= 307 10<< 1

Similarly multiply by 2 in binary,

类似地,在二进制中乘以 2

2 * (1001100112)

2 * (100110011 2)

= 2 * (1*28+ 1*25+ 1*24+ 1*211*20)

= 2 * (1*2 8+ 1*2 5+ 1*2 4+ 1*2 11*2 0)

= 1*28+1+ 1*25+1+ 1*24+1+ 1*21+11*20+1

= 1*2 8+1+ 1*2 5+1+ 1*2 4+1+ 1*2 1+11*2 0+1

= 1*29+ 1*26+ 1*25+ 1*22+ 1*21

= 1*2 9+ 1*2 6+ 1*2 5+ 1*2 2+ 1*2 1

= 10011001102

= 1001100110 2

= 1001100112<< 1

= 100110011 2<< 1

回答by Kaaf

An easy way to see why it works, is to look at the familiar decimal ten-based number system, 050 is fifty, shift it to the right, it becomes 005, five, equivalent to dividing it by 10. The same thing with shifting left, 050 becomes 500, five hundred, equivalent to multiplying it by 10.

一个简单的方法来了解它为什么起作用,是看熟悉的十进制十进制数字系统,050是五十,把它向右移,它变成005,五,相当于把它除以10。 与移位相同左边,050变成了500,五百,相当于乘以10。

All the other numeral systems work the same way.

所有其他数字系统的工作方式相同。

回答by Debobroto Das

Its inherent in the binary number system used in computer.

它固有地存在于计算机中使用的二进制数系统中。

a similar logic is --- left shifting 'n' times means multiplying by 2^n.

类似的逻辑是 --- 左移“n”次意味着乘以 2^n。

回答by Sam I am says Reinstate Monica

they do that because shifting is more efficient than actual division. you're just moving all the digits to the right or left, logically multiplying/dividing by 2 per shift

他们这样做是因为转移比实际除法更有效。您只是将所有数字向右或向左移动,逻辑上每班乘以/除以 2

If you're wondering why 7/4 = 1, that's because the rest of the result, (3/4) is truncated off so that it's an interger.

如果您想知道为什么 7/4 = 1,那是因为结果的其余部分 (3/4) 被截断,因此它是一个整数。

回答by nbloqs

Just my two cents: I did not see any mention to the fact that shifting right does not always produce the same results as dividing by 2. Since right shifting rounds toward negative infinity and integer division rounds to zero, some values (like -1 in two's complement) will just not work as expected when divided.

只是我的两分钱:我没有看到任何提到右移并不总是产生与除以 2 相同的结果的事实。由于右移向负无穷大和整数除法四舍五入到零,一些值(如 -1 in二进制补码)在划分时不会按预期工作。

回答by Cyberpks

You can call it an idea of a genius mind or just the need of the computer language.

您可以将其称为天才思想的想法,也可以称之为计算机语言的需要。

To my belief, a Computer as a device never divides or multiplies numbers, rather it only has a logic of adding or simply shifting the bits from here to there. You can make an algorithm work by telling your computer to multiply, subtract them up, but when the logic reaches for actual processing, your results will be either an outcome of shifting of bits or just adding of bits.

在我看来,作为设备的计算机永远不会对数字进行除法或乘法运算,它只有一种将位从这里加到那里或简单地将位移到那里的逻辑。您可以通过告诉您的计算机将它们相乘、相减来使算法工作,但是当逻辑达到实际处理时,您的结果将是位移位或只是位相加的结果。

You can simply think that for getting the result of a number being divided by 4, the computer actually right shifts the bits to two places, and gives the result:

你可以简单地认为,为了得到一个数除以 4 的结果,计算机实际上将位右移了两个位置,并给出了结果:

7 in 8-bit binary = 00000111
Shift Right 2 places = 00000001 // (Which is for sure equal to Decimal 1)

Further examples:
//-- We can divide 9 by four by Right Shifting 2 places
9 in 8-bit binary = 00001001
Shift right 2 places: 00000010 // (Which is equal to 9/4 or Decimal 2)

A person with deep knowledge of assembly language programming can explain it with more examples. If you want to know the actual sense behind all this, I guess you need to study bit level arithmetic and assembly language of computer.

对汇编语言编程有深入了解的人可以用更多的例子来解释它。如果你想知道这一切背后的实际意义,我猜你需要学习计算机的位级算术和汇编语言。