# Lesson 7, 8, and 9 Question

#1

Hello. I was doing the problems for lesson 7, 8, and 9 (all very similar) and they said try not to use &, |, and ^. I ended up using the operations and I passed the lesson. Could anyone give me advice on any other ways to do it without using the operations?

Thanks.

#2

they said try not to use &, |, and ^.

This section is basically designed to teach us about Bitwise operators, did they mention to not use them ?(in exercise 7,8,9?)

#3

``print bin(0b1110 ^ 0b101)    # 0b1011  (11, base 10)``

This is a long drawn out approach that returns a string, so not very practical.

``````def xor(a, b):
x = ''
if b > a:
a, b = b, a
while a > 0:
m, n = a & 1, b & 1
y = 1 if (m + n) % 2 else 0
x = str(y) + x
a = a >> 1
b = b >> 1
return '0b' + x

print xor(0b1110, 0b101)    # 0b1011``````

I don't remember the proof for this definition, but recall the basic expression...

``````a, b = (0b1110, 0b101)

print bin(~a & b | a & ~b)    # 0b1011``````

where `~` is bitwise NOT, `&` is bitwise AND, and `|` is bitwise OR.

``````~0b1110 => -0b1111

~0b101  => -0b110``````

Bitwise NOT is covered in lesson 10.

#4

ok, thank you for telling me about it.

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.