Binary number system

Unary, where we just write 1, 11, 111… just goes on forever. Binary, with two options (1 and 0) looks like this:

    1: 1
    2: 10 (we’re full – tick over)
    3: 11
    4: 100 (we’re full again – tick over)
    5: 101
    6: 110
    7: 111
    8: 1000 (tick over again)

and so on …

Now, how do we convert an arbitrary binary number to its decimal equivalent? Obviously counting and matching is not an option.

Let us first look at the value of 10000...n times ( 1 followed by n 0s ):

This number will only come after we have tried all possible n digit numbers in the binary system which has:

      2 * 2 * 2 * .. n times possibilities  ( every position can either have 0 or 1 ) 
   =  2^n possibility which covers numbers from 0 to 2^n - 1.

So, the value of 1000..n times = 2n.

Now any number like 1010110 can be expressed as 1000000 + 10000 + 100 + 10 = 2^6 + 2^4 + 2^2 + 2^1.

Hence, if the binary representation is An An-1 An-2 ... A1 A0, then the corresponding value is

A0 * 2^0 + A1 * 2^1 + A2 * 2^2 + .... + An * 2^n

Now look at the reverse process. Which is given a decimal number N, how do we convert it to binary.
To do this conversion, we need to divide repeatedly by 2, keeping track of the remainders as we go. Watch below:
Let us say our number is 357.

Thus the number converts to 101100101

Walkthrough Example :
DECBIN

Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

Math Problems

Base conversion
Problem Score Companies Time Status
Excel Column Number 175 23:19
Excel Column Title 175 41:11
Digit op
Problem Score Companies Time Status
Palindrome Integer 200
HCL
17:28
Reverse integer 200 28:36
Next Smallest Palindrome! 400 63:42
Combinatorics
Problem Score Companies Time Status
K-th Permutation 200 51:38
City Tour 300
62:42
Grid Unique Paths 375 32:35
Hash search
Problem Score Companies Time Status
Highest Score 200 34:50
Bit play
Problem Score Companies Time Status
Addition without Summation 200 26:07
Number encoding
Problem Score Companies Time Status
Next Similar Number 200 55:47
Rearrange Array 250
49:03
Array dp
Problem Score Companies Time Status
Numbers of length N and value less than K 200
90:00
lock
Topic Bonus
Bonus will be unlocked after solving min. 1 problem from each bucket