Our user asked us to create an online calculator for converting entered integer numbers into their binary form as well as display their inverse and complement codes /743/
Below is the calculator which does the task. It accepts positive or negative integer numbers and outputs the above-mentioned binary codes.
Below the calculator, as usual, is the explanation of what it is all about.
Update: From the comments, I can see that people misinterpret calculator results — my mistake. Calculator merely applied described algorithm to any entered number. Now I change it to avoid confusion. For positive numbers, it shows a binary representation of a number (cause there are no inverse or compliment for positive). For negative numbers, it shows its presentation from positive in inverse and complement codes.
So, here is a theory.
Binary code is the binary representation of unsigned integers. If we're talking about computers, there is a certain number of bits used to represent the number. So, the total range which n-bits can represent is
Inverse code or one's complement is inverted binary code of a number. That is, all zeroes become ones and all ones become zeroes.
Complement code or two's complement is inverse code plus one
Now, what is it all about?
These codes were invented to make sign operations more comfortable (for machines). Since I'm the kind of person who likes to learn by example, I'll explain this statement in examples.
Let's assume we have a computer with 4-bits binary numbers. A total range, which 4-bits can represent, is 16 - 0,1,... 15
00 - 0000
15 - 1111
But these are unsigned numbers and are not of much use. We need to introduce a sign. So, half of the range is taken for positive numbers (eight, including zero), and half of the range - for negative (also eight). Note that the machine considers zero as a positive number, unlike usual math.
So, our positives are 0,...,7, and negatives are -1,...,-8.
To distinguish positive and negative numbers, we assign the left-most bit as sign bit. Zero in sign bit tells as that this is a positive number and one - negative.
Positive numbers are represented by plain binary code
0 - 0000
1 - 0001
7 - 0111
But how can negative numbers be represented? Here comes the complement code.
That is, -7 complement is
binary 7 = 0111
inverse 7 = 1000
complement 7 = 1001
Note that binary 1001 is 9, which differs from -7 by 16, or . Or, which is the same, complement code "complements" binary code to , i.e. 7+9=16
This proved to be very useful for machine computation - usage of complement code to represent negatives allows engineers to use an addition scheme for both addition and subtraction, thus simplifying the design of ALU (arithmetic and logical unit - part of the processor). This representation easily detects overflow, and then there are not enough bits to represent the given number.
0111 binary 7
1101 two's complement of 3
0100 result of addition 4
1111 two's complement of 1
0111 binary 7
0110 result of addition 6
Overflow is detected by looking at the two last carries, including carrying beyond the right-most bit. If carry bits are 11 or 00, there is no overflow; if carry bits are 01 or 10, there is overflow. And, if there is no overflow, carry beyond the right-most bit can be safely ignored.
Some examples with carries and a fifth bit (a bit beyond right-most bit)
00111 binary 7
00001 binary 1
01000 result of addition 8 - overflow
The two last carries are 01. This gives a signal of overflow
00111 binary 7
01001 two's complement of 7
10000 result of addition 16 - but fifth bit can be ignored; the real result is 0
The two last carries are 11. There is no overflow, so the correct result is zero.
Overflow check can be done by simple XOR-ing two last carry bits.
Because of these convenient properties, two's complement is the most common method to represent negative numbers on computers.
P.S. Inverse code, or one's complement, "complements" binary code to , (all ones). It also can be used to represent negatives, but the addition scheme should employ cyclic carry and is more complex. The range, which n-bits can represent, is reduced by 1 since 1111 is busy as inverted 0000 - negative zero. So, it is less convenient.