Intersection
by Isai Damier, Android Engineer @ Google

#======================================================================
# Author: Isai Damier
# Title: intersection
# Project: geekviewpoint
# Package: algorithms
#
# Statement:
#   Given two sets of bits, x and y, find their intersection.
#
# Sample Input: 0101100 and 0110101
# Sample Output: 0100100
#
# Technical Details:
#   The intersection of two sets is the subset comprising only the
#   elements that appear in both sets. For example if s1 = [a,b,f,h,k]
#   and s2 = [b,c,d,f,h,i] then the intersection of s1 and s2 is
#   [b,f,h]. For two sets of bits the intersection is simply the bitwise
#    AND of the two sets. For example if x = 0110101 and y=0101100 then
#   the intersection of x and y is x U y = 0100100.
#====================================================================== 
 def intersection( x, y ):
  return x & y
import unittest
from algorithms import bitwise as bits

class Test( unittest.TestCase ):

    def testIntersection( self ):
      x = int( "0101100", 2 )
      y = int( "0110101", 2 )
      u = int( "0100100", 2 )

      self.assertEquals( u, bits.intersection( x, y ) )

      x = int( "0101100", 2 )
      y = int( "0110101", 2 )
      u = int( "0100100", 2 )
      self.assertEquals( u, bits.intersection( y, x ) )