2.1 KiB
obj | rev |
---|---|
concept | 2024-01-17 |
Binary System
The base-2 numeral system is a positional notation with a radix of 2. Each digit is referred to as a bit, or binary digit. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used by almost all modern computers and computer-based devices, as a preferred system of use, over various other human techniques of communication, because of the simplicity of the language and the noise immunity in physical implementation.
Negative numbers are commonly represented in binary using two's complement.
Two's complement
Two's complement of an integer number is achieved by:
- Step 1: Start with the absolute value of the number.
- Step 2: inverting (or flipping) all bits – changing every 0 to 1, and every 1 to 0;
- Step 3: adding 1 to the entire inverted number, ignoring any overflow. Accounting for overflow will produce the wrong value for the result.
For example, to calculate the decimal number −6 in binary:
- Step 1: +6 in decimal is 0110 in binary; the leftmost significant bit (the first 0) is the sign (just 110 in binary would be -2 in decimal).
- Step 2: flip all bits in 0110, giving 1001.
- Step 3: add the place value 1 to the flipped number 1001, giving 1010.
To verify that 1010 indeed has a value of −6, add the place values together, but subtract the sign value from the final calculation. Because the most significant value is the sign value, it must be subtracted to produce the correct result: 1010 = −(1×23) + (0×22) + (1×21) + (0×20) = 1×−8 + 0 + 1×2 + 0 = −6.
Bits: | 1 | 0 | 1 | 0 |
---|---|---|---|---|
Decimal bit value: | **−**8 | 4 | 2 | 1 |
Binary calculation: | −(1×23) | (0×22) | (1×21) | (0×20) |
Decimal calculation: | −(1×8) | 0 | 1×2 | 0 |