1

I have scenario where for any given number i need to identify the corresponding 2 to the power of value. for example if the given number is 12:

12 is represented in 2 to the power as: 2 to the power of 3 and 2 to the power of 2 5 is represented in 2 to the power as: 2 to the power of 2 and 2 to the power of 0

Can i know the algorithm named on this scenario

Matthias
  • 8,018
  • 2
  • 27
  • 53
Santosh
  • 2,355
  • 10
  • 41
  • 64

1 Answers1

5

It's name is radix conversion. Convert your number to binary radix and you'll get your sum of power of 2. For example,

12 = 1100

That means:

     1           1          0         0
     ^           ^          ^         ^
     |           |          |         |
12 = 1 * (2^3) + 1* (2^2) + 0*(2^1) + 0*(2^0)
            |          |         |         |
            V          V         V         V
            3          2         1         0

-it's by definition of what radix (numeral base) is.

Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • Depends of your DBMS. In MySQL, there's [`CONV()`](https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_conv), for example – Alma Do Jan 29 '14 at 08:37
  • what is the matching function in SQL Server for CONV() – Santosh Jan 29 '14 at 09:24
  • I'm not SQL Server expert, but as far as I know, there's no in-built equivalent for that. Instead you'll have to use something like [this](http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=109916) or [this answer](http://stackoverflow.com/questions/2568668/base-36-to-base-10-conversion-using-sql-only) – Alma Do Jan 29 '14 at 09:30