1左移31(1<<<<> 31)如何获得最大的int值?这是我的想法和在网上找到的一些解释 [英] How does 1 left shift by 31 (1 << 31) work to get maximum int value? Here are my thoughts and some explanations I found online

查看:174
本文介绍了1左移31(1<<<<> 31)如何获得最大的int值?这是我的想法和在网上找到的一些解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对位操作还很陌生,我试图弄清楚(1<< 31)-1的工作原理.

I'm fairly new to bit manipulation and I'm trying to figure out how (1 << 31) - 1 works.

首先,我知道1<< 31是

First I know that 1 << 31 is

1000000000000000000000000000

我知道它实际上是最小int值的补数,但是当我试图找出(1<< 31)-1时,我发现一个解释指出,这只是

and I know it's actually complement of minimum int value, but when I tried to figure out (1 << 31) - 1, I found an explanation states that, it's just

10000000000000000000000000000000 - 1 = 01111111111111111111111111111111

我真的很想相信它,因为它确实很简单.但这是真的吗?如果不是,为什么它恰好是正确的?

I was almost tempted to believe it since it's really straightforward. But is this what really happening? If it's not, why it happens to be right?

我最初的想法是,真正的过程应该是:-1的二进制补码是

My original thought was that, the real process should be: the two's complement of -1 is

11111111111111111111111111111111

然后(1<< 31)-1 =

then (1 << 31) - 1 =

(1)01111111111111111111111111111111

最左边的1被放弃,那么我们有int的最大值.

the leftmost 1 is abandoned, then we have maximum value of int.

我对哪个是正确的感到很困惑.

I'm really confused about which one is right.

推荐答案

两者都是! 1<< 31是:

It's both! 1 << 31 is:

1000 0000 0000 0000 0000 0000 0000 0000

减去1得到:

0111 1111 1111 1111 1111 1111 1111 1111

关于带符号的数字的补码布局的一个不错的功能之一是加法和减法与 unsigned 的数字完全相同.因此10000 ... 000表示二进制补码中的负数,最大的负数在这种情况下为-2,147,483,648,从中减去1会导致最大正数2,147,483,647的折回,但是排列了二进制补数这样我们就可以假装它是一个 unsigned 数字,因此减法并不复杂.从10000 ... 000中减去1只会将前导1减为0,并借用一堆1,与十进制一样,您会得到一堆9:10000-1 = 9999.

One of the nice features about the two's complement layout of signed numbers is that addition and subtraction are exactly the same operations as they are for unsigned numbers. So 10000...000 represents a negative number in two's complement, the largest negative number, which is -2,147,483,648 in this case, and subtracting 1 from it causes wrap-around to the largest positive number, 2,147,483,647, but two's complement numbers are arranged so that we can pretend it's an unsigned number instead, so the subtraction is uncomplicated. Subtracting 1 from 10000...000 simply drops the leading 1 to a 0, and borrows a bunch of 1s, same as in decimal you get a bunch of 9s: 10000 - 1 = 9999.

从数学上讲,(a - b)(a + (-b))相同,所以我们可以改为(1 << 31) + (-1):

It's also true that mathematically, (a - b) is the same as (a + (-b)), so we can do (1 << 31) + (-1) instead:

  1000 0000 0000 0000 0000 0000 0000 0000    (1 << 31)
  1111 1111 1111 1111 1111 1111 1111 1111    (-1)
-----------------------------------------
1 0111 1111 1111 1111 1111 1111 1111 1111    +

  0111 1111 1111 1111 1111 1111 1111 1111    (truncate)

A 1是高端的,结果被截断为32位整数后就会丢失.

A 1 is carried out of the high end, and lost once the result is truncated back into a 32-bit integer.

无论哪种方式,该模式的高端都为0,然后填充为1s,是表示任意宽度的2的补码整数的最大正值的表示.

Either way, that pattern, with a single 0 at the high end, then filled by 1s, is the representation of the maximum positive value for a two's complement integer of any width.

如果您愿意,还有其他方法可以生成该模式,例如~(1 << 31)(-1 >>> 1)(其中>>>表示逻辑右移),它与整数的宽度无关.

There are other ways to generate that pattern if you prefer, such as ~(1 << 31), and (-1 >>> 1) (where >>> means logical shift right) which is agnostic of the width of the integer.

这篇关于1左移31(1&lt;&lt;&lt;&lt;&gt; 31)如何获得最大的int值?这是我的想法和在网上找到的一些解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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