Why is it that ~2 is equal to -3? How does ~
operator work?
为什么~2等于-3? 〜运算符如何工作?
12 个解决方案
#1
Remember that negative numbers are stored as the two's complement of the positive counterpart. As an example, here's the representation of -2 in two's complement: (8 bits)
请记住,负数存储为正对应的两个补码。作为一个例子,这里是二进制补码中-2的表示:(8位)
1111 1110
The way you get this is by taking the binary representation of a number, taking its complement (inverting all the bits) and adding one. Two starts as 0000 0010, and by inverting the bits we get 1111 1101. Adding one gets us the result above. The first bit is the sign bit, implying a negative.
你得到这个的方法是取一个数字的二进制表示,取其补码(反转所有位)并加一个。两个开始为0000 0010,通过反转我们得到的位1111 1101.添加一个得到我们上面的结果。第一位是符号位,暗示为负。
So let's take a look at how we get ~2 = -3:
那么让我们来看看我们如何得到~2 = -3:
Here's two again:
这是两个:
0000 0010
Simply flip all the bits and we get:
只需翻转所有位,我们得到:
1111 1101
Well, what's -3 look like in two's complement? Start with positive 3: 0000 0011, flip all the bits to 1111 1100, and add one to become negative value (-3), 1111 1101.
嗯,在两个补码中-3是什么样的?从正3:0000 0011开始,将所有位翻转到1111 1100,并添加一个变为负值(-3),1111 1101。
So if you simply invert the bits in 2, you get the two's complement representation of -3.
因此,如果您只是将2中的位反转,则得到-3的二进制补码表示。
The complement operator (~) JUST FLIPS BITS. It is up to the machine to interpret these bits.
#2
~
flips the bits in the value.
〜翻转值中的位。
Why ~2
is -3
has to do with how numbers are represented bitwise. Numbers are represented as two's complement.
为什么~2是-3与数字按位表示的方式有关。数字表示为两个补码。
So, 2 is the binary value
所以,2是二进制值
00000010
And ~2 flips the bits so the value is now:
并且~2翻转位,所以现在的值是:
11111101
Which, is the binary representation of -3.
其中,是-3的二进制表示。
#3
As others mentioned ~
just flipped bits (changes one to zero and zero to one) and since two's complement is used you get the result you saw.
正如其他人提到的那样〜只是翻转位(将一个变为零,零变为一个),并且由于使用了两个补码,您将得到您看到的结果。
One thing to add is why two's complement is used, this is so that the operations on negative numbers will be the same as on positive numbers. Think of -3
as the number to which 3
should be added in order to get zero and you'll see that this number is 1101
, remember that binary addition is just like elementary school (decimal) addition only you carry one when you get to two rather than 10.
有一点需要补充的是为什么使用两个补码,这样负数的运算与正数的运算相同。将-3视为要添加3以便获得零的数字,你会看到这个数字是1101,记住二进制加法就像小学(十进制)加法只有当你到达时才携带一个两个而不是十个。
1101 +
0011 // 3
=
10000
=
0000 // lose carry bit because integers have a constant number of bits.
Therefore 1101
is -3
, flip the bits you get 0010
which is two.
因此1101是-3,翻转你得到的比特为0010的两个。
#4
This operation is a complement, not a negation.
这种操作是一种补充,而不是否定。
Consider that ~0 = -1, and work from there.
考虑到~0 = -1,并从那里开始工作。
The algorithm for negation is, "complement, increment".
否定算法是“补充,增量”。
Did you know? There is also "one's complement" where the inverse numbers are symmetrical, and it has both a 0 and a -0.
你知道吗?还有“一个补码”,其中倒数是对称的,并且它具有0和-0。
#5
int a=4; System.out.println(~a); Result would be :-5
int a = 4;的System.out.println(〜A);结果将是:-5
'~' of any integer in java represents 1's complement of the no. for example i am taking ~4,which means in binary representation 0100. first , length of an integer is four bytes,i.e 4*8(8 bits for 1 byte)=32. So in system memory 4 is represented as 0000 0000 0000 0000 0000 0000 0000 0100 now ~ operator will perform 1's complement on the above binary no
java中任何整数的'〜'代表no的1的补码。例如,我采用~4,这意味着在二进制表示0100中。首先,整数的长度是4个字节,即4 * 8(1个字节的8位)= 32。因此在系统存储器4中表示为0000 0000 0000 0000 0000 0000 0000 0100现在〜运算符将对上述二进制执行1的补码no
i.e 1111 1111 1111 1111 1111 1111 1111 1011->1's complement the most significant bit represents sign of the no(either - or +) if it is 1 then sign is '-' if it is 0 then sign is '+' as per this our result is a negative number, in java the negative numbers are stored in 2's complement form, the acquired result we have to convert into 2's complement( first perform 1's complement and just add 1 to 1's complement). all the one will become zeros,except most significant bit 1(which is our sign representation of the number,that means for remaining 31 bits 1111 1111 1111 1111 1111 1111 1111 1011 (acquired result of ~ operator) 1000 0000 0000 0000 0000 0000 0000 0100 (1's complement)
即1111 1111 1111 1111 1111 1111 1111 1011-> 1的补码最高有效位表示否的符号(或者 - 或者+)如果是1那么符号是' - '如果它是0那么符号是'+'按照这个我们的结果是负数,在java中,负数以2的补码形式存储,获得的结果我们必须转换为2的补码(首先执行1的补码,只需加1到1的补码)。除了最重要的位1(这是我们的数字的符号表示,这意味着剩余的31位1111 1111 1111 1111 1111 1111 1111 1011(获得的〜运算符的结果)1000 0000 0000 0000 0000 0000 0000除了最重要的位1之外,所有的都将变为零。 0100(1的补充)
1 (2's complement)
1000 0000 0000 0000 0000 0000 0000 0101 now the result is -5 check out this link for the video <[Bit wise operators in java] https://youtu.be/w4pJ4cGWe9Y
1000 0000 0000 0000 0000 0000 0000 0101现在结果为-5检查此视频链接<[java中的位操作符] https://youtu.be/w4pJ4cGWe9Y
#6
I know the answer for this question is posted a long back, but I wanted to share my answer for the same.
我知道这个问题的答案发布了很久,但我想分享我的答案。
For finding the one’s complement of a number, first find its binary equivalent. Here, decimal number 2
is represented as 0000 0010
in binary form. Now taking its one’s complement by inverting (flipping all 1’s into 0’s and all 0’s into 1’s) all the digits of its binary representation, which will result in:
为了找到一个数字的补码,首先找到它的二进制等价物。这里,十进制数2以二进制形式表示为0000 0010。现在通过将其二进制表示的所有数字反转(将所有1翻转为0并将所有0翻译为1)来获取其一个补码,这将导致:
0000 0010 → 1111 1101
This is the one’s complement of the decimal number 2. And since the first bit, i.e., the sign bit is 1 in the binary number, it means that the sign is negative for the number it stored. (here, the number referred to is not 2 but the one’s complement of 2).
这是十进制数2的一个补码。并且由于第一个比特,即符号位在二进制数中是1,这意味着符号对于它存储的数字是负的。 (这里,所提到的数字不是2,而是2的补码)。
Now, since the numbers are stored as 2’s complement (taking the one’s complement of a number plus one), so to display this binary number, 1111 1101
, into decimal, first we need to find its 2’s complement, which will be:
现在,因为数字被存储为2的补码(取一个数的加1加一),所以要将这个二进制数1111 1101显示为十进制,首先我们需要找到它的2的补码,它将是:
1111 1101 → 0000 0010 + 1 → 0000 0011
This is the 2’s complement. The decimal representation of the binary number, 0000 0011
, is 3
. And, since the sign bit was one as mentioned above, so the resulting answer is -3
.
这是2的补充。二进制数0000 0011的十进制表示为3.并且,由于符号位是如上所述的一个,因此得到的答案是-3。
Hint: If you read this procedure carefully, then you would have observed that the result for the one’s complement operator is actually, the number (operand - on which this operator is applied) plus one with a negative sign. You can try this with other numbers too.
提示:如果你仔细阅读这个程序,那么你会发现一个补码运算符的结果实际上是数字(操作数 - 应用此运算符)加上一个带负号的结果。您也可以尝试使用其他数字。
#7
Simply ...........
As 2's complement of any number we can calculate by inverting all 1s to 0's and vice-versa than we add 1 to it..
作为任何数字的2的补码,我们可以通过将所有1反转为0来反过来,反之亦然,我们将它加1。
Here N= ~N produce results -(N+1) always. Because system store data in form of 2's complement which means it stores ~N like this.
这里N = ~N总是产生结果 - (N + 1)。因为系统以2的补码形式存储数据,这意味着它像这样存储~N。
~N = -(~(~N)+1) =-(N+1).
For example::
N = 10 = 1010
Than ~N = 0101
so ~(~N) = 1010
so ~(~N) +1 = 1011
Now point is from where Minus comes. My opinion is suppose we have 32 bit register which means 2^31 -1 bit involved in operation and to rest one bit which change in earlier computation(complement) stored as sign bit which is 1 usually. And we get result as ~10 = -11.
现在点是Minus的来源。我的观点是假设我们有32位寄存器,这意味着2 ^ 31 -1位涉及操作,并且休息一位在早期计算中变化(补码)存储为通常为1的符号位。我们得到的结果为~10 = -11。
~(-11) =10 ;
〜(-11)= 10;
The above is true if printf("%d",~0); we get result: -1;
如果是printf(“%d”,~0),则上述情况属实;我们得到结果:-1;
But printf("%u",~0) than result: 4294967295 on 32 bit machine.
但是printf(“%u”,~0)比32位机器上的结果:4294967295。
#8
The Bitwise complement operator(~) is a unary operator.
按位补码运算符(〜)是一元运算符。
It works as per the following methods
它按照以下方法工作
First it converts the given decimal number to its corresponding binary value.That is in case of 2 it first convert 2 to 0000 0010 (to 8 bit binary number).
首先,它将给定的十进制数转换为其对应的二进制值。在2的情况下,它首先将2转换为0000 0010(到8位二进制数)。
Then it converts all the 1 in the number to 0,and all the zeros to 1;then the number will become 1111 1101.
然后它将数字中的所有1转换为0,并将所有零转换为1;然后该数字将变为1111 1101。
that is the 2's complement representation of -3.
这是-3的2的补码表示。
In order to find the unsigned value using complement,i.e. simply to convert 1111 1101 to decimal (=4294967293) we can simply use the %u during printing.
为了使用补码找到无符号值,即。只需将1111 1101转换为十进制(= 4294967293),我们就可以在打印过程中简单地使用%u。
#9
I think for most people the confusion part comes from the difference between decimal number and signed binary number, so lets clarify it first:
我认为对于大多数人来说,混淆部分来自十进制数和带符号二进制数之间的差异,所以我们先说明一下:
for human decimal world: 01 means 1, -01 means -1, for computer's binary world: 101 means 5 if it is unsigned. 101 means (-4 + 1) if is signed while the signed digit is at position x. | x
对于人类十进制世界:01表示1,-01表示-1,对于计算机的二进制世界:如果它是无符号,则表示5表示5。 101表示(-4 + 1)if如果在有符号数字位于x的位置进行签名。 | X
so 2's flipped bit = ~2 = ~(010) = 101 = -4 + 1 = -3 the confusion comes from mixing up the signed result(101=-3) and the unsinged result(101=5)
所以2的翻转位= ~2 =〜(010)= 101 = -4 + 1 = -3混淆来自混合签名结果(101 = -3)和未结果的结果(101 = 5)
#10
First we have to split the given digit into its binary digits and then reverse it by adding at the last binary digit.After this execution we have to give opposite sign to the previous digit that which we are finding the complent ~2=-3 Explanation: 2s binary form is 00000010 changes to 11111101 this is ones complement ,then complented 00000010+1=00000011 which is the binary form of three and with -sign I.e,-3
首先,我们必须将给定的数字拆分成二进制数字,然后通过添加最后一个二进制数字来反转它。执行完毕后,我们必须给前一个数字给出相反的符号,我们找到的数字是完全的~2 = -3说明:2s二进制形式是00000010更改为11111101这是一个补码,然后完成00000010 + 1 = 00000011这是二进制形式的三个并且带有-sign Ie,-3
#11
The bit-wise operator is a unary operator which works on sign and magnitude method as per my experience and knowledge.
按位运算符是一元运算符,根据我的经验和知识处理符号和幅度方法。
For example ~2 would result in -3.
例如~2将导致-3。
This is because the bit-wise operator would first represent the number in sign and magnitude which is 0000 0010 (8 bit operator) where the MSB is the sign bit.
这是因为逐位运算符将首先表示符号和幅度的数字,其为0000 0010(8位运算符),其中MSB是符号位。
Then later it would take the negative number of 2 which is -2.
然后它将采用负数2,即-2。
-2 is represented as 1000 0010 (8 bit operator) in sign and magnitude.
-2表示符号和幅度为1000 0010(8位运算符)。
Later it adds a 1 to the LSB (1000 0010 + 1) which gives you 1000 0011.
之后它向LSB(1000 0010 + 1)添加1,它给你1000 0011。
Which is -3.
这是-3。
#12
Javascript tilde (~) coerces a given value to the one's complement--all bits are inverted. That's all tilde does. It's not sign opinionated. It neither adds nor subtracts any quantity.
Javascript代字号(〜)将给定值强制转换为一个补码 - 所有位都被反转。这都是代字号。这不符合自己的观点。它既不增加也不减去任何数量。
0 -> 1
1 -> 0
...in every bit position [0...integer nbr of bits - 1]
On standard desktop processors using high-level languages like JavaScript, BASE10 signed arithmetic is the most common, but keep in mind, it's not the only kind. Bits at the CPU level are subject to interpretation based on a number of factors. At the 'code' level, in this case JavaScript, they are interpreted as a 32-bit signed integer by definition (let's leave floats out of this). Think of it as quantum, those 32-bits represent many possible values all at once. It depends entirely on the converting lens you view them through.
在使用JavaScript等高级语言的标准桌面处理器上,BASE10签名算法是最常见的,但请记住,它不是唯一的类型。 CPU级别的位可以根据许多因素进行解释。在'代码'级别,在这种情况下JavaScript,根据定义它们被解释为32位有符号整数(让我们留下浮点数)。把它想象成量子,那些32位代表了许多可能的值。这完全取决于您查看它们的转换镜头。
JavaScript Tilde operation (1's complement)
BASE2 lens
~0001 -> 1110 - end result of ~ bitwise operation
BASE10 Signed lens (typical JS implementation)
~1 -> -2
BASE10 Unsigned lens
~1 -> 14
All of the above are true at the same time.
所有这些都是同时存在的。
#1
Remember that negative numbers are stored as the two's complement of the positive counterpart. As an example, here's the representation of -2 in two's complement: (8 bits)
请记住,负数存储为正对应的两个补码。作为一个例子,这里是二进制补码中-2的表示:(8位)
1111 1110
The way you get this is by taking the binary representation of a number, taking its complement (inverting all the bits) and adding one. Two starts as 0000 0010, and by inverting the bits we get 1111 1101. Adding one gets us the result above. The first bit is the sign bit, implying a negative.
你得到这个的方法是取一个数字的二进制表示,取其补码(反转所有位)并加一个。两个开始为0000 0010,通过反转我们得到的位1111 1101.添加一个得到我们上面的结果。第一位是符号位,暗示为负。
So let's take a look at how we get ~2 = -3:
那么让我们来看看我们如何得到~2 = -3:
Here's two again:
这是两个:
0000 0010
Simply flip all the bits and we get:
只需翻转所有位,我们得到:
1111 1101
Well, what's -3 look like in two's complement? Start with positive 3: 0000 0011, flip all the bits to 1111 1100, and add one to become negative value (-3), 1111 1101.
嗯,在两个补码中-3是什么样的?从正3:0000 0011开始,将所有位翻转到1111 1100,并添加一个变为负值(-3),1111 1101。
So if you simply invert the bits in 2, you get the two's complement representation of -3.
因此,如果您只是将2中的位反转,则得到-3的二进制补码表示。
The complement operator (~) JUST FLIPS BITS. It is up to the machine to interpret these bits.
#2
~
flips the bits in the value.
〜翻转值中的位。
Why ~2
is -3
has to do with how numbers are represented bitwise. Numbers are represented as two's complement.
为什么~2是-3与数字按位表示的方式有关。数字表示为两个补码。
So, 2 is the binary value
所以,2是二进制值
00000010
And ~2 flips the bits so the value is now:
并且~2翻转位,所以现在的值是:
11111101
Which, is the binary representation of -3.
其中,是-3的二进制表示。
#3
As others mentioned ~
just flipped bits (changes one to zero and zero to one) and since two's complement is used you get the result you saw.
正如其他人提到的那样〜只是翻转位(将一个变为零,零变为一个),并且由于使用了两个补码,您将得到您看到的结果。
One thing to add is why two's complement is used, this is so that the operations on negative numbers will be the same as on positive numbers. Think of -3
as the number to which 3
should be added in order to get zero and you'll see that this number is 1101
, remember that binary addition is just like elementary school (decimal) addition only you carry one when you get to two rather than 10.
有一点需要补充的是为什么使用两个补码,这样负数的运算与正数的运算相同。将-3视为要添加3以便获得零的数字,你会看到这个数字是1101,记住二进制加法就像小学(十进制)加法只有当你到达时才携带一个两个而不是十个。
1101 +
0011 // 3
=
10000
=
0000 // lose carry bit because integers have a constant number of bits.
Therefore 1101
is -3
, flip the bits you get 0010
which is two.
因此1101是-3,翻转你得到的比特为0010的两个。
#4
This operation is a complement, not a negation.
这种操作是一种补充,而不是否定。
Consider that ~0 = -1, and work from there.
考虑到~0 = -1,并从那里开始工作。
The algorithm for negation is, "complement, increment".
否定算法是“补充,增量”。
Did you know? There is also "one's complement" where the inverse numbers are symmetrical, and it has both a 0 and a -0.
你知道吗?还有“一个补码”,其中倒数是对称的,并且它具有0和-0。
#5
int a=4; System.out.println(~a); Result would be :-5
int a = 4;的System.out.println(〜A);结果将是:-5
'~' of any integer in java represents 1's complement of the no. for example i am taking ~4,which means in binary representation 0100. first , length of an integer is four bytes,i.e 4*8(8 bits for 1 byte)=32. So in system memory 4 is represented as 0000 0000 0000 0000 0000 0000 0000 0100 now ~ operator will perform 1's complement on the above binary no
java中任何整数的'〜'代表no的1的补码。例如,我采用~4,这意味着在二进制表示0100中。首先,整数的长度是4个字节,即4 * 8(1个字节的8位)= 32。因此在系统存储器4中表示为0000 0000 0000 0000 0000 0000 0000 0100现在〜运算符将对上述二进制执行1的补码no
i.e 1111 1111 1111 1111 1111 1111 1111 1011->1's complement the most significant bit represents sign of the no(either - or +) if it is 1 then sign is '-' if it is 0 then sign is '+' as per this our result is a negative number, in java the negative numbers are stored in 2's complement form, the acquired result we have to convert into 2's complement( first perform 1's complement and just add 1 to 1's complement). all the one will become zeros,except most significant bit 1(which is our sign representation of the number,that means for remaining 31 bits 1111 1111 1111 1111 1111 1111 1111 1011 (acquired result of ~ operator) 1000 0000 0000 0000 0000 0000 0000 0100 (1's complement)
即1111 1111 1111 1111 1111 1111 1111 1011-> 1的补码最高有效位表示否的符号(或者 - 或者+)如果是1那么符号是' - '如果它是0那么符号是'+'按照这个我们的结果是负数,在java中,负数以2的补码形式存储,获得的结果我们必须转换为2的补码(首先执行1的补码,只需加1到1的补码)。除了最重要的位1(这是我们的数字的符号表示,这意味着剩余的31位1111 1111 1111 1111 1111 1111 1111 1011(获得的〜运算符的结果)1000 0000 0000 0000 0000 0000 0000除了最重要的位1之外,所有的都将变为零。 0100(1的补充)
1 (2's complement)
1000 0000 0000 0000 0000 0000 0000 0101 now the result is -5 check out this link for the video <[Bit wise operators in java] https://youtu.be/w4pJ4cGWe9Y
1000 0000 0000 0000 0000 0000 0000 0101现在结果为-5检查此视频链接<[java中的位操作符] https://youtu.be/w4pJ4cGWe9Y
#6
I know the answer for this question is posted a long back, but I wanted to share my answer for the same.
我知道这个问题的答案发布了很久,但我想分享我的答案。
For finding the one’s complement of a number, first find its binary equivalent. Here, decimal number 2
is represented as 0000 0010
in binary form. Now taking its one’s complement by inverting (flipping all 1’s into 0’s and all 0’s into 1’s) all the digits of its binary representation, which will result in:
为了找到一个数字的补码,首先找到它的二进制等价物。这里,十进制数2以二进制形式表示为0000 0010。现在通过将其二进制表示的所有数字反转(将所有1翻转为0并将所有0翻译为1)来获取其一个补码,这将导致:
0000 0010 → 1111 1101
This is the one’s complement of the decimal number 2. And since the first bit, i.e., the sign bit is 1 in the binary number, it means that the sign is negative for the number it stored. (here, the number referred to is not 2 but the one’s complement of 2).
这是十进制数2的一个补码。并且由于第一个比特,即符号位在二进制数中是1,这意味着符号对于它存储的数字是负的。 (这里,所提到的数字不是2,而是2的补码)。
Now, since the numbers are stored as 2’s complement (taking the one’s complement of a number plus one), so to display this binary number, 1111 1101
, into decimal, first we need to find its 2’s complement, which will be:
现在,因为数字被存储为2的补码(取一个数的加1加一),所以要将这个二进制数1111 1101显示为十进制,首先我们需要找到它的2的补码,它将是:
1111 1101 → 0000 0010 + 1 → 0000 0011
This is the 2’s complement. The decimal representation of the binary number, 0000 0011
, is 3
. And, since the sign bit was one as mentioned above, so the resulting answer is -3
.
这是2的补充。二进制数0000 0011的十进制表示为3.并且,由于符号位是如上所述的一个,因此得到的答案是-3。
Hint: If you read this procedure carefully, then you would have observed that the result for the one’s complement operator is actually, the number (operand - on which this operator is applied) plus one with a negative sign. You can try this with other numbers too.
提示:如果你仔细阅读这个程序,那么你会发现一个补码运算符的结果实际上是数字(操作数 - 应用此运算符)加上一个带负号的结果。您也可以尝试使用其他数字。
#7
Simply ...........
As 2's complement of any number we can calculate by inverting all 1s to 0's and vice-versa than we add 1 to it..
作为任何数字的2的补码,我们可以通过将所有1反转为0来反过来,反之亦然,我们将它加1。
Here N= ~N produce results -(N+1) always. Because system store data in form of 2's complement which means it stores ~N like this.
这里N = ~N总是产生结果 - (N + 1)。因为系统以2的补码形式存储数据,这意味着它像这样存储~N。
~N = -(~(~N)+1) =-(N+1).
For example::
N = 10 = 1010
Than ~N = 0101
so ~(~N) = 1010
so ~(~N) +1 = 1011
Now point is from where Minus comes. My opinion is suppose we have 32 bit register which means 2^31 -1 bit involved in operation and to rest one bit which change in earlier computation(complement) stored as sign bit which is 1 usually. And we get result as ~10 = -11.
现在点是Minus的来源。我的观点是假设我们有32位寄存器,这意味着2 ^ 31 -1位涉及操作,并且休息一位在早期计算中变化(补码)存储为通常为1的符号位。我们得到的结果为~10 = -11。
~(-11) =10 ;
〜(-11)= 10;
The above is true if printf("%d",~0); we get result: -1;
如果是printf(“%d”,~0),则上述情况属实;我们得到结果:-1;
But printf("%u",~0) than result: 4294967295 on 32 bit machine.
但是printf(“%u”,~0)比32位机器上的结果:4294967295。
#8
The Bitwise complement operator(~) is a unary operator.
按位补码运算符(〜)是一元运算符。
It works as per the following methods
它按照以下方法工作
First it converts the given decimal number to its corresponding binary value.That is in case of 2 it first convert 2 to 0000 0010 (to 8 bit binary number).
首先,它将给定的十进制数转换为其对应的二进制值。在2的情况下,它首先将2转换为0000 0010(到8位二进制数)。
Then it converts all the 1 in the number to 0,and all the zeros to 1;then the number will become 1111 1101.
然后它将数字中的所有1转换为0,并将所有零转换为1;然后该数字将变为1111 1101。
that is the 2's complement representation of -3.
这是-3的2的补码表示。
In order to find the unsigned value using complement,i.e. simply to convert 1111 1101 to decimal (=4294967293) we can simply use the %u during printing.
为了使用补码找到无符号值,即。只需将1111 1101转换为十进制(= 4294967293),我们就可以在打印过程中简单地使用%u。
#9
I think for most people the confusion part comes from the difference between decimal number and signed binary number, so lets clarify it first:
我认为对于大多数人来说,混淆部分来自十进制数和带符号二进制数之间的差异,所以我们先说明一下:
for human decimal world: 01 means 1, -01 means -1, for computer's binary world: 101 means 5 if it is unsigned. 101 means (-4 + 1) if is signed while the signed digit is at position x. | x
对于人类十进制世界:01表示1,-01表示-1,对于计算机的二进制世界:如果它是无符号,则表示5表示5。 101表示(-4 + 1)if如果在有符号数字位于x的位置进行签名。 | X
so 2's flipped bit = ~2 = ~(010) = 101 = -4 + 1 = -3 the confusion comes from mixing up the signed result(101=-3) and the unsinged result(101=5)
所以2的翻转位= ~2 =〜(010)= 101 = -4 + 1 = -3混淆来自混合签名结果(101 = -3)和未结果的结果(101 = 5)
#10
First we have to split the given digit into its binary digits and then reverse it by adding at the last binary digit.After this execution we have to give opposite sign to the previous digit that which we are finding the complent ~2=-3 Explanation: 2s binary form is 00000010 changes to 11111101 this is ones complement ,then complented 00000010+1=00000011 which is the binary form of three and with -sign I.e,-3
首先,我们必须将给定的数字拆分成二进制数字,然后通过添加最后一个二进制数字来反转它。执行完毕后,我们必须给前一个数字给出相反的符号,我们找到的数字是完全的~2 = -3说明:2s二进制形式是00000010更改为11111101这是一个补码,然后完成00000010 + 1 = 00000011这是二进制形式的三个并且带有-sign Ie,-3
#11
The bit-wise operator is a unary operator which works on sign and magnitude method as per my experience and knowledge.
按位运算符是一元运算符,根据我的经验和知识处理符号和幅度方法。
For example ~2 would result in -3.
例如~2将导致-3。
This is because the bit-wise operator would first represent the number in sign and magnitude which is 0000 0010 (8 bit operator) where the MSB is the sign bit.
这是因为逐位运算符将首先表示符号和幅度的数字,其为0000 0010(8位运算符),其中MSB是符号位。
Then later it would take the negative number of 2 which is -2.
然后它将采用负数2,即-2。
-2 is represented as 1000 0010 (8 bit operator) in sign and magnitude.
-2表示符号和幅度为1000 0010(8位运算符)。
Later it adds a 1 to the LSB (1000 0010 + 1) which gives you 1000 0011.
之后它向LSB(1000 0010 + 1)添加1,它给你1000 0011。
Which is -3.
这是-3。
#12
Javascript tilde (~) coerces a given value to the one's complement--all bits are inverted. That's all tilde does. It's not sign opinionated. It neither adds nor subtracts any quantity.
Javascript代字号(〜)将给定值强制转换为一个补码 - 所有位都被反转。这都是代字号。这不符合自己的观点。它既不增加也不减去任何数量。
0 -> 1
1 -> 0
...in every bit position [0...integer nbr of bits - 1]
On standard desktop processors using high-level languages like JavaScript, BASE10 signed arithmetic is the most common, but keep in mind, it's not the only kind. Bits at the CPU level are subject to interpretation based on a number of factors. At the 'code' level, in this case JavaScript, they are interpreted as a 32-bit signed integer by definition (let's leave floats out of this). Think of it as quantum, those 32-bits represent many possible values all at once. It depends entirely on the converting lens you view them through.
在使用JavaScript等高级语言的标准桌面处理器上,BASE10签名算法是最常见的,但请记住,它不是唯一的类型。 CPU级别的位可以根据许多因素进行解释。在'代码'级别,在这种情况下JavaScript,根据定义它们被解释为32位有符号整数(让我们留下浮点数)。把它想象成量子,那些32位代表了许多可能的值。这完全取决于您查看它们的转换镜头。
JavaScript Tilde operation (1's complement)
BASE2 lens
~0001 -> 1110 - end result of ~ bitwise operation
BASE10 Signed lens (typical JS implementation)
~1 -> -2
BASE10 Unsigned lens
~1 -> 14
All of the above are true at the same time.
所有这些都是同时存在的。