#======================================================================
# Author: Isai Damier
# Title: Subtraction
# Project: geekviewpoint
# Package: algorithms
#
# Statement:
# Given two sets of bits, x and y, find their difference.
#
# Sample Input: 0010010010
# Sample Output: 0010010011
#
# Technical Details: The subtraction of two sets is the subset
# comprising the elements that appear in the minuend x and not in the
# subtrahend y. For example if s1 = [a,b,f,h,k] and s2 = [b,c,d,f,h,i]
# then the subtraction of s1 and s2 is [a,k]. For two sets of bits the
# subtraction is a two steps process. First, the negative of the
# second set is calculated. Then the bitwise AND of the result and the
# first set is taken. For example if x = 0110101 and y=0101100 then
# the subtraction of x and y follows:
#
# step_1: z = negative y = ~y = 1010011.
# step_2: x AND z = x & z = x & (~y) = 0010001
#======================================================================
def subtraction( x, y ) :
return x & ~y

import unittest
from algorithms import bitwise as bits
class Test( unittest.TestCase ):
def testSubtraction( self ):
x = int( "0101100", 2 )
y = int( "0110101", 2 )
u = int( "0001000", 2 )
self.assertEquals( u, bits.subtraction( x, y ) )
x = int( "0101100", 2 )
y = int( "0110101", 2 )
u = int( "0010001", 2 )
self.assertEquals( u, bits.subtraction( y, x ) )