4

I am reading this material to understand the basics of number system. I am stuck at a point in that material where it writes the algorithm to convert a decimal number to binary number. The heading of that part where I am stuck is Decimal to Base The algorithm (may be presented less than faithfully, please refer the link) it mentions there is:

  1. Let $p = \lfloor \sqrt{V} \rfloor$
  2. Let $v = \lfloor \dfrac V {B^p} \rfloor$
    (v is the next digit to the right)
  3. Make $V = V − v * B^p$
  4. Repeat steps 1 through 3 until $p = 0$

It is explaining by taking an example of converting decimal number 236 to binary.

I am not getting how it is calculating the 1st step, i.e. to get the value of p.

It writes that p = int(square root of V)

Now, square root of 236 = 15.36229149573721635154

As per point number 1, p = integer part of 15.36229149573721635154 So, I remove the decimal part and p then becomes 15. But the material there says it is 7.

I can't get what is happening here. I am stuck.

greybeard
  • 1,172
  • 2
  • 9
  • 24
Ravi
  • 151
  • 1
  • 1
  • 6

3 Answers3

5

Just converting the comment into a short answer:

$7 = \text{int}(\log_{2} 236)$. Generally, $p = \text{int}(\log_{B}V)$.


As other people pointed out, this algorithm is needlessly complicated and not practical; it is not easy to calculate $\log_B V$ for large $V$ by pencil and paper. Instead, use the other algorithm which is also mentioned in the article you are reading:

From decimal to binary

  • Step 1: Check if your number is odd or even.
  • Step 2: If it's even, write 0 (proceeding backwards, adding binary digits to the left of the result).
  • Step 3: Otherwise, if it's odd, write 1 (in the same way).
  • Step 4: Divide your number by 2 (dropping any fraction) and go back to step 1. Repeat until your original number is 0.
hengxin
  • 9,671
  • 3
  • 37
  • 75
1

In Cisco academy for doing the math by heart we know the powers of two, to allow us to do calculations by head. Note that the algorithm only is efficient once you can do it on your head, as it is mostly subtractions once you are familiar with the procedure.

The algorithm works roughly this way:

  • you pickup the power of two immediately lower to your value
  • do
  • number lower power == 0
  • number greater power == 1 ; and number = number - power
  • go down to next power of 2
  • while number not 0

If the number comes to 0 before the power end, you fill out the others with 0.

So for your value of 236, I know the powers bellow are: 128 64 32 16 8 4 2 1

So, lets see the powers of 2^n

(7) 236>128==1, number=236-128=108
(6) 108>64==1, number=108-64=44
(5) 44>32==1, number=44-32=12
(4) 12>16==0
(3) 12>8==1, number=12-8=4
(2) 4>=4==1, number=4-4=0 number==0 ends calculations
(1) ==0
(0) ==0

So 236 is 11101100 in binary.

0

As long as the input as non-negative integer, you don't need to perform ANY divisions at all if you want.

Because there are 2 directions to convert, so you can "pull up" the bits instead of "pulling it down" (which is the standard odd/even then keep dividing by 2 approach).

The only extra up front processing you need is figuring out what's the next power of 2 above it. The function jumps every 4, so it shouldn't take too many loops to figure out how large the input is (without using the log2() function)

Let's use the same example 236 :

function dec2bin(__, _, ___, ____, _____) {
if ((__ = int(__)) < (___ = (_ += _ ^= _ < _) + (_____ = _)))

    return __ < _ ? __ : __ + _ * ___
else
    while ((___ += ___ += ___) < __)_____ += _

if ((_____ += (____ = "") + _) && (___ - __)^_ < _)

    return ___ <= __ \
        ? sprintf("%d%.*d", --_, _____, ___ < __)
        : substr((_ = (_ = (_ = (--_)_ (_)_)_ (_)_)_ (_)_)_,
                                               !!___, _____)
else if (_____ % _ && 
         _____--)____ = (___ <= (__ += __) &&
                           _ + (__ -= ___))____
    _____/= _
        _ = ___
      ___ = ____

while (_____--)___ = (___) (_ <= (__ += __) &&
            (__ -= _) + _) (_ <= (__ += __) &&
            (__ -= _) + _)
return ___

}


236 11101100

The single pair of modulo + division that exists in the code is for doubling the speed and processing 2 bits at a time. Other than that, absolutely no division, modulo, bit-masking, or bit-shifting ops were needed.

It also has rapid handling logic for x < 4 and x within 2^n ± 1