# Introduction to Bitwise Operators 14/14 Confusing

#1

Im a bit confused with this lesson…

``````a = 0b101
mask = (0b1 << 9)  # One less than ten
``````

Let’s say that I want to turn on the 10th bit from the right of the integer `a` .

Instead of writing out the entire number, we slide a bit over using the `<<` operator.

We use 9 because we only need to slide the mask nine places over from the first bit to reach the tenth bit.

Question nr.1

What I discovered is that:

1.We dont actually need the parentheses around (0b1 << 9)
2.0b1 << 9 is the same as 0b1 << 10 - 1, we get the same binary number - 0b1000000101

So to get to the 10th bit it doesn’t matter which way we choose, because they both give you the same result…right?

Question nr.2

The Instructions:

Define a function called `flip_bit` that takes the inputs `(number, n)` .

Flip the nth bit (with the ones bit being the first bit) and store it in `result` .

Return the result of calling `bin(result)` .

What do they mean with (with the ones bit being the first bit) ?

Question nr.3

Lets look at the Codecademy solution and use the numbers from the example they gave us:

``````def flip_bit(number, n):
mask = 0b1 << (n - 1)
return bin(result)
print flip_bit(0b101, 9)
``````

Here we want to flip the 9th bit…n inside the mask variable turns to 9 so that we get 0b1 << (9-1), BUT if we wanted, AND knew, that that we wanted to flip the 9th bit, why not just call the the function with 8 and lose the - 1 part inside the mask variable, so that the mask variable would look like this mask = 0b1 << n , with other words mask = 0b1 << 8?

Is it because they want us to make sure that if somebody else uses the function, besides us, gets to flip the right bit(9th bit) when they enter 9?

Question nr.4

How do we flip a bit the other way around…with the right shift, how is the mask going to look?
Lest say we have the number 100000101, and I want to flip the third bit from right to left, so that the number changes to 101000101?

#2

The reason they are the same is because `-` takes precedence to `<<`, so 10 -1 becomes 9.

Not really. It is more for cases where `n` occurs dynamically in the program. Writing the offset in the part of the program that uses it is better than having to explain why we take 1 from something before passing it to the function.

We would need to know the degree on the highest order bit (the one to the far left). We can either use the len() function on the binary string (which is clunky) or we can use the math.log() function on the binary integer.

``````>>> from math import log
>>> a = '0b100000101'
>>> log(int(a, 2), 2)
8.027905996569885
>>> int(log(int(a, 2), 2)) + 1
9
>>>
``````

Knowing that the highest order bit is degree 8 (remember the 1’s bit is degree 0) we add 1 and get the bit number. Now subtract 2 from that and run your flip_bit function

``````b = flip_bit(a, 7)
``````

``````>>> int(2 ** 8.027905996569885)
261
>>> bin(261)
'0b100000101'
>>>
``````

#3

Exactly, but I thought why not, since we know what bit we want to flip, just write the number below the number of the bit we want to flip right away, so if we want to flip the 9th bit we write 8, or if we want to flip the 5th bit we write 4…

Oh ok, I see I see, but again that will make it easier for another user also, instead of them thinking “I want to flip the 9th bit, so I have to subtract 1 and write 8”, they can just write 9 and the calculation happens in the background without them knowing so that the 9th bit is flipped.

And Im guessing that that is the reason they didnt give us an example with a right shift, because its way more complicated than the left shift.

What does log do again?
I can see that int(a, 2) is basically (100000101, 2) which means 261, but what is log doing surrounding 261, and what is the number 2 inside log doing to 261?

And what is going on here…? I see that we turn the last line of code which gave us the number 8.02790etc into an integer an added 1 so that we get 9, but why do we do that?

Im not sure I get this, isnt the bit to the far right 9? Which is on 256s place?

I see whats going on here, we basically get to the number we got inside log earlier, but why do we do it again?

#4

We’re not talking other user. We’re talking about the program working with dynamic data that is either handed to the program or generated by it. The program works according to our instructions.

If you are still in grade school then you probably haven’t been exposed to logaritms. In a nut shell, the log, base 2 of a number is the exponent we have to raise 2 to in order to produce that number. See my last example where I applied the log as the exponent on 2 which gave us 261.

``````If 2 ** 8 is 256 then log, base 2 of 256 is 8.
``````

In high school academic math the exponent laws are covered in great detail, including logarithms. If one has no understanding of these laws then putting them into practice will be very hard. In which case we would revert to the `len()` function to find the degree.

``````a = '0b100000101'
b = len(a[2:])
print b    # 9
``````

We know that the degree is one less than the length, so bit 9 is degree 8.

Again, without knowledge of exponent laws, the term `degree` might also be confounding. Simply put, the degree on a term is the exponent.

``````y = x ** 2 + c
``````

Is a second degree equation since the highest degree term has exponent 2 on the base.

``````y = 2x ** 4 + 3x ** 3 - 4x ** 2 + 5
``````

The above polynomial is a fourth degree equation since the highest degree is 4.

int(log(int(a, 2), 2)) + 1

First off, to operate on a binary number we need to first construct it as an integer. We use the `int()` function for this, but also have to tell Python what base the number is that we’re passing it. In this case 2.

``````n = int(a, 2)
``````

Now we have a number we can pass to the log function. We need to tell the function the number base so it can compute the correct log. That’s what the other 2 is for.

``````log(n, 2)
``````

We want only the integer of the log so we get the degree on a single bit, not the degree of the actual number. In this case, the degree is 8, so by adding 1 we get the bit number.

#5

My head is about to explode right now hehe I can’t remember doing that type of math when I was a kid in school, so when I wake up tomorrow, I’ll dive in and look at the information you have provided here with a fresh mind and try to decode everything. So expect more questions from me tomorrow hehe

#7

So…I read through everything again and did some “research” on what natural logarithm is, so now I understand whats happening. When we write log(261, 2) and we get the number 8.027905996569885, it basically tells us how many times we have to multiply 2 by itself to get to our number which is 261, so basically 2 to the power of 8.027905996569885.

And I’ve come to the conclusion that is better to do right shift when we want to flip a bit, than to go through this whole process just to use left shift hehe

#8

To do a right shift we still need to know the degree of the highest order bit (which, by the way is on the left side, not the right) or the length.

``````>>> def flip_bit(number, n):
mask = 0b1 << n - 1
return bin(result)

>>> a = 261
>>> b = len(bin(a)[2:])
>>> c = flip_bit(261, b - 2)
>>> c
'0b101000101'
>>>
``````

#9

Btw, my bad, what I meant to say here is:

“better to do LEFT SHIFT when we want to flip a bit, than to go through this whole process just to use RIGHT SHIFT”

#10

I see what you mean, but thats why Im thinking its better to use left shift than right shift. Left shift seems to be the easier way to flip a bit.

#11

It is the practical form. Recall that you wanted to flip a bit that was counted from the left. We computed its position based upon length (or degree) then passed it to the practical function. Nothing to get too hung up at present but as your skillset grows, keep coming back to this unit and up the ante.

#12

I definitely will! I’ll finish the course in a week or two and then I plan revisiting every topic through the whole course to see that I still remember everything and then I’ll start digging deeper in each topic while I practice my python “skills” at codefights/codesignal.com. But I’ve heard the best way to get better and learn more is to create your own projects, but Im kind of stuck on figuring out what I want to do with Python, because I want to lets say build an app, both for lets say iPhone, but at the same time I want a person to be able to download that same app to their computer and use it there too, and maybe even have that app somehow built inside into my webpage, so that people can go to my website and use the app/software on my website if they dont want to download it on their phone or computer…SO the next big question is what do I need to learn to put this app/software together so that a person can use it. I hear that the framework Django is the way to go if you want to build websites, but does it mean that I can use Django to lets say create an app that people can use on my website, or downloaded on their phone or computer or is there something else I have to use to put my code together so that it becomes a functional app/software?

PS: Sorry for the long question!

#13

This question can go on the back burner for awhile. Beef up your general skills and add to that HTML and CSS so you get an understanding of the user interface, then gradually work toward app design. I am not the one to answer this question with regard to specifics, but I do recommend NOT starting a large project. Work on creating simple projects that can be written in a few hours or days, get them to work, then refine and refactor. It takes a lot of these sort of projects to build up a skillset sufficient enough to be able to tackle a full fledged web app.

#14

Thats fine and I agree, and yes I hear that html and css is needed and I’ll go through the courses on them again(I took them a couple of years back), but what do I need to put everything together.
Lets say I create those simple project, I write the code for lets say a simple calculator…I want to know how to use that calculator on my phone or my desktop without needing to execute the project through Python, but actually be able to push an icon that opens the app/software and a calculator with buttons and all pops up so that you can press the buttons on it and calculate stuff with it?

#15

A user cannot be expected to have Python on their machine. You could have Python on the server though, and use an API to hand values up to the server to be processed. This puts a burden on the server, though, and greatly affects scalability (increase in user base from IK to IM+ users). Once you learn JavaScript you can leverage it on the client side and only pass data and requests to the server that need to be run there (for privacy and security, or for database interaction).

#16

Yes thats what I meant, that a user pushes an icon that opens the app/software(not Python, but the app/software I’ve written).

And I want to use Python(and html css for the visuals) only, will I be able to create something then or do I need to learn another language/s? Because I really want to stick with one language, Python. And do everyhting with Python.

Very often I hear people say, Python developer, or Java developer etc. That gives me the impression that they use only that language to create stuff. I I would like to, as I said earlier, stick with one language, in my case Python(and html css for the visuals) since its the easiest to learn and its a very powerful language. That way I dont have to jump from language to language to do stuff, I simply want to learn how to create what I want in one language.

#17

If you are writing for yourself, or writing for other Python users, then you can create a client-side program that leverages a Python GUI, assuming the users also have that GUI in their environment.

Still, that is a ways off, for the time. There is a lot to learn, and trying to get too far ahead will cloud the waters and imporant lessons may be missed. Ambition is good, but temper it while you learn.

#18

First and foremost yes, Im writing for myself, but when I do so, I want to make a finished app that everybody can use, Im talking about the average Joe that goes to lets say the Apple app store and downloads my app. My focus is not developing something for people that are into programming, my focus is developing a “product” for a consumer, that can be anybody in the world.

So I want to write code, but I want turn the code into a finished app that everybody with a phone or a coumputer can use.

Besides that, I wouldn’t mind getting a developer job where I just do the coding and dont have to think about how the end product looks. Lets say Im told to write the code for a calculator, without thinking about the visuals etc, just like they ask us to do different stuff here in the course, that would be nice too. But because I have to create my own projects first, I need to know how to turn the code into a finished app so that I can put it in my portfolio when I look for a job. Because if I dont have a finished product to show for that shows my python “skills”, it will be hard to get hired.

I understand, but everyway I look, people say after you learn the basics, like we do here in the Python course, the best way to learn after that is by creating your own projects.

#19

Then you would need to leverage what the average user has for an environment… A browser. This means HTML/CSS and JavaScript will need to be in your toolkit, and Python would be running on the server the same way ASP, PHP, Rails, etc are implemented.

I’ve never built a full-fledged project with Python and keep learning everyday just tinkering with it (and solving other people’s problems). Mind I’m in my sixties and not looking to start a career. I don’t agree that the best way to learn is by doing projects of your own. That is one way to learn, and is akin to jumping down a rabbit hole.

Better that you can visualize and compose the components of a finished app, and write small projects that explore that component. You could spend days or weeks perfecting that tiny piece of the program. Error checking and mitigation, efficiency in terms of Big O, best practices, maintainability, upgradability, proof of concepts, reliability, and so on. There are so many technical concerns that come up when we start writing production code that we need to take the time to learn them all… Up front. Small, single purpose programs are the way to explore all these. Don’t listen to others who are trying to push you down the rabbit hole. Focus on the little things so they don’t come back around and bite you just before a deadline.

Learn the standard library, as well as the myriad of other special purpose libraries and how to implement them. Learn the dozens of ways to do the same thing so you can study the differences in performance, resource load, logic, readability, ease of debugging, and so on. There are no shortcuts. Take it slow and break off small pieces to build up your skills and knowledge base.

#20

I guess I need to learn JavaScript too then I was hoping to be able to create everything with Python and html/css

Yes and I want to learn that, but where?
Where do I go after I finish the Python Course here?
Where can I build up on what I’ve learned here?
Where can I do these small projects?
Where can I practice all of that?

And for a person that actually wants to work with that profedsionally, when do I know that I know enough so that I can apply for at least a junior developer job?

I’ve seen soooo many times people learn to code in 3-6 months (without a degree or prior knowledge of programming) and land a job…and I want to do that too, so where do I go next so that maybe I can also get good enough so that in lets say 6 months or a year I can be ready for a developer job(and of course continue learning while working as a developer)?

#21

For most of these questions I must defer to somebody who has actually landed a job with very little education. They would have been hired not so much on their ability, but on their attitude, mental acumen, personality, willingness to learn and fit into a group. They would not be handed a lead position on a product, but would be slotted in with a senior programmer who could direct and mentor them. They would only keep that job if their performance evaluations are positive as they grow into the organization.

If you are seriously looking to enter the field then start looking for more learning resources, especially in the paid-for category that give some certification of course completion and proficiency. Invest in recommended books and engage in things like codewars, etc. Just because something looks simple doesn’t mean it is. If you are going to rely upon your memory to be a good programmer then you better have perfect recall and photographic memory. It’s not what we know, as such, as much as how much we understand what we know, and how we think our way through problems to solve them. Programming is a thought process more than anything else. An employer who spots that skill will surely take a risk, but it does not guarantee a future with that organization, just a try out.

As stated before, there is a lot of work for you to do, and a lot of learning on the whole. Don’t try to skip around it.