The binary numeral system (binary system), or base-2 number system, represents numeric values using two symbols, usually 0 and 1.


To understand how binary numbers are constructed, it is useful to consider the decimal numeral system, or base-10 number system, which is the number system most commonly used. Digits in the decimal system are organised into columns multiplying them by powers of ten. For example, the columns for 123 are:

102 101 100
1 2 3

The number is calculated as follows:

1 * 102 = 1 * ( 1 * 10 * 10 ) = 1 * 100 = 100
2 * 101 = 2 * ( 1 * 10 ) = 2 * 10 = 20
3 * 100 = 3 * ( 1 ) = 3 * 1 = 3
123

Decimal number 123 is ( 1 * 102 ) + ( 2 * 101 ) + ( 3 * 100 ).

If more detail is required, refer to the explanation of term decimal.

The binary system works under the same principles as the decimal system, except that it operates in base-2 rather than base-10. Therefore, the columns for binary number 01111011 are:

27 26 25 24 23 22 21 20
0 1 1 1 1 0 1 1

The number is calculated as follows:

0 * 27 = 0 * ( 1 * 2 * 2 * 2 * 2 * 2 * 2 * 2 ) = 0 * 128 = 0
1 * 26 = 1 * ( 1 * 2 * 2 * 2 * 2 * 2 * 2 ) = 1 * 64 = 64
1 * 25 = 1 * ( 1 * 2 * 2 * 2 * 2 * 2 ) = 1 * 32 = 32
1 * 24 = 1 * ( 1 * 2 * 2 * 2 * 2 ) = 1 * 16 = 16
1 * 23 = 1 * ( 1 * 2 * 2 * 2 ) = 1 * 8 = 8
0 * 22 = 0 * ( 1 * 2 * 2 ) = 0 * 4 = 0
1 * 21 = 1 * ( 1 * 2 ) = 1 * 2 = 2
1 * 20 = 1 * ( 1 ) = 1 * 1 = 1
123

Binary number 01111011 is ( 0 * 27 ) + ( 1 * 26 ) + ( 1 * 25 ) + ( 1 * 24 ) + ( 1 * 23 ) + ( 0 * 22 ) + ( 1 * 21 ) + ( 1 * 20 ) = 123.

Instead of using digits 0 to 9 as the decimal system does, the binary system uses digits 0 to 1 to represent numbers. To put a larger number than 1 in column 2n it is neccesary to multiply 2 * 2n, which gives 2n+1 and carry a column to the left. For example, putting 11 in the 20 column is impossible, so a 1 is put in the 21 column and a 1 in the 20 column, thus using two columns. Three is 3 * 20 = 20( 2 + 1 ) = ( 1 * 21 ) + ( 1 * 20 ).

Negative Values

Negative integers are represented in computers by binary values whose most significant digit (the leftmost digit) is set (equal to 1). For example, using four binary digits, or bits, decimal 5 can be represented as 0101 and decimal -5 can be represented as 1011. 1011 is considered to be negative because its most significant bit is set. Adding any negative integer to its positive equivalent evaluates to zero. In decimal 5 + -5 = 0, so binary 0101 + 1011 should also equal zero. Adding 0101 to 1011 gives 10000 and since only four digits are being used, the first digit is discarded, leaving 0000.

Negative values are calculated using the two's complement system.

one's complement

The one's complement of a binary number is the bitwise complement of the number, or the number with each bit complemented (set to 1 if it is 0 or to 0 if it is 1).

The one's complement of binary 0101 (decimal 5) is therefore 1010.

Adding any integer to its one's complement evaluates to an integer consisting entirely of set bits (1111), which is the one's complement of zero and from a one's complement perspective may be used to represent negative zero.

two's complement

The two's complement of a binary number is the one's complement plus one, and represents the negative value of the number.

The two's complement of binary 0101 (decimal 5 ) is therefore 1010 + 0001 = 1011.

Adding any integer to its two's complement evaluates to zero (0000).


examples GCC C++
Borland C++ Compiler
Java
home Home Page