# 7. A BIT of This AND That

#1

I promise this is (probably) the last one of these. As always, any advice is appreciated.

``````#print bin(14 & 5)

X = "1110"
Y  = "101"

def bitwise_XAND(X, Y):
one = "1"
new_x = X
new_y = Y
list_x = []
list_y = []
together = []
different_length = abs(len(X) - len(Y))
final = ""

if len(X) > len(Y):
for i in X:
together.append("0")
new_y = ("0" * different_length) + Y
elif len(Y) > len(X):
for i in Y:
together.append("0")
new_x = ("0" * different_length) + X

for i in new_x:
list_x.append(i)
for i in new_y:
list_y.append(i)

for i, c in enumerate(new_x):
if c == list_y[i] and c != "0":
together[i] = "1"

for i, c in enumerate(new_y):
if c == list_x[i] and c != "0":
together[i] = "1"

for i, c in enumerate(together):
if c == "0":
together[i] = ""
if c == "1":
break

final = "0b" + final.join(together)
return final

print bitwise_XAND(X, Y)``````

#2

### The XAND simply would be always FALSE, so there is no need for XAND

#3

Now just looking at AND, we can take from your idea, and fashion something slightly different:

``````def pad_left(x, p):
p = (p - len(str(bin(x)))) * '0'
return p + strip_0b(x)

def strip_0b(x):
return str(bin(x)).replace('0b','')

a = 0b1110
b = 0b110

print (bin(a), bin(b), bin(a & b))

aa = len(str(bin(a)))
bb = len(str(bin(b)))

if aa > bb:
m = strip_0b(a)
elif bb > aa:
n = strip_0b(b)
else:
m = strip_0b(a)
n = strip_0b(b)

print m, n

result = []
for i in range(len(m)):
result.append(str(int(m[i]) * int(n[i])))
print '0b' + ''.join(result)``````

Output

``````('0b1110', '0b110', '0b110')
1110 0110
0b0110``````

#4

didn't realize until just now that i had named it XAND instead of just AND