Binary Algebra: (PHC504)

The 9's complement of a decimal number

The 9's complement of a decimal number is a mathematical operation that involves replacing each digit with its complement to 9. This operation is commonly used in computer science and digital arithmetic. The 9's complement operation is often used in digital arithmetic for subtraction operations using addition with the complemented number.

To find the 9's complement of a decimal number, following are the steps:

  1. Subtract each digit from 9.
  2. Write down the results of the subtractions as the digits of the 9's complement.

Few examples of finding the 9's complement of decimal numbers are given below:

Example 1: Number = 12345

Step 1: Subtract each digit from 9

9 - 1 = 8 
9 - 2 = 7 
9 - 3 = 6 
9 - 4 = 5 
9 - 5 = 4

Step 2: Write down the results:

9's Complement: 87654

Example 2: Number = 9876

Step 1: Subtract each digit from 9

9 - 9 = 0
9 - 8 = 1
9 - 7 = 2
9 - 6 = 3

Step 2: Write down the results: :

9's Complement: 123

Example 3: Number = 555

Step 1: 9 - 5 = 4, 9 - 5 = 4, 9 - 5 = 4
9's Complement: 444

Example 4: Number = 1000

Step 1: 9 - 1 = 8, 9 - 0 = 9, 9 - 0 = 9, 9 - 0 = 9
9's Complement: 8999

Example 5: Number = 420

Step 1: 9 - 4 = 5, 9 - 2 = 7, 9 - 0 = 9
9's Complement: 579

Example 6: Number = 9999

Step 1: 9 - 9 = 0, 9 - 9 = 0, 9 - 9 = 0, 9 - 9 = 0
9's Complement: 0

Example 7: Number = 735

Step 1: 9 - 7 = 2, 9 - 3 = 6, 9 - 5 = 4
9's Complement: 264

Example 8: Number = 1234

Step 1: 9 - 1 = 8, 9 - 2 = 7, 9 - 3 = 6, 9 - 4 = 5
9's Complement: 8765

Example 9: Number = 909

Step 1: 9 - 9 = 0, 9 - 0 = 9, 9 - 9 = 0
9's Complement: 90

Example 10: Number = 0

Step 1: 9 - 0 = 9
9's Complement: 9

The 10's complement

The 10's complement is a technique used in digital arithmetic to represent negative numbers. It simplifies subtraction by transforming it into addition and allows both positive and negative numbers to be represented uniformly. It involves finding the complement of a given decimal number with respect to 10. To find the 10's complement of a number, you subtract each digit from 9. Following steps are used to find the 10's complement of a decimal number:

  1. Subtract each digit of the number from 9.
  2. Add 1 to the result.

Few examples of finding the 10's complement along with the method of conversion:

Example 1: Number: 325 10's Complement: 675

Method: Subtract each digit from 9: 
For the first digit,  9 - 3 = 6.
For the second digit, 9 - 2 = 7.
For the third digit,  9 - 5 = 4.

Example 2: Number: 780 10's Complement: 220

Method: Subtract each digit from 9: 
For the first digit,  9 - 7 = 2.
For the second digit, 9 - 8 = 1.
For the third digit,  9 - 0 = 9.

Example 3: Number: 4562 10's Complement: 5438

Method: Subtract each digit from 9: 
For the first digit,  9 - 4 = 5.
For the second digit, 9 - 5 = 4.
For the third digit,  9 - 6 = 3.
For the fourth digit, 9 - 2 = 7.

Repeat the same process for the remaining examples.

Example 4: Number: 912 10's Complement: 88

Example 5: Number: 1000 10's Complement: 9000

Example 6: Number: 7 10's Complement: 3

Example 7: Number: 84379 10's Complement: 15621

Example 8: Number: 12345 10's Complement: 87655

Example 9: Number: 555 10's Complement: 445

Example 10: Number: 99999 10's Complement: 1

One's complement

One's complement is a binary number representation where the negative of a number is represented by inverting all the bits (changing 0s to 1s and 1s to 0s) of its binary representation. The one's complement representation is mainly used for binary arithmetic operations on computers. Negative numbers are represented by taking the one's complement of their positive binary counterpart. Few examples of converting decimal numbers to their one's complement representation:

Method of Conversion:**

  1. Convert the decimal number to its binary representation.
  2. Invert all the bits (change 0s to 1s and 1s to 0s) to get the one's complement representation.

Conversion examples:**

Example 1:

Decimal          : 7
Binary           : 0111
One's Complement : 1000

Example 2:

Decimal          : -5
Binary           : 1011 (2's complement representation of 5)
One's Complement : 0100

Example 3:

Decimal          : 0
Binary           : 0000
One's Complement : 1111

Example 4:

Decimal          : -15
Binary           : 10001 (2's complement representation of 15)
One's Complement : 01110

Example 5:

Decimal          : 10
Binary           : 1010
One's Complement : 0101

Example 6:

Decimal          : -3
Binary           : 1101 (2's complement representation of 3)
One's Complement : 0010

Example 7:

Decimal          : 127
Binary           : 1111111
One's Complement : 0000000

Example 8:

Decimal          : -64
Binary           : 1000000 (2's complement representation of 64)
One's Complement : 0111111

Example 9:

Decimal          : -9
Binary           : 10111 (2's complement representation of 9)
One's Complement : 01000

Example 10:

Decimal          : 255
Binary           : 11111111
One's Complement : 00000000

Two's complement

Two's complement is a method for representing signed integers in binary form. It involves taking the binary representation of a positive integer, inverting its bits (changing 0s to 1s and 1s to 0s), and then adding 1 to the result. the leftmost bit (the most significant bit) is used to indicate the sign of the number when dealing with two's complement. 0 represents a positive number, and 1 represents a negative number. The range of representable values depends on the number of bits used for the representation. Few examples of converting decimal numbers to their two's complement binary representation are given below:

Example 1: Converting -5 to Two's Complement

  1. Represent 5 in binary : 00000101
  2. Invert the bits : 11111010
  3. Add 1 : 11111011 (two's complement of -5)

Example 2: Converting -12 to Two's Complement

  1. Represent 12 in binary : 00001100
  2. Invert the bits : 11110011
  3. Add 1 : 11110100 (two's complement of -12)

Example 3: Converting 7 to Two's Complement (positive number)

  1. Represent 7 in binary : 00000111
  2. Pad with leading zeros to maintain the same bit length : 00000111
  3. This is already the two's complement of 7 (positive numbers remain the same in two's complement).

Example 4: Converting -128 to Two's Complement

  1. Represent 128 in binary : 10000000
  2. Invert the bits : 01111111
  3. Add 1 : 10000000 (overflow occurs, wrapping around)

Example 5: Converting -1 to Two's Complement

  1. Represent 1 in binary : 00000001
  2. Invert the bits : 11111110
  3. Add 1 : 11111111 (two's complement of -1)

Example 6: Converting 15 to Two's Complement (positive number)

  1. Represent 15 in binary : 00001111
  2. Pad with leading zeros to maintain the same bit length : 00001111
  3. This is already the two's complement of 15 (positive numbers remain the same in two's complement).

Example 7: Converting -31 to Two's Complement

  1. Represent 31 in binary : 00011111
  2. Invert the bits : 11100000
  3. Add 1 : 11100001 (two's complement of -31)

Example 8: Converting -64 to Two's Complement

  1. Represent 64 in binary : 01000000
  2. Invert the bits : 10111111
  3. Add 1 : 11000000 (two's complement of -64)

Example 9: Converting 63 to Two's Complement (positive number)

  1. Represent 63 in binary : 00111111
  2. Pad with leading zeros to maintain the same bit length : 00111111
  3. This is already the two's complement of 63 (positive numbers remain the same in two's complement).

Example 10: Converting -255 to Two's Complement

  1. Represent 255 in binary : 11111111
  2. Invert the bits : 00000000
  3. Add 1 : 00000001 (overflow occurs, wrapping around)

Binary Addition

Binary addition is a fundamental arithmetic operation in the realm of digital systems and computer science. It plays a crucial role in various computing processes, from basic calculations to complex data manipulation. Binary addition is a foundational operation in computer systems and plays a crucial role in various applications:

  1. Arithmetic Operations: Binary addition forms the basis for other arithmetic operations, such as subtraction, multiplication, and division.
  2. Data Manipulation: Computers use binary addition to manipulate data stored in memory, enabling calculations and transformations.
  3. Logic and Circuits: In digital logic design, binary addition is essential for building circuits that perform tasks like counting and data processing.
  4. Error Detection and Correction: Binary addition is used in error detection and correction codes, enhancing data reliability in communication systems.
  5. Binary Coded Decimal (BCD): BCD is a binary-encoded representation of decimal numbers, and binary addition is used to perform arithmetic on BCD numbers.

Binary Addition Rules

Binary addition follows similar principles to decimal addition. Just as you carry over digits in decimal addition when the sum exceeds 9, the carry over used when the sum of digits in a column is 2 or greater. The basic rules are as follows:

0 + 0 = 0: Adding two binary digits of 0 results in 0.
0 + 1 = 1: Adding 0 and 1 gives 1.
1 + 0 = 1: Adding 1 and 0 also yields 1.
1 + 1 = 0, Carry 1: Adding two 1s results in 0, with a carry of 1 to the next digit.

These rules are essential building blocks for binary addition, just as their decimal counterparts are for decimal addition.

Binary Addition Process

An example of binary addition: 1101 + 101 is given below.

   1 1 0 1   (1101)
+    1 0 1   (101)
------------
  1 0 1 1 0

Method of Binary addition

  1. Start by adding the rightmost digits (least significant bits): 1 + 1 = 0 (with a carry of 1).
  2. Move to the next digits: 0 + 0 + 1 (carry) = 1.
  3. Then: 1 + 1 = 0 (with a carry of 1).
  4. Finally: 1 + 1 + 1 (carry) = 1, with a carry of 1.
  5. The result is 10110, which is the binary equivalent of 22 in decimal.

Carrying Over in Binary Addition

Carrying over in binary addition is analogous to carrying over in decimal addition. Consider the example: 111 + 111.

    1 1 1   (111)
+   1 1 1   (111)
------------
   1 1 1 0

Method of Binary addition

  1. Start by adding the rightmost digits: 1 + 1 = 0 (with a carry of 1).
  2. Move to the next digits: 1 + 1 + 1 (carry) = 1, with a carry of 1.
  3. Then: 1 + 1 + 1 (carry) = 1, with a carry of 1.
  4. The result is 1110, which is 14 in decimal.

Binary Addition with Varying Lengths

Binary addition becomes more complex when dealing with numbers of different lengths. In this case, align the numbers to the right and fill empty spaces with leading zeros.

Consider: 1101 + 11.

   1 1 0 1   (1101)
+      1 1   (11)
------------
   1 1 1 0


Method of Binary addition

  1. Start with the rightmost digits: 1 + 1 = 0 (with a carry of 1).
  2. Move to the next digit: 0 + 1 = 1.
  3. Then: 1 + 1 = 0 (with a carry of 1).
  4. Finally: 1 + 0 (carry) = 1, with a carry of 1.
  5. The result is 1110, which is 14 in decimal, as expected.

Practice examples

Few more practice examples of adding binary numbers, along with the step-by-step method of conversion:

Example 1:

  1101
+ 1010
-------
  10111

Method:

  1. Start adding from the rightmost digits (least significant bit) and move left.
  2. Add the corresponding digits: 1 + 0 = 1, 1 + 1 = 0 with a carry of 1, 0 + 0 = 0, 1 + 1 = 0 with a carry of 1.
  3. Continue this process, including the carry, until you've added all the digits.

Example 2:

   110
+  101
-------
  1011

Method:

  1. Add the digits: 0 + 1 = 1, 1 + 0 = 1, 1 + 1 = 0 with a carry of 1.
  2. Add the carry to the leftmost digit.

Example 3:

  1111
+ 1001
-------
  11000

Method:

  1. Add the digits: 1 + 1 = 0 with a carry of 1, 1 + 0 = 1, 1 + 0 = 1, 1 + 1 = 0 with a carry of 1.
  2. Add the carry to the leftmost digit.

Example 4:

  1010
+  111
-------
   10001

Method:

  1. Add the digits: 0 + 1 = 1, 1 + 1 = 0 with a carry of 1, 0 + 1 = 1, 1 + 0 = 1.
  2. Add the carry to the leftmost digit.

Example 5:

  1101
+   11
-------
   1110

Method:

  1. Add the digits: 1 + 1 = 0 with a carry of 1, 0 + 1 = 1, 1 + 1 = 0 with a carry of 1.
  2. Add the carry to the leftmost digit.

Example 6:

   101
+  110
-------
   1011

Method:

  1. Add the digits: 1 + 0 = 1, 0 + 1 = 1, 1 + 1 = 0 with a carry of 1.
  2. Add the carry to the leftmost digit.

Example 7:

   111
+  101
-------
   1100

Method:

  1. Add the digits: 1 + 1 = 0 with a carry of 1, 1 + 0 = 1, 1 + 1 = 0 with a carry of 1.
  2. Add the carry to the leftmost digit.

Example 8:

  10111
+    10
-------
  11001

Method:

  1. Add the digits: 1 + 0 = 1, 1 + 0 = 1, 0 + 1 = 1, 1 + 0 = 1, 1 + 1 = 0 with a carry of 1.
  2. Add the carry to the leftmost digit.

Example 9:

   1110
+   101
-------
   10011

Method:

  1. Add the digits: 0 + 1 = 1, 1 + 0 = 1, 1 + 1 = 0 with a carry of 1, 1 + 1 = 0 with a carry of 1.
  2. Add the carry to the leftmost digit.

Example 10:

   110
+  110
-------
  1100

Method:

  1. Add the digits: 0 + 0 = 0, 1 + 1 = 0 with a carry of 1, 1 + 1 = 0 with a carry of 1.
  2. Add the carry to the leftmost digit.

Binary Subtraction: The one's complement method

The one's complement method is used to subtract binary numbers. In this method, you negate the second number (the subtrahend) by flipping all of its bits (changing 0s to 1s and 1s to 0s) and then add it to the first number (the minuend). The carry-over is required if any overflow to the next higher bit after performing the addition. If there's a carry-out after adding, you need to add it back to the result. Also, in the final result, ignore any carry-out beyond the leftmost bit. A few examples of binary subtraction using the one's complement method are given below:

Example 1:

Minuend:  10101
Subtrahend:  1101
Find the one's complement of the subtrahend:  0010
Add the one's complement to the minuend:  10101 + 0010 = 10101 + 2 = 10111

Example 2:

Minuend:  11010
Subtrahend:  1001
Find the one's complement of the subtrahend:  0110
Add the one's complement to the minuend:  11010 + 0110 = 11010 + 6 = 11100

Example 3:

Minuend:  11100
Subtrahend:  1011
Find the one's complement of the subtrahend:  0100
Add the one's complement to the minuend:  11100 + 0100 = 11100 + 4 = 11104

Example 4:

Minuend:  101110
Subtrahend:  11001
Find the one's complement of the subtrahend:  00110
Add the one's complement to the minuend:  101110 + 00110 = 101110 + 6 = 110000

Example 5:

Minuend:  111111
Subtrahend:  10101
Find the one's complement of the subtrahend:  01010
Add the one's complement to the minuend:  111111 + 01010 = 111111 + 10 = 1000001

Example 6:

Minuend:  110011
Subtrahend:  11100
Find the one's complement of the subtrahend:  00011
Add the one's complement to the minuend:  110011 + 00011 = 110011 + 3 = 110110

Example 7:

Minuend:  1001001
Subtrahend:  101101
Find the one's complement of the subtrahend:  010010
Add the one's complement to the minuend:  1001001 + 010010 = 1001001 + 18 = 10111011

Example 8:

Minuend:  1100100
Subtrahend:  101011
Find the one's complement of the subtrahend:  010100
Add the one's complement to the minuend:  1100100 + 010100 = 1100100 + 20 = 1101000

Example 9:

Minuend:  1110001
Subtrahend:  110011
Find the one's complement of the subtrahend:  001100
Add the one's complement to the minuend:  1110001 + 001100 = 1110001 + 12 = 1111010

Example 10:

Minuend:  1001010
Subtrahend:  10110
Find the one's complement of the subtrahend:  01001
Add the one's complement to the minuend:  1001010 + 01001 = 1001010 + 9 = 1001101

Practice examples

More examples of subtracting binary numbers using the one's complement method, along with the step-by-step conversion process:

  1. Example 1: 101 - 11

    Convert the second number to its one's complement: 1100
    Add:

     101
   +  00
   ------
     101
  1. Example 2: 1101 - 101

    Convert the second number to its one's complement: 101010
    Add:

    1101
   + 010
   ------
    1000
  1. Example 3: 11100 - 1011

    Convert the second number to its one's complement: 10110100
    Add:

     11100
   +  0100
   -------
    100100
  1. Example 4: 10010 - 1111

    Convert the second number to its one's complement: 11110000
    Add:

     10010
   +  0000
   -------
     10010
  1. Example 5: 11111 - 10101

    Convert the second number to its one's complement: 1010101010
    Add:

      11111
   +  01010
   --------
     101001
  1. Example 6: 101010 - 1101

    Convert the second number to its one's complement: 11010010
    Add:

     101010
   +   0010
   --------
     101100
  1. Example 7: 11001 - 10011

    Convert the second number to its one's complement: 1001101100
    Add:

      11001
   +  01100
   --------
     100101
  1. Example 8: 101001 - 100110

    Convert the second number to its one's complement: 100110011001
    Add:

     101001
   + 011001
   --------
     100100
  1. Example 9: 111111 - 101010

    Convert the second number to its one's complement: 101010010101
    Add:

     111111
   + 010101
   --------
    1010110
  1. Example 10: 1001010 - 10110

    Convert the second number to its one's complement: 1011001001
    Add:

     1001010
   +   01001
   ---------
     1011111

Binary Subtraction: two's complement method

Example 1: Subtract 10101 from 11001.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   11001 (minuend)
   +01011 (two's complement of subtrahend)
   ------
   00100 (result in binary)
  1. The result is 00100 in binary, which is 4 in decimal.

Example 2: Subtract 1110 from 10011.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   10011 (minuend)
   + 0010 (two's complement of subtrahend)
   ------
   11001 (result in binary)
  1. The result is 11001 in binary, which is 25 in decimal.

Example 3: Subtract 101 from 1100.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   1100 (minuend)
   + 011 (two's complement of subtrahend)
   ------
   1011 (result in binary)
  1. The result is 1011 in binary, which is 11 in decimal.

Example 4: Subtract 110 from 1001.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   1001 (minuend)
   + 010 (two's complement of subtrahend)
   ------
   11011 (result in binary)
  1. The result is 11011 in binary, which is 27 in decimal.

Example 5: Subtract 11001 from 11111.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   11111 (minuend)
   + 00111 (two's complement of subtrahend)
   ------
   11000 (result in binary)
  1. The result is 11000 in binary, which is 24 in decimal.

Example 6: Subtract 1010 from 11100.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   11100 (minuend)
   + 0110 (two's complement of subtrahend)
   ------
   10010 (result in binary)
  1. The result is 10010 in binary, which is 18 in decimal.

Example 7: Subtract 1111 from 10101.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   10101 (minuend)
   + 0001 (two's complement of subtrahend)
   ------
   10100 (result in binary)
  1. The result is 10100 in binary, which is 20 in decimal.

Example 8: Subtract 11010 from 101010.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   101010 (minuend)
   + 00110 (two's complement of subtrahend)
   ------
   111100 (result in binary)
  1. The result is 111100 in binary, which is 60 in decimal.

Example 9: Subtract 1101 from 11110.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   11110 (minuend)
   + 0011 (two's complement of subtrahend)
   ------
   10111 (result in binary)
  1. The result is 10111 in binary, which is 23 in decimal.

Example 10: Subtract 10100 from 111000.

  1. Convert the subtrahend to its two's complement:
  1. Add the two's complement of the subtrahend to the minuend:
   111000 (minuend)
   + 01100 (two's complement of subtrahend)
   ------
   011100 (result in binary)
  1. The result is 011100 in binary, which is 28 in decimal.

These examples demonstrate the process of subtracting binary numbers using the two's complement method.