Member-only story
Code Challenge: Bitwise Basics in Swift
After helping iOS developers prepare for technical interviews, I like to reconnect with folks to discuss tough questions that they were asked. Recently, I came across an interesting challenge that involved binary notation and thought it worthy of a review. Even though it’s rare for hiring managers to ask candidates questions on this topic, this one has less to do with the notation and more to do with data management. For example:
For this challenge, our goal is to convert the binary value of “1001” to its regular base-10 equivalent. For our implementation, we’ll somehow manipulate our structure to return an Int `value of 9. As part of our analysis, we can see the binary value isn’t a regular String but is organized through a singly linked list.
Review & Analysis
This challenge involves a few key areas that we can break down into more detail. To start, since our goal is to return a base-10 value, we should do a quick refresher on how this conversion process works. Many of us (myself included) consider bitwise operations to be an advanced topic, but much of it is based on regular addition and multiplication. Even though Swift has default functions for doing these types of operations, let’s review how this works:
As shown, each binary digit (e.g. 0 or 1) converts to a base-10 equivalent…