CodeFights and Re-Learning About Bit Manipulation

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.

CodeFights

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 CodeFights.com. 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.

Takeaway

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.





Doug Watkins
Find me on:

About Doug Watkins

Developer of My Food Storage and Go Coder, available now on the app store. Owner of DougWatkinsDev.com.
Bookmark the permalink.

Comments are closed.