Get the sum of powers of 2 for a given number c
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Questions about the sum of powers of two usually mean one of two things. Either you want the geometric-series sum 2^0 + 2^1 + ... + 2^c, or you want to express a number as a sum of powers of two, which is exactly what binary representation does.
Those are related ideas, but they are not the same problem. In C#, both are easy once you choose the correct interpretation.
Case 1: Sum All Powers From 2^0 Through 2^c
If c is the highest exponent, the series has a closed-form solution:
2^0 + 2^1 + ... + 2^c = 2^(c + 1) - 1
That means you do not need a loop at all:
The bit shift works because 1L << n is the same as 2^n for non-negative integer n. So 1L << (c + 1) gives 2^(c + 1), and subtracting 1 gives the final sum.
This is an O(1) solution, which is better than adding the values one by one.
Case 2: Express a Number as a Sum of Powers of Two
Sometimes the real task is different: given a number such as 13, find the powers of two that add up to it. In that case, you are asking for the set represented by the number's binary digits:
- '
13 = 8 + 4 + 1' - or
13 = 2^3 + 2^2 + 2^0
In C#, bit operations make that straightforward:
Each set bit means that power of two participates in the sum. That is why binary notation is so useful: every non-negative integer already is a sum of powers of two.
Choosing the Right Interpretation
The two problems produce very different results:
- If
c = 3as an exponent, then2^0 + 2^1 + 2^2 + 2^3 = 15 - If
c = 3as a value, then3 = 2 + 1
That distinction is where many bugs start. If the caller says "given a number c", clarify whether c is:
- the exponent limit
- or the actual number to decompose
Once that is clear, the implementation is simple.
A Loop Version for the Geometric Sum
Even though the formula is better, a loop can still be useful for learning or debugging:
This is easier to read if bit shifting feels too compact, but it is still solving the "sum from 2^0 through 2^c" problem, not the decomposition problem.
Common Pitfalls
- Confusing "sum up to exponent
c" with "decompose the integercinto powers of two." - Using
intwhen the shift result can overflow.longis safer for larger exponents. - Forgetting that
1 << nusesintarithmetic unless you start with1L. - Trying to loop through all powers when the closed-form formula already exists.
- Assuming negative values behave meaningfully in the same code. For most versions of the problem, input should be non-negative.
Summary
- If
cis the highest exponent, use2^(c + 1) - 1. - In C#,
(1L << (c + 1)) - 1is a compact way to compute that sum. - If
cis the number itself, use its binary representation to find the powers of two that add up to it. - Bit operations are the natural tool for the decomposition version.
- Clarifying what
cmeans is the most important part of solving the problem correctly.
Related reading
- Getting all possible combinations from a list of numbers
- Getting all possible sums that add up to a given number
- Getting century from year
- Getting median out of frequency table counting sort
- Get Timezone Offset of Server in C
- Get URL parameters from a string in .NET
- Getting parent of a vertex in a perfect binary tree
- Getting the closest string match

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.