Field is empty
'%1' is not a valid email address.
Field must not be empty.
It must be at least %1 characters long.
It must be less than %1 characters long.
It must be the same as the field '%1' value.
Invalid symbol entered. Allowed symbols:'%1'.
Number expected.
Positive number expected.
Integer expected.
Positive integer expected.
Values must be in the range [%1 .. %2].
Symbol %1 is already entered in allowed character set.
Field value must be less than %1.
First character must be a Latin letter.
Sun
Mon
Tue
Wed
Thu
Fri
Sat
January
February
March
April
May
June
July
August
September
October
November
December
century
B.C.
%1 century
Error occurred while importing data on line:%1. Invalid value: '%2'. Error: %3
"Could not determine a data field separator. You may use one of the following characters to separate data fields: tab, ';' or ','."
%2/%3/%1%4
%2/%3/%1%4 %6:%7
N
S
E
W
yes
no
Invalid file format. We support only the following formats: %1
Please leave your email or phone number.
minutes
minutes
minute
minutes
minutes
minutes
minutes
minutes
minutes
minutes
minutes
minutes
minutes
hour
hours
hours
hours
hours
hours
hours
hours
hours
hours
hours
days
day
days
days
days
days
days
days
days
days
days
days
month
months
months
months
months
months
months
months
months
months
months
year
years
years
years
years
years
years
years
years
years
years
ago
%1 minutes ago
%1 minutes ago
%1 minute ago
%1 minutes ago
%1 minutes ago
%1 minutes ago
%1 minutes ago
%1 minutes ago
%1 minutes ago
%1 minutes ago
%1 minutes ago
%1 minutes ago
%1 minutes ago
%1 hour ago
%1 hours ago
%1 hours ago
%1 hours ago
%1 hours ago
%1 hours ago
%1 hours ago
%1 hours ago
%1 hours ago
%1 hours ago
%1 hours ago
%1 days ago
%1 day ago
%1 days ago
%1 days ago
%1 days ago
%1 days ago
%1 days ago
%1 days ago
%1 days ago
%1 days ago
%1 days ago
%1 days ago
%1 month ago
%1 months ago
%1 months ago
%1 months ago
%1 months ago
%1 months ago
%1 months ago
%1 months ago
%1 months ago
%1 months ago
%1 months ago
%1 year ago
%1 years ago
%1 years ago
%1 years ago
%1 years ago
%1 years ago
%1 years ago
%1 years ago
%1 years ago
%1 years ago
%1 years ago
HTML code for site/blog
Appearance
Sample
Close
Sofia Vasilievna
ProfessionalUnits converters

Binary, inverse and complement codes

Binary, one's complement (inverse) and two's complement codes (calculator was created by user's request)
Timur2010-03-23 19:43:59
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

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.

Binary, inverse and complement codesCreative Commons Attribution/Share-Alike License 3.0 (Unported)
 Range:
 Binary code:
 Inverse code (one's complement):
 Complement code (two's complement):


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 2^n

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 2^4. Or, which is the same, complement code "complements" binary code to 2^n, 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.

Several examples

7-3=4
0111 binary 7
1101 two's complement of 3
0100 result of addition 4

-1+7=6
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)

7+1=8

00111 binary 7
00001 binary 1
01110 carries
01000 result of addition 8 - overflow

Two last carries are 01. This gives signal of overflow

-7+7=0
00111 binary 7
01001 two's complement of 7
11110 carries
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 2^n-1, (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.

Not suitable?

View all calculators
(240 calculators in total. )
Request a calculator
 

Comments

Spam filter
Send comment