Suggestions on how to calculate something

I am trying to calculate the height of nodes in a tree. I think that if I take the closest power of 2 that is larger than the number of nodes in a tree and subtract one from it, that would be the height of the node. For example,

if a tree has 15 nodes, the closest power of 2 higher than 15 is 4, so the height of that node with respect to the node is 3. So it seems that it works, but I am having trouble coding it. Can anyone provide tips or suggestions to get me on track? Thanks.