Our user asked as to create online calculator for converting entered integer number into it's binary form as well us display it's inverse and complement codes /743/
Below is the calculator which does the task. It accepts positive or negative integer number and outputs above-mentioned binary codes.
Below the calculator, as usual, is the explanation what 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. That is, for positive numbers it shows binary representation of number (cause there are no inverse or compliment for positive), and for negative number it shows it's presentation from positive in inverse and complement codes.
So, here is some theory
Binary code is the binary representation of unsigned integer. If we're talking about computers, there is certain number of bits used to represent the number. So, total range which can be represented by n-bits is
Inverse code or one's complement is simply 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 all about?
These codes were invented to make sign operations more comfortable (for machines). Since I'm a kind of person who likes to learn by example, I'll explain this statement on examples.
Let's assume we have computer with 4-bits binary numbers. Total range which can be represented by 4-bits is 16 - 0,1,... 15
00 - 0000
15 - 1111
But these are unsigned numbers and are not of much use. We need to introduce sign. So, half of range is taken for positive numbers (eight, including zero), and half of range - for negative (also eight). Note that machine considers zero as positive number, unlike usual math.
So, our positives are 0,...,7, and negatives are -1,...,-8.
To distinguish positive and negative numbers we assign left-most bit as sign bit. Zero in sign bit tells as that this is positive number and one - negative.
Positive numbers are represented by plain binary code
0 - 0000
1 - 0001
7 - 0111
But how negative numbers can 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 addition scheme for both addition and subtraction, thus simplifying the design of ALU (arithmetic and logical unit - part of processor). Also, this representation easily detects on 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 two last carries, including carry beyond 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 right-most bit can be safely ignored.
Some examples with carries and fifth bit (bit beyond right-most bit)
00111 binary 7
00001 binary 1
01000 result of addition 8 - overflow
Two last carries are 01. This gives signal of overflow
00111 binary 7
01001 two's complement of 7
10000 result of addition 16 - but fifth bit can be ignored, real result is 0
Two last carries are 11. There is no overflow, so 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 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 addition scheme should employ cyclic carry and is more complex. Besides, range, which can be represented by n-bits is reduced by 1, since 1111 is busy as inverted 0000 - negative zero. So, it is less convenient.