- Diff Checker 2 1 4 234 X 256
- Diff Checker 2 1 4 234 X 2
- Diff Checker 2 1 4 234 X 24
- Diff Checker 2 1 4 234 X 27
- Diff Checker 2 1 4 234 X 22
On Unix-like operating systems, the diff command analyzes two files and prints the lines that are different. In essence, it outputs a set of instructions for how to change one file to make it identical to the second file.
Chromes task manager shows the tab I have this website open in as using almost 1.5 GB of RAM. That is an absurd amount. That is even after clearing cookies and reloading the tab. Diff Checker is a free online diff tool that gives you the text differences between two blocks of text. Diff Checker 2 1 4 234 2; Diff Checker 2 1 4 234 3; Diff Checker 2 1 4 234 Download; MINIMAL ABSTRACT Flyer Template 364175 PSD 4 x 6 in 300 DPI 12.73 MB Demo & Sale Page This is a flyer template for all kind of party! You can modify everything very easy and quick. Changing the color style, picture, typo is no problem. It is well-assorted in. Shift+Alt+2- creates 2 columns. Ctrl+2- move selected file to column 2. This is for side by side comparison. For actual diff, there is the diff function other already mentioned. Unfortunately, I can't find a way to make columns scroll at the same time, which would be a nice feature. Get the free 'General Differential Equation Solver' widget for your website, blog, Wordpress, Blogger, or iGoogle. Find more Mathematics widgets in Wolfram Alpha.
This document covers the GNU/Linux version of diff.
Description
The diff software does not actually change the files it compares. However, it can optionally generate a script (if the -e option is specified) for the program ed or ex which can be used to apply the changes.
For example, consider two files, file1.txt and file2.txt.
If file1.txt contains the following four lines of text: Vst plugins for adobe audition mac.
.and file2.txt contains these four lines:
.then we can use diff to automatically display for us which lines differ between the two files with this command:
.and the output will be:
Let's take a look at what this output means. The important thing to remember is that when diff is describing these differences to you, it's doing so in a prescriptive context: it's telling you how to change the first file to make it match the second file.
The first line of the diff output will contain:
- line numbers corresponding to the first file,
- a letter (a for add, c for change, or d for delete), and
- line numbers corresponding to the second file.
In our output above, '2,4c2,4' means: 'Lines 2 through 4 in the first file need to be changed to match lines 2 through 4 in the second file.' It then tells us what those lines are in each file:
- Lines preceded by a < are lines from the first file;
- lines preceded by > are lines from the second file.
The three dashes ('---') merely separate the lines of file 1 and file 2.
Let's look at another example. Let's say our two files look like this:
file1.txt:
file2.txt:
Output:
Diff Checker 2 1 4 234 X 256
Here, the output is telling us 'After line 2 in the first file, a line needs to be added: line 3 from the second file.' It then shows us what that line is.
Now let's see what it looks like when diff tells us we need to delete a line.
file1:
file2:
Our command:
The output:
Here, the output is telling us 'You need to delete line 4 in the first file so that both files sync up at line 3.' It then shows us the contents of the line that needs to be deleted.
Viewing diff Output In Context
The examples above show the default output of diff. It's intended to be read by a computer, not a human, so for human purposes, sometimes it helps to see the context of the changes.
GNUdiff, which is the version most linux users will be using, offers two different ways to do this: 'context mode' and 'unified mode'.
To view differences in context mode, use the -c option. For instance, let's say file1.txt and file2.txt contain the following:
file1.txt:
file2.txt:
Let's look at the contextual output for the diff of these two files. Our command is:
And our output looks like this:
The first two lines of this output show us information about our 'from' file (file 1) and our 'to' file (file 2). It lists the file name, modification date, and modification time of each of our files, one per line. The 'from' file is indicated by '***', and the 'to' file is indicated by '---'.
The line '***************' is just a separator.
The next line has three asterisks ('***') followed by a line range from the first file (in this case lines 1 through 4, separated by a comma). Then four asterisks ('****').
Then it shows us the contents of those lines. If the line is unchanged, it's prefixed by two spaces. If the line is changed, however, it's prefixed by an indicative character and a space. The character meanings are as follows:
character | meaning |
---|---|
! | Indicates that this line is part of a group of one or more lines that needs to change. There is a corresponding group of lines prefixed with '!' in the other file's context as well. |
+ | Indicates a line in the second file that needs to be added to the first file. |
- | Indicates a line in the first file that needs to be deleted. |
After the lines from the first file, there are three dashes ('---'), then a line range, then four dashes ('----'). This indicates the line range in the second file that will sync up with our changes in the first file.
If there is more than one section that needs to change, diff will show these sections one after the other. Lines from the first file will still be indicated with '***', and lines from the second file with '---'.
Unified Mode
Unified mode (the -u option) is similar to context mode, but it doesn't display any redundant information. Here's an example, using the same input files as our last example:
file1.txt:
file2.txt:
Our command:
The output:
The output is similar to above, but as you can see, the differences are 'unified' into one set.
Finding Differences In Directory Contents
diff can also compare directories by providing directory names instead of file names. See the Examples section.
Using diff To Create An Editing Script
The -e option tells diff to output a script, which can be used by the editing programs ed or ex, that contains a sequence of commands. The commands are a combination of c (change), a (add), and d (delete) which, when executed by the editor, will modify the contents of file1 (the first file specified on the diff command line) so that it matches the contents of file2 (the second file specified).
Let's say we have two files with the following contents:
file1.txt:
file2.txt
We can run the following command to analyze the two files with diff and produce a script to create a file identical to file2.txt from the contents of file1.txt:
diff -e file1.txt file2.txt
.and the output will look like this:
Notice that the changes are listed in reverse order: the changes closer to the end of the file are listed first, and changes closer to the beginning of the file are listed last. This order is to preserve line numbering; if we made the changes at the beginning of the file first, that might change the line numbers later in the file. So the script starts at the end, and works backwards.
Here, the script is telling the editing program: 'change line 5 to (the following line), and change lines 2 through 3 to (the following two lines).'
Next, we should save the script to a file. We can redirect the diff output to a file using the > operator, like this:
This command will not display anything on the screen (unless there is an error); instead, the output is redirected to the file my-ed-script.txt. If my-ed-script.txt doesn't exist, it will be created; if it exists already, it will be overwritten.
If we now check the contents of my-ed-script.txt with the cat Wolf casino games. command.
.we will see the same script we saw displayed above.
There's still one thing missing, though: we need the script to tell ed to actually write the file. All that's missing from the script is the w command, which will write the changes. We can add this to our script by echoing the letter 'w' and using the >> operator to add it to our file. (The >> operator is similar to the > operator. It redirects output to a file, but instead of overwriting the destination file, it appends to the end of the file.) The command looks like this:
Now, we can check to see that our script has changed by running the cat command again:
Now our script, when issued to ed, will make the changes and write the changes to disk.
So how do we get ed to do this?
We can issue this script to ed with the following command, telling it to overwrite our original file. The dash ('-') tells ed to read from the standard input, and the < operator directs our script to that input. In essence, the system enters whatever is in our script as input to the editing program. The command looks like this:
This command displays nothing, but if we look at the contents of our original file.
.we can see that file1.txt now matches file2.txt exactly.
Warning! In this example, ed overwrote the contents of our original file, file1.txt. After running the script, the original text of file1.txt disappears, so make sure you understand what you're doing before running these commands!
Commonly-Used diff Options
Here are some useful diff options to take note of:
-b | Ignore any changes which only change the amount of whitespace (such as spaces or tabs). |
-w | Ignore whitespace entirely. |
-B | Ignore blank lines when calculating differences. |
-y | Display output in two columns. |
These are only some of the most commonly-used diff options. What follows is a complete list of diff options and their function.
Syntax
Options
--normal | Output a 'normal' diff, which is the default. | ||||||
-q, --brief | Produce output only when files differ. If there are no differences, output nothing. | ||||||
-s, --report-identical-files | Report when two files are the same. | ||||||
-c, -CNUM, --context[=NUM] | Provide NUM (default 3) lines of context. | ||||||
-u, -UNUM, --unified[=NUM] | Provide NUM (default 3) lines of unified context. | ||||||
-e, --ed | Output an ed script. | ||||||
-n, --rcs | Output an RCS-format diff. | ||||||
-y, --side by side | Format output in two columns. | ||||||
-W, --width=NUM | Output at most NUM (default 130) print columns. | ||||||
--left-column | Output only the left column of common lines. | ||||||
--suppress-common-lines | Do not output lines common between the two files. | ||||||
-p, --show-c-function | For files that contain C code, also show each C function change. | ||||||
-F, --show-function-line=RE | Show the most recent line matching regular expression RE. | ||||||
--labelLABEL | When displaying output, use the label LABEL instead of the file name. This option can be issued more than once for multiple labels. | ||||||
-t, --expand-tabs | Expand tabs to spaces in output. | ||||||
-T, --initial-tab | Make tabs line up by prepending a tab if necessary. | ||||||
--tabsize=NUM | Define a tab stop as NUM (default 8) columns. | ||||||
--suppress-blank-empty | Suppress spaces or tabs before empty output lines. | ||||||
-l, --paginate | Pass output through pr to paginate. | ||||||
-r, --recursive | Recursively compare any subdirectories found. | ||||||
-N, --new-file | If a specified file does not exist, perform the diff as if it is an empty file. | ||||||
--unidirectional-new-file | Same as -n, but only applies to the first file. | ||||||
--ignore-file-name-case | Ignore case when comparing file names. | ||||||
--no-ignore-file-name-case | Consider case when comparing file names. | ||||||
-x, --exclude=PAT | Exclude files that match file name pattern PAT. | ||||||
-X, --exclude-from=FILE | Exclude files that match any file name pattern in file FILE. | ||||||
-S, --starting-file=FILE | Start with file FILE when comparing directories. | ||||||
--from-file=FILE1 | Compare FILE1 to all operands; FILE1 can be a directory. | ||||||
--to-file=FILE2 | Compare all operands to FILE2; FILE2 can be a directory. | ||||||
-i, --ignore-case | Ignore case differences in file contents. | ||||||
-E, --ignore-tab-expansion | Ignore changes due to tab expansion. | ||||||
-b, --ignore-space-change | Ignore changes in the amount of white space. | ||||||
-w, --ignore-all-space | Ignore all white space. | ||||||
-B, --ignore-blank-lines | Ignore changes whose lines are all blank. | ||||||
-I, --ignore-matching-lines=RE | Ignore changes whose lines all match regular expressionRE. | ||||||
-a, --text | Treat all files as text. | ||||||
--strip-trailing-cr | Strip trailing carriage return on input. | ||||||
-D, --ifdef=NAME | Output merged file with '#ifdef NAME' diffs. | ||||||
--GTYPE-group-format=GFMT | Format GTYPE input groups with GFMT. | ||||||
--line-format=LFMT | Format all input lines with LFMT. | ||||||
--LTYPE-line-format=LFMT | Format LTYPE input lines with LFMT. These format options provide fine-grained control over the output of diff, generalizing -D/--ifdef. LTYPE is old, new, or unchanged. GTYPE can be any of the LTYPE values, or the value changed. GFMT (but not LFMT) may contain:
| ||||||
%[-][WIDTH][.[PREC]]{doxX}LETTER | printf-style spec for LETTER |
LETTERs are as follows for new group, lower case for old group:
F | First line number. |
L | Last line number, |
N | Number of lines = L - F + 1. |
E | F - 1 |
M | L + 1 |
%(A=B?T:E) | If A equals B then T else E. |
The diff software does not actually change the files it compares. However, it can optionally generate a script (if the -e option is specified) for the program ed or ex which can be used to apply the changes.
For example, consider two files, file1.txt and file2.txt.
If file1.txt contains the following four lines of text: Vst plugins for adobe audition mac.
.and file2.txt contains these four lines:
.then we can use diff to automatically display for us which lines differ between the two files with this command:
.and the output will be:
Let's take a look at what this output means. The important thing to remember is that when diff is describing these differences to you, it's doing so in a prescriptive context: it's telling you how to change the first file to make it match the second file.
The first line of the diff output will contain:
- line numbers corresponding to the first file,
- a letter (a for add, c for change, or d for delete), and
- line numbers corresponding to the second file.
In our output above, '2,4c2,4' means: 'Lines 2 through 4 in the first file need to be changed to match lines 2 through 4 in the second file.' It then tells us what those lines are in each file:
- Lines preceded by a < are lines from the first file;
- lines preceded by > are lines from the second file.
The three dashes ('---') merely separate the lines of file 1 and file 2.
Let's look at another example. Let's say our two files look like this:
file1.txt:
file2.txt:
Output:
Diff Checker 2 1 4 234 X 256
Here, the output is telling us 'After line 2 in the first file, a line needs to be added: line 3 from the second file.' It then shows us what that line is.
Now let's see what it looks like when diff tells us we need to delete a line.
file1:
file2:
Our command:
The output:
Here, the output is telling us 'You need to delete line 4 in the first file so that both files sync up at line 3.' It then shows us the contents of the line that needs to be deleted.
Viewing diff Output In Context
The examples above show the default output of diff. It's intended to be read by a computer, not a human, so for human purposes, sometimes it helps to see the context of the changes.
GNUdiff, which is the version most linux users will be using, offers two different ways to do this: 'context mode' and 'unified mode'.
To view differences in context mode, use the -c option. For instance, let's say file1.txt and file2.txt contain the following:
file1.txt:
file2.txt:
Let's look at the contextual output for the diff of these two files. Our command is:
And our output looks like this:
The first two lines of this output show us information about our 'from' file (file 1) and our 'to' file (file 2). It lists the file name, modification date, and modification time of each of our files, one per line. The 'from' file is indicated by '***', and the 'to' file is indicated by '---'.
The line '***************' is just a separator.
The next line has three asterisks ('***') followed by a line range from the first file (in this case lines 1 through 4, separated by a comma). Then four asterisks ('****').
Then it shows us the contents of those lines. If the line is unchanged, it's prefixed by two spaces. If the line is changed, however, it's prefixed by an indicative character and a space. The character meanings are as follows:
character | meaning |
---|---|
! | Indicates that this line is part of a group of one or more lines that needs to change. There is a corresponding group of lines prefixed with '!' in the other file's context as well. |
+ | Indicates a line in the second file that needs to be added to the first file. |
- | Indicates a line in the first file that needs to be deleted. |
After the lines from the first file, there are three dashes ('---'), then a line range, then four dashes ('----'). This indicates the line range in the second file that will sync up with our changes in the first file.
If there is more than one section that needs to change, diff will show these sections one after the other. Lines from the first file will still be indicated with '***', and lines from the second file with '---'.
Unified Mode
Unified mode (the -u option) is similar to context mode, but it doesn't display any redundant information. Here's an example, using the same input files as our last example:
file1.txt:
file2.txt:
Our command:
The output:
The output is similar to above, but as you can see, the differences are 'unified' into one set.
Finding Differences In Directory Contents
diff can also compare directories by providing directory names instead of file names. See the Examples section.
Using diff To Create An Editing Script
The -e option tells diff to output a script, which can be used by the editing programs ed or ex, that contains a sequence of commands. The commands are a combination of c (change), a (add), and d (delete) which, when executed by the editor, will modify the contents of file1 (the first file specified on the diff command line) so that it matches the contents of file2 (the second file specified).
Let's say we have two files with the following contents:
file1.txt:
file2.txt
We can run the following command to analyze the two files with diff and produce a script to create a file identical to file2.txt from the contents of file1.txt:
diff -e file1.txt file2.txt
.and the output will look like this:
Notice that the changes are listed in reverse order: the changes closer to the end of the file are listed first, and changes closer to the beginning of the file are listed last. This order is to preserve line numbering; if we made the changes at the beginning of the file first, that might change the line numbers later in the file. So the script starts at the end, and works backwards.
Here, the script is telling the editing program: 'change line 5 to (the following line), and change lines 2 through 3 to (the following two lines).'
Next, we should save the script to a file. We can redirect the diff output to a file using the > operator, like this:
This command will not display anything on the screen (unless there is an error); instead, the output is redirected to the file my-ed-script.txt. If my-ed-script.txt doesn't exist, it will be created; if it exists already, it will be overwritten.
If we now check the contents of my-ed-script.txt with the cat Wolf casino games. command.
.we will see the same script we saw displayed above.
There's still one thing missing, though: we need the script to tell ed to actually write the file. All that's missing from the script is the w command, which will write the changes. We can add this to our script by echoing the letter 'w' and using the >> operator to add it to our file. (The >> operator is similar to the > operator. It redirects output to a file, but instead of overwriting the destination file, it appends to the end of the file.) The command looks like this:
Now, we can check to see that our script has changed by running the cat command again:
Now our script, when issued to ed, will make the changes and write the changes to disk.
So how do we get ed to do this?
We can issue this script to ed with the following command, telling it to overwrite our original file. The dash ('-') tells ed to read from the standard input, and the < operator directs our script to that input. In essence, the system enters whatever is in our script as input to the editing program. The command looks like this:
This command displays nothing, but if we look at the contents of our original file.
.we can see that file1.txt now matches file2.txt exactly.
Warning! In this example, ed overwrote the contents of our original file, file1.txt. After running the script, the original text of file1.txt disappears, so make sure you understand what you're doing before running these commands!
Commonly-Used diff Options
Here are some useful diff options to take note of:
-b | Ignore any changes which only change the amount of whitespace (such as spaces or tabs). |
-w | Ignore whitespace entirely. |
-B | Ignore blank lines when calculating differences. |
-y | Display output in two columns. |
These are only some of the most commonly-used diff options. What follows is a complete list of diff options and their function.
Syntax
Options
--normal | Output a 'normal' diff, which is the default. | ||||||
-q, --brief | Produce output only when files differ. If there are no differences, output nothing. | ||||||
-s, --report-identical-files | Report when two files are the same. | ||||||
-c, -CNUM, --context[=NUM] | Provide NUM (default 3) lines of context. | ||||||
-u, -UNUM, --unified[=NUM] | Provide NUM (default 3) lines of unified context. | ||||||
-e, --ed | Output an ed script. | ||||||
-n, --rcs | Output an RCS-format diff. | ||||||
-y, --side by side | Format output in two columns. | ||||||
-W, --width=NUM | Output at most NUM (default 130) print columns. | ||||||
--left-column | Output only the left column of common lines. | ||||||
--suppress-common-lines | Do not output lines common between the two files. | ||||||
-p, --show-c-function | For files that contain C code, also show each C function change. | ||||||
-F, --show-function-line=RE | Show the most recent line matching regular expression RE. | ||||||
--labelLABEL | When displaying output, use the label LABEL instead of the file name. This option can be issued more than once for multiple labels. | ||||||
-t, --expand-tabs | Expand tabs to spaces in output. | ||||||
-T, --initial-tab | Make tabs line up by prepending a tab if necessary. | ||||||
--tabsize=NUM | Define a tab stop as NUM (default 8) columns. | ||||||
--suppress-blank-empty | Suppress spaces or tabs before empty output lines. | ||||||
-l, --paginate | Pass output through pr to paginate. | ||||||
-r, --recursive | Recursively compare any subdirectories found. | ||||||
-N, --new-file | If a specified file does not exist, perform the diff as if it is an empty file. | ||||||
--unidirectional-new-file | Same as -n, but only applies to the first file. | ||||||
--ignore-file-name-case | Ignore case when comparing file names. | ||||||
--no-ignore-file-name-case | Consider case when comparing file names. | ||||||
-x, --exclude=PAT | Exclude files that match file name pattern PAT. | ||||||
-X, --exclude-from=FILE | Exclude files that match any file name pattern in file FILE. | ||||||
-S, --starting-file=FILE | Start with file FILE when comparing directories. | ||||||
--from-file=FILE1 | Compare FILE1 to all operands; FILE1 can be a directory. | ||||||
--to-file=FILE2 | Compare all operands to FILE2; FILE2 can be a directory. | ||||||
-i, --ignore-case | Ignore case differences in file contents. | ||||||
-E, --ignore-tab-expansion | Ignore changes due to tab expansion. | ||||||
-b, --ignore-space-change | Ignore changes in the amount of white space. | ||||||
-w, --ignore-all-space | Ignore all white space. | ||||||
-B, --ignore-blank-lines | Ignore changes whose lines are all blank. | ||||||
-I, --ignore-matching-lines=RE | Ignore changes whose lines all match regular expressionRE. | ||||||
-a, --text | Treat all files as text. | ||||||
--strip-trailing-cr | Strip trailing carriage return on input. | ||||||
-D, --ifdef=NAME | Output merged file with '#ifdef NAME' diffs. | ||||||
--GTYPE-group-format=GFMT | Format GTYPE input groups with GFMT. | ||||||
--line-format=LFMT | Format all input lines with LFMT. | ||||||
--LTYPE-line-format=LFMT | Format LTYPE input lines with LFMT. These format options provide fine-grained control over the output of diff, generalizing -D/--ifdef. LTYPE is old, new, or unchanged. GTYPE can be any of the LTYPE values, or the value changed. GFMT (but not LFMT) may contain:
| ||||||
%[-][WIDTH][.[PREC]]{doxX}LETTER | printf-style spec for LETTER |
LETTERs are as follows for new group, lower case for old group:
F | First line number. |
L | Last line number, |
N | Number of lines = L - F + 1. |
E | F - 1 |
M | L + 1 |
%(A=B?T:E) | If A equals B then T else E. |
LFMT (only) may contain:
%L | Contents of line. |
%l | Contents of line, excluding any trailing newline. |
%[-][WIDTH][.[PREC]]{doxX}n | printf-style spec for input line number. |
Both GFMT and LFMT may contain:
%% | A literal %. |
%c'C' | The single character C. |
%c'OOO' | The character with octal code OOO. |
C | The character C (other characters represent themselves). |
-d, --minimal | Try hard to find a smaller set of changes. |
--horizon-lines=NUM | Keep NUM lines of the common prefix and suffix. |
--speed-large-files | Assume large files and many scattered small changes. |
--help | Display a help message and exit. |
-v, --version | Output version information and exit. |
FILES takes the form 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE.' or 'FILE. DIR'.
If the --from-file or --to-file options are given, there are no restrictions on FILE(s). If a FILE is a dash ('-'), diff reads from standard input.
Exit status is either 0 if inputs are the same, 1 if different, or 2 if diff encounters any trouble.
Examples
Here's an example of using diff to examine the differences between two files side by side using the -y option, given the following input files:
file1.txt: Proencryptor 1 7 7 – encrypt your files online.
file2.txt: Scrutiny 5 2 1 – web optimization tool suite.
Output:
And as promised, here is an example of using diff to compare two directories:
Output:
Related commands
bdiff — Identify the differences between two very big files.
cmp — Compare two files byte by byte.
comm — Compare two sorted files line by line.
dircmp — Compare the contents of two directories, listing unique files.
ed — A simple text editor.
pr — Format a text file for printing.
ls — List the contents of a directory or directories.
sdiff — Compare two files, side-by-side.
Use the following calculators to perform the addition, subtraction, multiplication, or division of two binary values, as well as convert binary values to decimal values, and vice versa.
Binary Calculation—Add, Subtract, Multiply, or Divide
Convert Binary Value to Decimal Value
Convert Decimal Value to Binary Value
RelatedHex Calculator | IP Subnet Calculator
The binary system is a numerical system that functions virtually identically to the decimal number system that people are likely more familiar with. While the decimal number system uses the number 10 as its base, the binary system uses 2. Furthermore, although the decimal system uses the digits 0 through 9, the binary system uses only 0 and 1, and each digit is referred to as a bit. Apart from these differences, operations such as addition, subtraction, multiplication, and division are all computed following the same rules as the decimal system.
Almost all modern technology and computers use the binary system due to its ease of implementation in digital circuitry using logic gates. It is much simpler to design hardware that only needs to detect two states, on and off (or true/false, present/absent, etc.). Using a decimal system would require hardware that can detect 10 states for the digits 0 through 9, and is more complicated.
Below are some typical conversions between binary and decimal values:
Binary/Decimal Conversion
Decimal | Binary |
0 | 0 |
1 | 1 |
2 | 10 |
3 | 11 |
4 | 100 |
7 | 111 |
8 | 1000 |
10 | 1010 |
16 | 10000 |
20 | 10100 |
While working with binary may initially seem confusing, understanding that each binary place value represents 2n, just as each decimal place represents 10n, should help clarify. Take the number 8 for example. In the decimal number system, 8 is positioned in the first decimal place left of the decimal point, signifying the 100 place. Essentially this means:
8 × 100 = 8 × 1 = 8
Using the number 18 for comparison: Video editor enhancer 1 0 29 – video quality editing.
(1 × 101) + (8 × 100) = 10 + 8 = 18
In binary, 8 is represented as 1000. Reading from right to left, the first 0 represents 20, the second 21, the third 22, and the fourth 23; just like the decimal system, except with a base of 2 rather than 10. Since 23 = 8, a 1 is entered in its position yielding 1000. Using 18, or 10010 as an example:
18 = 16 + 2 = 24 + 21
10010 = (1 × 24) + (0 × 23) + (0 × 22) + (1 × 21) + (0 × 20) = 18
The step by step process to convert from the decimal to the binary system is:
- Find the largest power of 2 that lies within the given number
- Subtract that value from the given number
- Find the largest power of 2 within the remainder found in step 2
- Repeat until there is no remainder
- Enter a 1 for each binary place value that was found, and a 0 for the rest
Using the target of 18 again as an example, below is another way to visualize this:
2n | 24 | 23 | 22 | 21 | 20 |
Instances within 18 | 1 | 0 | 0 | 1 | 0 |
Target: 18 | 18 - 16 = 2 | → | 2 - 2 = 0 |
Converting from the binary to the decimal system is simpler. Determine all of the place values where 1 occurs, and find the sum of the values.
EX: 10111 = (1 × 24) + (0 × 23) + (1 × 22) + (1 × 21) + (1 × 20) = 23
24 | 23 | 22 | 21 | 20 |
1 | 0 | 1 | 1 | 1 |
16 | 0 | 4 | 2 | 1 |
Diff Checker 2 1 4 234 X 2
Hence: 16 + 4 + 2 + 1 = 23.
Diff Checker 2 1 4 234 X 24
Binary Addition
Binary addition follows the same rules as addition in the decimal system except that rather than carrying a 1 over when the values added equal 10, carry over occurs when the result of addition equals 2. Refer to the example below for clarification.
Note that in the binary system:
- 0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0, carry over the 1, i.e. 10
EX:
10 | 11 | 11 | 10 | 1 | ||
+ | 1 | 0 | 1 | 1 | 1 | |
= | 1 | 0 | 0 | 1 | 0 | 0 |
The only real difference between binary and decimal addition is that the value 2 in the binary system is the equivalent of 10 in the decimal system. Note that the superscripted 1's represent digits that are carried over. A common mistake to watch out for when conducting binary addition is in the case where 1 + 1 = 0 also has a 1 carried over from the previous column to its right. The value at the bottom should then be 1 from the carried over 1 rather than 0. This can be observed in the third column from the right in the above example.
Binary Subtraction
Similarly to binary addition, there is little difference between binary and decimal subtraction except those that arise from using only the digits 0 and 1. Borrowing occurs in any instance where the number that is subtracted is larger than the number it is being subtracted from. In binary subtraction, the only case where borrowing is necessary is when 1 is subtracted from 0. When this occurs, the 0 in the borrowing column essentially becomes '2' (changing the 0-1 into 2-1 = 1) while reducing the 1 in the column being borrowed from by 1. If the following column is also 0, borrowing will have to occur from each subsequent column until a column with a value of 1 can be reduced to 0. Refer to the example below for clarification.
Diff Checker 2 1 4 234 X 27
Note that in the binary system:
Diff Checker 2 1 4 234 X 22
- 0 - 0 = 0
0 - 1 = 1, borrow 1, resulting in -1 carried over
1 - 0 = 1
1 - 1 = 0
EX1:
-11 | 20 | 1 | 1 | 1 | |
– | 0 | 1 | 1 | 0 | 1 |
= | 0 | 1 | 0 | 1 | 0 |
EX2:
-11 | 2-10 | 0 | |
– | 0 | 1 | 1 |
= | 0 | 0 | 1 |
Note that the superscripts displayed are the changes that occur to each bit when borrowing. The borrowing column essentially obtains 2 from borrowing, and the column that is borrowed from is reduced by 1.
Binary Multiplication
Binary multiplication is arguably simpler than its decimal counterpart. Since the only values used are 0 and 1, the results that must be added are either the same as the first term, or 0. Note that in each subsequent row, placeholder 0's need to be added, and the value shifted to the left, just like in decimal multiplication. The complexity in binary multiplication arises from tedious binary addition dependent on how many bits are in each term. Refer to the example below for clarification.
Note that in the binary system:
- 0 × 0 = 0
0 × 1 = 0
1 × 0 = 0
1 × 1 = 1
EX:
1 | 0 | 1 | 1 | 1 | |||
× | 1 | 1 | |||||
1 | 0 | 1 | 1 | 1 | |||
+ | 1 | 0 | 1 | 1 | 1 | 0 | |
= | 1 | 0 | 0 | 0 | 1 | 0 | 1 |
As can be seen in the example above, the process of binary multiplication is the same as it is in decimal multiplication. Note that the 0 placeholder is written in the second line. Typically the 0 placeholder is not visually present in decimal multiplication. While the same can be done in this example (with the 0 placeholder being assumed rather than explicit), it is included in this example because the 0 is relevant for any binary addition / subtraction calculator, like the one provided on this page. Without the 0 being shown, it would be possible to make the mistake of excluding the 0 when adding the binary values displayed above. Note again that in the binary system, any 0 to the right of a 1 is relevant, while any 0 to the left of the last 1 in the value is not.
EX:
- 1 0 1 0 1 1 0 0
= 0 0 1 0 1 0 1 1 0 0
≠ 1 0 1 0 1 1 0 0 0 0
Binary Division
The process of binary division is similar to long division in the decimal system. The dividend is still divided by the divisor in the same manner, with the only significant difference being the use of binary rather than decimal subtraction. Note that a good understanding of binary subtraction is important for conducting binary division. Refer to the example below, as well as to the binary subtraction section for clarification.