8. Bitwise NOT, alternative


Yo, I couldn't find out how to do it, so I've searched a bit on this foru and I found out this (working) code:

def counting_bits(a,b):
    a = '{:b}'.format(a)
    b = '{:b}'.format(b)
    a = a.zfill(len(b))
    b = b.zfill(len(a))
    ans = ['0' if a[i] == '0' and b[i] == '0' else '1' for i in range(len(a))]
    ans = '0b' + ''.join(ans)
    ans = int(ans, 2)
    return ans

print bin(counting_bits(0b1110,0b101))

Can someone explain me some commends: .zfill and '{:b}'.format(a) (I think what it converts the number into binary, but Im not sure), And also if I've done already 80% of Python academy and I;m still not too good in it, should I keep going or try something other? Many people posting here their codes are using commends which I've never seen and solutions I would never think of...


Yes, it does convert numbers into binary. What's your error message?


I don't have any problem,s Im just asking to understand :slight_smile:

BTW. At least I was able to change this code to work as XOR :C


For exercise 8, it really just needed one line. But it's okay if you're not great at python yet lol. I finished it and I can't do a lot of things. This is an introductory, and since you've gotten this far, you really should finish. Codecademy is really just testing to see if you have the APTITUDE to really code in the future or for a career, etc. It's an awesome language, but what I honestly suggest, is starting Ruby and JavaScript because they are very similar to python, but once you do those, you get a really good understanding of Objective C, etc. The more you get into it, the better you get at all of them. :slight_smile:
Also, to answer your question about the format of some things, I would check out stackoverflow.com because they are advanced programmers and developers asking questions about literally anything and there are millions of posts of help. All you have to do is type in the language you are working with, and the thing you want to know about. I hope that can help you.


Boa noite,
Simplesmente tente o seguinte.

Para exibir o resultado de usar | em 0b1110 e 0b101 como uma string binária:


print bin(0b1110 | 0b101)


These.... THESE WORDS... are the wisest words, I've ever read


Yes, that is an excellent place to go for help and ideas.

Stack Overflow


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