Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Go to Problems

# 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 :

## Serious about Learning Programming ?

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

## Math Problems

Problem Score Companies Time Status
Total Moves For Bishop! 75 30:55
Distribute in Circle! 100 21:33
Prime Sum 150 43:15
Sum of pairwise Hamming Distance 200
66:52
FizzBuzz 200
12:34
Is Rectangle? 200 11:17
Step by Step 200 50:47
Power Of Two Integers 250 39:36
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 17:28
Reverse integer 200 28:36
Next Smallest Palindrome! 400 60:21
Number theory
Combinatorics
Problem Score Companies Time Status
K-th Permutation 200 60:31
City Tour 300
61:35
Grid Unique Paths 375 32:35
Hash search
Problem Score Companies Time Status
Highest Score 200 27:59
Bit play
Problem Score Companies Time Status
Number encoding
Problem Score Companies Time Status
Next Similar Number 200 55:07
Rearrange Array 250
49:03
Array dp
Problem Score Companies Time Status
Numbers of length N and value less than K 200
89:59

Problem Score Companies Time Status
Sum of 7's Multiple 200 22:58
Find Last Digit 200 29:54
Last digit K count 200 23:42
Digital Root 200 13:44
Number of Sundays 200 21:28
Product of Digits 150 9:46
Round Table 150 18:32
Odd Even Rule 150 11:20
Armstrong Number 200 13:05
Leap Year 100 9:14
Lowest Common Multiple (LCM) 100 18:14
Which Season? 100 9:59
Socks Pair 100 15:05
Two Stores 150 35:11
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *