Python Fraud and Binary Numbers
I've been using Python for years, so I always felt comfortable in saying that I'm a Python programmer. But, recently I was put in a position in which I had to actually talk about it... and I just couldn't do it. To me, this was a failure. It's made me reconsider my standing with all these technologies that I claim to be knowledgable with. While I felt like a fraud at the time, I know that the reality of the situation is much much different. I have plenty of experience with them, but my focus was always task focused, rather than knowledge focused. So, I'm thinking it's time to find some kind of balance between the two.
It didn't take much time or thought for me to realize that I've only ever really made one thing (for my own use) with Python — an API — and I never ended up using it. I've spent plenty of time creating Python programs to automate and complete job tasks, but I think the exploratory nature of building something on your own would be a good thing for me.
It's a great time for me to realize all this, because I'm already in the middle of rethinking whether I am where I want to be career-wise. So I made the decision to go for the entry-level Python certification from the Python Institute. Sure, it may be the easiest one they offer, but at least it will quickly give me an idea of where I stand.
I thought that I'd breeze right through the study course, so I skipped all the reading and went right to the quizzes and tests. I failed. But, I swallowed my pride, went back to the beginning, and started really going through the material. After a couple weeks, between work and life distractions, I finished the beginner course and, you know, I came away from it feeling more confident than I ever felt. Even with something so basic, it was good to know that I had the capacity to build a knowledge base.
I came across a few things in the course that I never took the time to understand, or just never committed to memory (I blame the internet for the later). The most notable of them being:
- Basic Math (there's a lot of it in the course)
- Binary and Bitwise Operations
I can't imagine I'll ever really need to deal with binary numbers, let alone shifting bits, but it's still exciting anyway. I imagine that it'll be beneficial as I spend more time with Golang rather than with Python.
So, I wanted to post about bitwise operators (because it's always been a topic that I avoided). But, I can't do that without first figuring out binary numbers. You might know about converting base 10 and base 2 numbers, but I didn't. Plus, I'm hoping that by writing this out will, I'll be reinforcing the material (Really, why else would I be doing this).
I know you could just google something like “Binary converter”, which is what I had always done, but, today, I won't be doing that.
Converting an Integer to a Binary Number
A binary number is a number expressed in the base 2 numeral system. The base 2 system uses only two digits to represent any number: 0s and 1s. Every number in our base 10 system (0-9... get it?) can be represented in binary, or base 2, using only 0s and 1s.
To convert any base 10 number to binary, you divide it by 2 until you reach 0. The remainder from each step is your binary number (from right to left) It makes more sense when you see it happening.
Let's say you want to convert the number 12 to binary. You start with 12 and divide by 2, which gives you 6 with a remainder of 0.
12 ÷ 2 = 6 R 0
That remainder of 0 will be the right most digit in the binary representation of 12. Now we know that 12 in binary will be
? ? ? 0 (I'm showing you that it will be 4 digits, but normally you might now know).
Next, take that 6 from the previous step and divide it by 2 again.
6 ÷ 2 = 3 R 0
You get 3 with a remainder of 0. That 0 will be the next digit in our binary representation of 12,
? ? 0 0. It goes to the left of the previous digit.
Now we take that 3 from the previous step and, again, divide by 2.
3 ÷ 2 = 1 R 1
You'll get 1 with a remainder of 1. Our next binary digit will be 1,
? 1 0 0.
Finally, you can take the 1 from the previous step and divide by 2.
1 ÷ 2 = 0 R 1
We've got a quotient of 0, which means this is last step. Take the remainder and we've got our binary, base 2, representation of 12:
** Note that python will show it like this:
print(bin(12)) # 0b1100
In summary, this is what we've done
12 ÷ 2 = 6 R 0 6 ÷ 2 = 3 R 0 3 ÷ 2 = 1 R 1 1 ÷ 2 = 0 R 1
Easy, right? Let's try it with 19.
19 ÷ 2 = 9 R 1 9 ÷ 2 = 4 R 1 4 ÷ 2 = 2 R 0 2 ÷ 2 = 1 R 0 1 ÷ 2 = 0 R 1
It's a breeze.
Converting a Binary Number to a Base 10 Integer
Converting back to a base 10 number is more confusing. But it's still just basic math, which you'll realize once you see it.
1100 back into base 10, that way you'll know if you're doing it correctly (we know its going to be 12 from above).
The method we're going to use is called Positional Notation, though I'm not sure if that's official method name.
1100 has four positions that need to be noted. Start counting from the right with the first position being 0.
$1100$ Binary $3210$ Positional Notation
The final step is to take 2 to the power of each position, multiplied by the binary number in that position.
$1100$ Binary $3210$ Positional Notation $1(2^3)\;\;1(2^2)\;\;0(2^1)\;\;0(2^0)$
You're answer will be the sum of the above: $8+4+0+0=12$ Easy. But, just to make sure you have it figured out, try to convert 1001011 to a base 10 integer.
Start with positional notation: $1001011$ $6543210$
Lastly, solve for each position by taking 2 to the power of each position number, multiplied by the position's binary number. $1(2^6)\;0(2^5)\;0(2^4)\;1(2^3)\;0(2^2)\;1(2^1)\;1(2^0)$
Add them all together, and you'll get your answer: $64+0+0+8+0+2+1=75$
See something wrong? Email [email protected].