"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"# Solution Notebook"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Problem: Determine if a number is a power of two.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Constraints\n",
"\n",
"* Is the input number an int?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Is the output a boolean?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Test Cases\n",
"\n",
"* None -> TypeError\n",
"* 0 -> False\n",
"* 1 -> True\n",
"* 2 -> True\n",
"* 15 -> False\n",
"* 16 -> True"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Algorithm\n",
"\n",
"We can use bit manipulation to determine if a number is a power of two. \n",
"\n",
"For a number to be a power of two, there must only be one bit that is a `1`. \n",
"\n",
"We can use the following bit manipulation trick to determine this:\n",