Programing

비트 보수 연산자 (~ 틸드)는 어떻게 작동합니까?

crosscheck 2020. 5. 27. 21:39
반응형

비트 보수 연산자 (~ 틸드)는 어떻게 작동합니까?


~ 2가 -3과 같은 이유는 무엇입니까? ~운영자 는 어떻게 작동합니까?


음수 는 양수에 대한 2의 보수 로 저장됩니다 . 예를 들어 다음은 2의 보수에서 -2를 나타냅니다. (8 비트)

1111 1110

이것을 얻는 방법은 숫자의 이진 표현을 취하고 보수를 취하고 (모든 비트를 뒤집 음) 1을 더하는 것입니다. 두 개는 0000 0010으로 시작하고 비트를 뒤집 으면 1111 1101이됩니다. 하나를 추가하면 위의 결과를 얻습니다. 첫 번째 비트는 부호 비트이며 음수를 의미합니다.

따라서 ~ 2 = -3을 얻는 방법을 살펴 보겠습니다.

다시 두 가지가 있습니다.

0000 0010

모든 비트를 뒤집기 만하면 다음과 같이됩니다.

1111 1101

2의 보수에서 -3의 모양은 무엇입니까? 양수 3 : 0000 0011로 시작하여 모든 비트를 1111 1100으로 뒤집고 1을 추가하여 음수 값 (-3), 1111 1101이되도록합니다.

따라서 단순히 2의 비트를 뒤집 으면 2의 보수 표현 인 -3을 얻게됩니다.

보수 연산자 (~) JUST FLIPS BITS. 이 비트들을 해석하는 것은 기계에 달려 있습니다.


~ 값의 비트를 뒤집습니다.

숫자가 비트 단위로 표시되는 방식과 관련이있는 이유 ~2무엇입니까? -3숫자는 2의 보수 로 표시됩니다 .

2는 이진수 값입니다

00000010

그리고 ~ 2는 비트를 뒤집어서 값이 다음과 같습니다.

11111101

-3의 이진 표현입니다.


다른 사람들이 언급 한 ~것처럼 비트를 뒤집어 놓고 (1에서 0으로, 0에서 1로 변경) 2의 보수 가 사용되므로 결과를 얻습니다.

한 가지 더해야 할 점은 2의 보수가 사용되는 이유 입니다. 따라서 음수에 대한 연산이 양수에 대한 연산과 동일합니다. 0을 얻기 위해 추가해야 할 -3숫자로 생각 3하면이 숫자는 1101임을 알 수 있습니다. 이진수 추가는 10이 아닌 2에 도달 할 때 하나만 소지하는 초등학교 (10 진수) 추가와 같습니다. .

 1101 +
 0011 // 3
    =
10000
    =
 0000 // lose carry bit because integers have a constant number of bits.

따라서 1101됩니다 -3, 당신이 얻을 비트 플립 0010이있다.


이 작업은 부정이 아닌 보완입니다.

~ 0 = -1이라고 생각하고 거기서부터 작업하십시오.

부정 알고리즘은 "보완, 증분"입니다.

알고 계십니까? 역수 대칭 인 "1의 보수"도 있으며 0과 -0을 모두 갖습니다.


이 질문에 대한 답변이 오래 전 게시 된 것을 알고 있지만 동일한 답변을 공유하고 싶었습니다.

숫자의 보수를 찾으려면 먼저 이진수를 찾으십시오. 여기에서 10 진수 는 이진 형식으로 2표시됩니다 0000 0010. 이제 이진 표현의 모든 자릿수를 뒤집어 (모든 1을 0으로, 0을 모두 1로 뒤집 음) 1을 보완하면 다음과 같은 결과가 나타납니다.

0000 0010 → 1111 1101

이것은 십진수 2의 보수입니다. 그리고 첫 번째 비트, 즉 부호 비트는 이진수에서 1이므로 부호는 저장된 숫자에 대해 음수 임을 의미합니다 . (여기서 언급 된 숫자는 2 아니라 2의 보수입니다).

이제 숫자가 2의 보수로 저장되므로 (1의 보수에 1을 더한 1을 차지함)이 이진수 1111 1101를 10 진수 로 표시 하려면 먼저 2의 보수를 찾아야합니다.

1111 1101 → 0000 0010 + 1 → 0000 0011

이것은 2의 보수입니다. 이진수의 10 진수 표현은 0000 0011입니다 3. 그리고 부호 비트는 위에서 언급 한 것과 같으므로 결과 답은 -3입니다.

힌트 : 이 절차를주의 깊게 읽으면, 보수 연산자의 결과는 실제로 숫자 (오퍼레이터-이 연산자가 적용되는 숫자)에 음수 부호를 더한 것임을 알 수 있습니다. 다른 번호로도 시도 할 수 있습니다.


int a = 4; System.out.println (~ a); 결과는 다음과 같습니다 .-5

Java에서 정수의 '~'는 1의 1의 보수를 나타냅니다. 예를 들어 나는 ~ 4를 취하고 있는데, 이는 이진 표현 0100을 의미합니다. 첫째, 정수의 길이는 4 바이트, 즉 4 * 8 (1 바이트의 경우 8 비트) = 32입니다. 따라서 시스템 메모리에서 4는 0000 0000 0000 0000 0000 0000 0000 0100으로 표시됩니다. ~ 연산자는 위의 바이너리에 대해 1의 보수를 수행합니다.

즉 1111 1111 1111 1111 1111 1111 1111 1011-> 1의 보수는 최상위 비트가 1이면 no (-또는 +)의 부호를 나타내고 0이면 부호는 '-'이고 0이면 부호는 '+'입니다 이 결과는 음수이며, 자바에서는 음수가 2의 보수 형식으로 저장됩니다. 획득 한 결과는 2의 보수로 변환해야합니다 (먼저 1의 보수를 수행하고 1을 1의 보수를 추가하십시오). 가장 중요한 비트 1을 제외한 모든 비트가 0이됩니다 (숫자의 부호 표시, 나머지 31 비트는 1111 1111 1111 1111 1111 1111 1111 1011 (~ 연산자의 획득 결과) 1000 0000 0000 0000 0000 0000 0000 0100 (1의 보수)

1 (2의 보수)

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


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..

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)+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.

~(-11) =10 ;

The above is true if printf("%d",~0); we get result: -1;

But printf("%u",~0) than result: 4294967295 on 32 bit machine.



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).

Then it converts all the 1 in the number to 0,and all the zeros to 1;then the number will become 1111 1101.

that is the 2's complement representation of -3.

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.


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

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)


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


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.

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.

Then later it would take the negative number of 2 which is -2.

-2 is represented as 1000 0010 (8 bit operator) in sign and magnitude.

Later it adds a 1 to the LSB (1000 0010 + 1) which gives you 1000 0011.

Which is -3.


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.

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 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.


Basically action is a complement not a negation .

Here x= ~x produce results -(x+1) always .

x = ~2

-(2+1)

-3

참고URL : https://stackoverflow.com/questions/791328/how-does-the-bitwise-complement-operator-tilde-work

반응형