CodeFights and Re-Learning About Bit Manipulation

Find me on:
iOS Developer at Doug Watkins Development
Developer of My Food Storage and Go Coder, available now on the app store. Owner of
Doug Watkins
Find me on:

never-memorize-something-that-you-can-look-upSince my background at Uni started in Computer Engineering and Computer Science, I know more than most people about how computers work. I understand things that few people – even people who use computers well and frequently – understand. Remember this because it is the real reason I am writing today.


I recently clicked on an ad on Facebook – it does happen sometimes, usually only to ads that are highly targeted with things I’m actually interested in – that brought me to I was intrigued about coding against bots, and I love testing my ability to solve coding problems. So I signed up – its free – and wrote some code.

Because of the way that I work, and learn, I can’t tell you exactly how to do something off the top of my head. I learn that things exist, and I learn how to implement them. But if I don’t need to implement something frequently – every time I sit down to code – I usually forget the specifics about implementation.

Thank you Internet, because now I can leave that brain shelving space open for other things. Now, when I come upon a problem, I know that there is a way to accomplish some small part of the task, and I know how to search and find the exact details required to implement it.

A simple Google search remind me of exactly how to use what I know exists. And in the case that I don’t know something exists, Google again comes to the rescue. This is the reason I included the quote above, I believe strongly in this way of thinking.

Now that we understand how my development process works a little, lets get into more detail about a specific challenge I had the other day.

Bit Manipulation

While taking digital circuits at USU, almost everything we did involved bits and bit manipulation. But since changing my major to MIS Database Administration, I haven’t had a reason to put any of that information to use. Nor have I wanted to.

I changed majors because I hated the process of solving problems for Computer/Electrical Engineering. I had greater highs from the work, but also lower lows – and more of them.

I’ve mostly forgotten that information/ability or put it on the back shelf of my brain.

I’ve also never had to actually manipulate bits directly in my Computer Science studies. Nor have I had to use it to write the software that I write.

I understand much of what my software is doing in the background – but I don’t write software that directly controls that since I’m not writing game engines. I usually have my chosen language do it for me.

When I took digital circuits we had to design and implement a CPU – it sucked but was totally awesome at the same time. So I understand the basics of how programs are actually executed.

The Intersection of CodeFights and Bit Manipulation

Now that we have a little back story, we can get into what I actually experienced.

I was on CodeFights going through the arcade section which are small coding challenges to accomplish specific tasks. The first MANY of these were exceedingly simple, at least for me. Things I would consider basic programming. Manipulate an array or basic logic and program control structures like loops and if statements.

They don’t tell you how to solve the problem, so it can seem more complicated if you don’t have a firm grasp of Computer Science basics. The problems themselves are pretty straightforward, and usually are solved with less than 10 lines of code.

Then I hit a snag. I started the arcade section called “The Intersection of 0’s and 1’s”.

The first problem through me for a loop. I needed to check to see if a specific bit was set – a 1 – and if it was I needed to unset it – to 0 – and return the result as an Int.

Ok, I’m not sure how to do this. I have literally NEVER manipulated bits in this manner, not in code at least. So I start coding some sort of if statement to check for the set bit – figuring I’d add the actual code to check the bit in a minute after some Googling – and add a 0 return value to just try compiling the code. I was thinking that maybe I could print some things out and hobble my way along till I get it working. Click “Run Tests” aaaaannnnndddddd

Error – I can only edit the that followed the return statement. So the whole program has to run on a single line. Crap.

Finding a Solution

So I start to Google. I find familiar faces in the Swift documentation – bitwise logic and math operators – but none will do exactly what I want.

So I started to search the forums on CodeFights to see if anyone posted anything about this challenge.

I found something about a different challenge. One answer posted a link to a blog post about Low Level Bit Hacks.

At first, it seemed like all the same things I found in the Swift docs, but then I realized something.

I was reading about using the bitwise AND – & – to change a number. You & two numbers together, and every time a specific bit is a 1 on both numbers, the result number is a 1, otherwise its a zero. Great, except in the problem I’m not sure how to use this to only check, and change 1 bit – not only that, but I need the bit to be a 0 not a 1 which is the output of &.

Lightbulb Time

I could use two three things at the same time to accomplish what I want to. Using & and shifting 1 the necessary number of times.

Using 1<<x allowed me to create a binary number of 1 followed by x 0’s -> 1<<3 creates 0b00001000. This I could use to & the original number with – except that this doesn’t do me any good by itself. I have to first NOT – ~ – the shifted number. Now the shifted number becomes 0b11110111 – thus allowing me to make sure that the xth bit is 0.

&ing this works because all bits are 1 except the one I want to be zero. All bits that are 1 in the original number will be ones, and all zeros will be zeros, except that one bit I want to change which will always be a zero now.

Needless to say, this was an interesting experience. And I’m glad I did it, because now I am a slightly better programmer – even though I’ll probably never use this information again, though I might.


I guess the reason I’m writing this is because I want programmers out there to know that you don’t need to know everything. All you need to know are the basics, and how to find the information you want.

Being able to do things like this without looking them up comes with time, but in the end it only makes you a slightly faster programmer.

What is more important is being able to write solid code – code for all edge cases, code for scalability, code for reusability, and code for stability <I’m sure there are more> – and know how to fill in any gaps you have in the language you are using currently.

That being said, what about you? What experiences have you had where you discovered something new? Or rediscovered something you forgot? Or just plain old did it without being able to spout out the answer from memory? Let me know in the comments below.

Also, if you sign up for CodeFights, add me as a friend.

If you like what you see and want to keep updated, as well as be informed about when I release new freebies, sign up for my newsletter below for more tips and advice on growing an app business.

Firebase Account Creation Failure Without an Error

Find me on:
iOS Developer at Doug Watkins Development
Developer of My Food Storage and Go Coder, available now on the app store. Owner of
Doug Watkins
Find me on:

Why I Decided to Use Firebase, and an Interesting Error I Got Because of 3 Characters

Before I get into the Firebase error I got, or more appropriately the error I didn’t get but should have gotten, lets discuss why I chose Firebase in the first place.

firebaseI recently decided to use Firebase to do authentication and database storage for a few apps I am working on. Both for myself and some freelance clients. Firebase was my choice for three reasons:

  1. The backend for the auth and database storage is already done and well tested.
  2. I planned to sell the apps, so paying money for Firebase if I go over their free limits isn’t a big deal.
  3. Parse is no longer an option. I had been planning to use it for these apps for a while and was disappointed to find out it was being discontinued.

While researching Parse alternatives, I came across a tutorial on Ray Wenderlich’s site, and became intrigued by Firebase. Finding out it was a Google product increased my interest, since in my experience Google puts out fairly good products.

So, after doing a little research, I decided upon Firebase and set to work creating an abstract project. Once done I’m turning it into a framework, and then a cocoapod, for use in future apps. This app would do user login/authentication, user account creation, password reset requests, and database communication.

My Journey With Firebase

simulator-screen-shot-oct-13-2016-3-57-56-pmFirst I started working on the sign in screen and user creation screen.

The UI is pretty simple for both. It is just a series of TableViewCell’s with text inputs used to create, or sign in to their account. I had to add some extra inputs on the creation screen because I want to add new users to my email list unless they opt out.

Because I planned on the entire project being used for multiple projects in the future, it was fairly simple to abstract everything out and make the data collected by the app adjustable by simply changing certain constants all contained within a specific file.

Now that I have everything abstracted, but also concrete by setting the specific constants to what I plan to use for several apps, I was ready to finally test creating an account. Once I had an account created I could start testing the login screen – an important thing to note here is that I didn’t want to create an account through the webpage console for Firebase, I wanted to do it solely through the app … remember this point because it become significant.

simulator-screen-shot-oct-13-2016-3-58-02-pmTesting the Account Creation

So, I’m ready to go, run the app click the account creation button, and fill out the form.

Because I am doing this on my phone, I really dont want to type all my actual information out. So I used strings like “asd” for both names – the name fields were just random letters. The email was “” because it was quick and much easier to type than my real email.

The last part of the form is a password which I didn’t want a full, long, real password for testing, so I used “bim” because it was short and I could remember it.

I finally clicked “Create Account” and waited for the magic, which never happened.

Dang! Now I get to pore through documentation and find out whats going on.

What Went Wrong, and What I Did to Fix it

Usually the Google machine and Stack Overflow keep me going. If something doesn’t work, they almost always have the answer. This moment marks the second time I’ve been let down by the two of them. Though SO did eventually rescue me from the first one, but I had to actually ask a question instead of just search through answers.

Firebase documentation states specifically that if there is no error returned by the create account call, then the account was created successfully. The only problem was I was getting no error, nor was the account being created! I FRIGGIN BROKE FIREBASE!?!?!

Almost, but not exactly. After searching for multiple hours for a solution, and finding zero helpful information, I accidentally stumbled upon the problem. I tried to create the same account on the website – the thing I wanted to avoid doing – and got a freaking error.

What The Crap Google! Here I am not being sent an error – but the website is throwing an error. Everything was the same on the app and the website.

So, what went wrong you may be asking yourself … The password was the problem.

Firebase Password Documentation

The Firebase docs say that any password authentication needed to be done by me before creating the account. For these particular apps I am currently working on, I really don’t care if people use a 1 character password – so I assumed that since it was left to me, there were no restrictions placed by Google, but there are.

Firebase passwords must be 6 characters or more. This is the error message I finally got while trying to create a password online. Tried this in the app and bam, works like a charm.

Why oh why does this not throw an error – or at least have a mention in the documentation somewhere. I am actually going to write Google about the issue and ask them to update the documentation or cause an error to be thrown by the app.

In the meanwhile I am putting this here – as well as Stack Overflow. That way any other unsuspecting people can find a possible solution to their problem.

If you like what you see and want to keep updated, as well as be informed about when I release new freebies, sign up for my newsletter below for more tips and advice on growing an app business.