development

부호있는 숫자에 대해 부호와 크기보다 2의 보수를 선호하는 이유는 무엇입니까?

big-blog 2020. 5. 12. 19:13
반응형

부호있는 숫자에 대해 부호와 크기보다 2의 보수를 선호하는 이유는 무엇입니까?


이진수로 -1을 나타 내기 위해 2의 보수가 사용되는 이유가 있다면 궁금합니다. 비트를 뒤집고 1을 추가합니까?

-1은 (보다 직관적으로) 10000001이 아니라 11111111 (2의 보수)로 표시됩니다. 10000001은 첫 번째 비트를 음의 플래그로 사용하는 이진 1입니다.

면책 조항 : 나는 직업에 이진 산술에 의존하지 않습니다!


음수를 처리하기 위해 추가에 특별한 논리가 필요하지 않도록 완료되었습니다. Wikipedia 기사를 확인하십시오 .

2와 -1의 두 숫자가 있다고 가정 해보십시오. 숫자를 나타내는 "직관적 인"방식으로 각각 0010이고 1001(크기는 4 비트를 고수합니다). 2의 보수 방법으로, 그들은있다 00101111. 이제 그것들을 추가하고 싶다고합시다.

2의 보수 추가는 매우 간단합니다. 일반적으로 숫자를 추가하면 끝에있는 캐리 비트가 삭제됩니다. 따라서 다음과 같이 추가됩니다.

  0010
+ 1111
=10001
= 0001 (discard the carry)

0001 "2 + (-1)"의 예상 결과 인 1입니다.

그러나 "직관적 인"방법에서는 추가가 더 복잡합니다.

  0010
+ 1001
= 1011

어느 것이 -3입니까? 이 경우 단순 추가는 작동하지 않습니다. 숫자 중 하나는 음수이며 다른 경우 다른 알고리즘을 사용해야합니다.

이 "직관적 인"저장 방법의 경우 뺄셈은 덧셈과 다른 연산이므로 덧셈하기 전에 숫자를 추가로 확인해야합니다. 가장 기본적인 연산 (더하기, 빼기 등)을 가능한 빨리하기를 원하므로 가능한 가장 간단한 알고리즘을 사용할 수있는 방식으로 숫자를 저장해야합니다.

또한 "직관적 인"저장 방법에는 두 가지 0이 있습니다.

0000  "zero"
1000  "negative zero"

직관적으로 같은 숫자이지만 저장시 두 개의 다른 값이 있습니다. 모든 응용 프로그램은 0이 아닌 값이 음의 0이 아닌지 확인하기 위해 추가 단계를 수행해야합니다.

이 방법으로 ints를 저장하면 또 다른 보너스가 있습니다.이 경우 값이 저장되는 레지스터의 너비를 확장해야합니다. 2의 보수로 8 비트 레지스터에 4 비트 숫자를 저장하면 반복됩니다. 가장 중요한 비트 :

    0001 (one, in four bits)
00000001 (one, in eight bits)
    1110 (negative two, in four bits)
11111110 (negative two, in eight bits)

작은 단어의 부호 비트를보고 큰 단어의 너비에 닿을 때까지 반복하면됩니다.

이 방법을 사용하면 기존 비트를 지워야합니다. 이는 패딩 외에도 추가 작업입니다.

    0001 (one, in four bits)
00000001 (one, in eight bits)
    1010 (negative two, in four bits)
10000010 (negative two, in eight bits)

두 경우 모두 4 비트를 추가로 설정해야하지만 "직관적 인"경우 5 번째 비트도 지워야합니다. 모든 애플리케이션에 존재하는 가장 기본적이고 일반적인 작업 중 하나에서 아주 작은 단계입니다.


Wikipedia 는 모든 것을 말합니다.

2의 보수 시스템은 덧셈과 뺄셈 회로가 피연산자의 부호를 검사하여 뺄셈인지 뺄셈을 요구하지 않는 이점이 있습니다. 이 속성을 사용하면 시스템을보다 간단하게 구현하고보다 정밀한 산술을 쉽게 처리 할 수 ​​있습니다. 또한, 0은 단일 표현만을 가지므로 1의 보수 시스템에 존재하는 음의 0과 관련된 미묘함을 피할 수 있습니다.

다시 말해, 더하기는 같지만 숫자는 음수입니다.


이 질문이 오래되었지만 2 센트를 넣겠습니다.

이것을 설명하기 전에 기본으로 돌아가 봅시다. 2 '보수는 1의 보수 + 1입니다. 이제 1의 보수는 무엇이고 또한 그 의미는 무엇입니까?

n- 비트 수와 1의 보수의 합은 해당 n- 비트로 표시 될 수있는 가장 높은 수를 제공합니다. 예:

 0010 (2 in 4 bit system)
+1101 (1's complement of 2)
___________________________
 1111  (the highest number that we can represent by 4 bits)

이제 결과에 1을 더 추가하면 어떻게 될까요? 오버플로가 발생합니다.

결과 1 0000는 0입니다 (4 비트 숫자로 작업 할 때 (왼쪽의 1은 오버플로입니다))

그래서

Any n-bit number + its 1's complement = max n-bit number
Any n-bit number + its 1'complement + 1 = 0 ( as explained above, overflow will occur as we are adding 1 to max n-bit number)

그런 다음 누군가 1의 보수 + 1을 2의 보수로 부르기로 결정했습니다. 따라서 위의 진술은 다음과 같습니다. 모든 n '비트 숫자 + 2의 보수 = 0은 숫자의 2의 보수 =-(해당 숫자)를 의미합니다

이 모든 것이 하나의 질문을 낳습니다. 왜 우리는 n 비트의 (n-1) 만 사용하여 양수를 나타낼 수 있고 가장 왼쪽의 n 번째 비트가 부호를 나타냅니다 (가장 왼쪽 비트의 0은 + ve 숫자를 의미하고 1은 -ve 번호). 예를 들어 32 비트가 1 인 경우 -ve 숫자 인 양수를 나타 내기 위해 Java에서 int의 처음 31 비트 만 사용하는 이유는 무엇입니까?

 1100 (lets assume 12 in 4 bit system)
+0100(2's complement of 12)
___________________________

1 0000 (캐리 1 오버플로로 결과는 0입니다)

따라서 (n + 2'complement of n) = 0의 시스템은 여전히 ​​작동합니다. 여기에서 유일한 모호성은 2의 12의 보수는 0100이며 2의 보수 시스템에서 -12를 나타내는 것 외에는 모호하게도 +8을 나타냅니다.

이 문제는 양수가 가장 왼쪽 비트에 항상 0 인 경우 해결됩니다. 이 경우 2의 보수는 항상 가장 왼쪽 비트에 1을 가지며 2의 보수 숫자와 + ve 숫자를 나타내는 동일한 비트 세트의 모호성을 갖지 않습니다.


2의 보수 는 부호없는 숫자로 감는 것처럼 정상적인 방법으로 더하기와 빼기를 수행 할 수 있습니다. 또한 -0을 방지합니다 (일반적인 비트 단위 비교 방법으로 0과 같지 않은 0을 나타내는 별도의 방법).


이것은 숫자의 합과 차이를 단순화하는 것입니다. 2의 보수로 체계화 된 음수와 양수의 합은 정상적인 방법으로 합산하는 것과 같습니다.


연산의 일반적인 구현은 "비트를 뒤집고 1을 더하는 것"이지만, 근거를보다 명확하게하는 다른 방법이 있습니다. 2의 보수는 각 비트가 다음 2의 제곱을 제어하는 ​​일반적인 부호없는 표현을 취하고 가장 중요한 항을 음수로 만들면 얻을 수있는 형태입니다.

8 비트 값 취하기 a 7 a 6 a 5 a 4 a 3 a 2 a 1 a 0

The usual unsigned binary interpretation is:
27*a7 + 26*a6 + 25*a5 + 24*a4 + 23*a3 + 22*a2 + 21*a1 + 20*a0
11111111 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255

The two's complement interpretation is:
-27*a7 + 26*a6 + 25*a5 + 24*a4 + 23*a3 + 22*a2 + 21*a1 + 20*a0
11111111 = -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1

None of the other bits change meaning at all, and carrying into a7 is "overflow" and not expected to work, so pretty much all of the arithmetic operations work without modification (as others have noted). Sign-magnitude generally inspect the sign bit and use different logic.


Two's complement allows negative and positive numbers to be added together without any special logic.

If you tried to add 1 and -1 using your method
10000001 (-1)
+00000001 (1)
you get
10000010 (-2)

Instead, by using two's complement, we can add

11111111 (-1)
+00000001 (1) you get
00000000 (0)

The same is true for subtraction.

Also, if you try to subtract 4 from 6 (two positive numbers) you can 2's complement 4 and add the two together 6 + (-4) = 6 - 4 = 2

This means that subtraction and addition of both positive and negative numbers can all be done by the same circuit in the cpu.


To expand on others answers:

In two's complement

  • Adding is the same mechanism as plain positive integers adding.
  • Subtracting doesn't change too
  • Multiplication too!

Division does require a different mechanism.

All these are true because two's complement is just normal modular arithmetic, where we choose to look at some numbers as negative by subtracting the modulo.


Reading the answers to this question, I came across this comment [edited].

2's complement of 0100(4) will be 1100. Now 1100 is 12 if I say normally. So, when I say normal 1100 then it is 12, but when I say 2's complement 1100 then it is -4? Also, in Java when 1100 (lets assume 4 bits for now) is stored then how it is determined if it is +12 or -4 ?? – hagrawal Jul 2 at 16:53

In my opinion, the question asked in this comment is quite interesting and so I'd like first of all to rephrase it and then to provide an answer and an example.

QUESTION – How can the system establish how one or more adjacent bytes have to be interpreted? In particular, how can the system establish whether a given sequence of bytes is a plain binary number or a 2's complement number?

ANSWER – The system establishes how to interpret a sequence of bytes through types. Types define

  • how many bytes have to be considered
  • how those bytes have to be interpreted

EXAMPLE – Below we assume that

  • char's are 1 byte long
  • short's are 2 bytes long
  • int's and float's are 4 bytes long

Please note that these sizes are specific to my system. Although pretty common, they can be different from system to system. If you're curious of what they are on your system, use the sizeof operator.

First of all we define an array containing 4 bytes and initialize all of them to the binary number 10111101, corresponding to the hexadecimal number BD.

// BD(hexadecimal) = 10111101 (binary)
unsigned char   l_Just4Bytes[ 4 ]   =   { 0xBD, 0xBD, 0xBD, 0xBD };

Then we read the array content using different types.

unsigned char and signed char

// 10111101 as a PLAIN BINARY number equals 189
printf( "l_Just4Bytes as unsigned char  -> %hi\n", *( ( unsigned char* )l_Just4Bytes ) );

// 10111101 as a 2'S COMPLEMENT number equals -67
printf( "l_Just4Bytes as signed char    -> %i\n", *( ( signed char* )l_Just4Bytes ) );

unsigned short and short

// 1011110110111101 as a PLAIN BINARY number equals 48573
printf( "l_Just4Bytes as unsigned short -> %hu\n", *( ( unsigned short* )l_Just4Bytes ) );

// 1011110110111101 as a 2'S COMPLEMENT number equals -16963
printf( "l_Just4Bytes as short          -> %hi\n", *( ( short* )l_Just4Bytes ) );

unsigned int, int and float

// 10111101101111011011110110111101 as a PLAIN BINARY number equals 3183328701
printf( "l_Just4Bytes as unsigned int   -> %u\n", *( ( unsigned int* )l_Just4Bytes ) );

// 10111101101111011011110110111101 as a 2'S COMPLEMENT number equals -1111638595
printf( "l_Just4Bytes as int            -> %i\n", *( ( int* )l_Just4Bytes ) );

// 10111101101111011011110110111101 as a IEEE 754 SINGLE-PRECISION number equals -0.092647
printf( "l_Just4Bytes as float          -> %f\n", *( ( float* )l_Just4Bytes ) );

The 4 bytes in RAM (l_Just4Bytes[ 0..3 ]) always remain exactly the same. The only thing that changes is how we interpret them.

Again, we tell the system how to interpret them through types.

For instance, above we have used the following types to interpret the contents of the l_Just4Bytes array

  • unsigned char: 1 byte in plain binary
  • signed char: 1 byte in 2's complement
  • unsigned short: 2 bytes in plain binary notation
  • short: 2 bytes in 2's complement
  • unsigned int: 4 bytes in plain binary notation
  • int: 4 bytes in 2's complement
  • float: 4 bytes in IEEE 754 single-precision notation

[EDIT] This post has been edited after the comment by user4581301. Thank you for taking the time to drop those few helpful lines!


You can watch Professor Jerry Cain from Stanford explaining the two's complement, in the second lecture (the explanation regarding the 2's complement starts around 13:00) in the series of lectures called Programming Paradigms available to watch from Standford's YouTube channel. Here's the link to the lecture series: http://www.youtube.com/view_play_list?p=9D558D49CA734A02.


Two's complement is used because it is simpler to implement in circuitry and also does not allow a negative zero.

If there are x bits, two's complement will range from +(2^x/2+1) to -(2^x/2). One's complement will run from +(2^x/2) to -(2^x/2), but will permit a negative zero (0000 is equal to 1000 in a 4 bit 1's complement system).


Well, your intent is not really to reverse all bits of your binary number. It is actually to subtract each its digit from 1. It's just a fortunate coincidence that subtracting 1 from 1 results in 0 and subtracting 0 from 1 results in 1. So flipping bits is effectively carrying out this subtraction.

But why are you finding each digit's difference from 1? Well, you're not. Your actual intent is to compute the given binary number's difference from another binary number which has the same number of digits but contains only 1's. For example if your number is 10110001, when you flip all those bits, you're effectively computing (11111111 - 10110001).

This explains the first step in the computation of Two's Complement. Now let's include the second step -- adding 1 -- also in the picture.

Add 1 to the above binary equation:

11111111 - 10110001 + 1

What do you get? This:

100000000 - 10110001

This is the final equation. And by carrying out those two steps you're trying to find this, final difference: the binary number subtracted from another binary number with one extra digit and containing zeros except at the most signification bit position.

But why are we hankerin' after this difference really? Well, from here on, I guess it would be better if you read the Wikipedia article.


We perform only addition operation for both addition and subtraction. We add the second operand to the first operand for addition. For subtraction we add the 2's complement of the second operand to the first operand.

With a 2's complement representation we do not need separate digital components for subtraction—only adders and complementers are used.


It's worthwhile to note that on some early adding machines, before the days of digital computers, subtraction would be performed by having the operator enter values using a different colored set of legends on each key (so each key would enter nine minus the number to be subtracted), and press a special button would would assume a carry into a calculation. Thus, on a six-digit machine, to subtract 1234 from a value, the operator would hit keys that would normally indicate "998,765" and hit a button to add that value plus one to the calculation in progress. Two's complement arithmetic is simply the binary equivalent of that earlier "ten's-complement" arithmetic.


The advantage of performing subtraction by the complement method is reduction in the hardware
complexity.The are no need of the different digital circuit for addition and subtraction.both addition and subtraction are performed by adder only.


A major advantage of two's-complement representation which hasn't yet been mentioned here is that the lower bits of a two's-complement sum, difference, or product are dependent only upon the corresponding bits of the operands. The reason that the 8 bit signed value for -1 is 11111111 is that subtracting any integer whose lowest 8 bits are 00000001 from any other integer whose lowest 8 bits are 0000000 will yield an integer whose lowest 8 bits are 11111111. Mathematically, the value -1 would be an infinite string of 1's, but all values within the range of a particular integer type will either be all 1's or all 0's past a certain point, so it's convenient for computers to "sign-extend" the most significant bit of a number as though it represented an infinite number of 1's or 0's.

Two's-complement is just about the only signed-number representation that works well when dealing with types larger than a binary machine's natural word size, since when performing addition or subtraction, code can fetch the lowest chunk of each operand, compute the lowest chunk of the result, and store that, then load the next chunk of each operand, compute the next chunk of the result, and store that, etc. Thus, even a processor which requires all additions and subtractions to go through a single 8-bit register can handle 32-bit signed numbers reasonably efficiently (slower than with a 32-bit register, of course, but still workable).

When using of the any other signed representations allowed by the C Standard, every bit of the result could potentially be affected by any bit of the operands, making it necessary to either hold an entire value in registers at once or else follow computations with an extra step that would, in at least some cases, require reading, modifying, and rewriting each chunk of the result.


There are different types of representations those are:

  1. unsigned number representation
  2. signed number representation
  3. one's complement representation
  4. Two's complement representation

-Unsigned number representation used to represent only positive numbers

-Signed number representation used to represent positive as well as a negative number. In Signed number representation MSB bit represents sign bit and rest bits represents the number. When MSB is 0 means number is positive and When MSB is 1 means number is negative.

Problem with Signed number representation is that there are two values for 0.

Problem with one's complement representation is that there are two values for 0.

But if we use Two's complement representation then there will only one value for 0 that's why we represent negative numbers in two's complement form.

Source:Why negative numbers are stored in two's complement form bytesofgigabytes


One satisfactory answer of why Two2's Complement is used to represent negative numbers rather than One's Complement system is that Two's Complement system solves the problem of multiple representations of 0 and the need for end-around-carry which exist in the One's complement system of representing negative numbers.

For more information Visit https://en.wikipedia.org/wiki/Signed_number_representations

For End-around-carry Visit https://en.wikipedia.org/wiki/End-around_carry


because CPU manufacturers are lazy!

참고URL : https://stackoverflow.com/questions/1125304/why-prefer-twos-complement-over-sign-and-magnitude-for-signed-numbers

반응형