_TOP_MENU

Jun 28, 2016

One's Complement

The ones' complement
The ones' complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa). The ones' complement of the number then behaves like the negative of the original number in some arithmetic operations.


8-bit ones'-complement integers
Bits Unsigned
value
Ones'
complement
value
0111 1111 127  127 
0111 1110 126  126 
0000 0010 2  2 
0000 0001 1  1 
0000 0000 0  0 
1111 1111 255  −0 
1111 1110 254  −1 
1000 0010 130  −125 
1000 0001 129  −126 
1000 0000 128 −127

One's complement having something called "Negative Zero".

In one's complement system, when you take one's complement of all zero, you will get all 1's, 1111 1111 ( one's complement of 0000 0000 ) ,  this is because of offset of -1 in one's complement.

A possible range for 1's compliment for n-bit number is  from −(2N−1−1) to 2N−1−1.
Example- for 8-bit number , 1's complement range is  -127 to +127

Practical application of one's compliment.

one's complement represent the negative of original number, so for subtraction, we can use one's complement. see below the examples.

Example 1 :  Add +25 with +15.

 +25
 +15
---------
   40
---------

Easy ... no need to take one's complement.

Example 2.   Subtract 15 from 25 .

  25
- 15
-------

Or
      25
+ (-15)
------------
------------

To calculate in Digital system ->
 +25 ->  0001  1001
  -15  ->  one's complement of 15  0000 1111
              which is  1111 0000

So we need to add
   0001 1001
   1111 0000
----------------------
1 0000 1001        [09] ,  we need to add carry if sign bit is zero.
   0000 0001
----------------------
  0000 1010    -> which is 10 (in decimal)

Example 3. Subtract 25 from 15.

    + 15
    -  25
-----------

Or   +    15
       +  (-25)
---------------

One's complement for -25  ->
   25 ->   0001 1001
 -25  ->   1110  0110

 + 15  ->    0000 1111
  -25   >     1110 0110
---------------------------
                  1111 0101
---------------------------

Since sign bit is 1 , we need to take complement of it.

After taking complement , it will be  0000 1010  =>  -10

Example 4. Subtract 25 from -15 

  -25
 - 15
--------

One's complement for 25  ->   1110 0110
One's complement for 15  ->   1111 0000

Now,
    1110  0110
    1111  0000
------------------
 1 1101 0110

Since sign bit is 1 , we need to take one's complement , it will be  0010 1001
Carry bit is 1, and sign bit was 1, we need to subtract 1 from the answer.

Final Answer will be   41 - 1 =>  - (40 )


Another Example -> 


  60   0011 1100
+ 36   0010 0100
----- -----------
  96   0110 0000

------------------------------------------------


  60     0011 1100
- 36     1101 1011
-------------------
  24   1 0001 0111
+                1
        -----------
         0001 1000 => 24    

------------------------------------------------

  36     0010 0100
- 60     1100 0011
-----  -------------
- 24   0 1110 0111
 sign bit -> 1
one's complement ->   0001 1000  -> 24

------------------------------------------------

- 60    one's complement -> 1100 0011
- 36    one's complement -> 1101 1011
-----                     --------------
- 96                       1 1001 1110
   sign bit is 1 and carry borrow is one
   one's complement and subtract 1 to get final answer
           
    one's complement ->    0110 0001
                          -        1
                         --------------  
                           0110 0000   => -96

If sign bit is 1 , and carry borrow is 1
    -->  take one's complement and subtract 1
If sign bit is 1 and carry borrow is 0
    -->  take one's complement
If sign bit is 0 and carry borrow is 0
    --> No need to do anything extra
If sign bit is 0 and carry borrow is 1
    --> subtract 1


Interview Questions on one's complement.
Q. How to design one's complement circuit ?
A. Simple enough, need to invert each bit.

Example for one's complement

Table of Contents

No comments:

Post a Comment