C#
programming
powers of 2
sum calculation
algorithms

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.

Practice algorithms

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:

csharp
1using System;
2
3static long SumPowersOfTwo(int c)
4{
5    if (c < 0)
6    {
7        throw new ArgumentOutOfRangeException(nameof(c));
8    }
9
10    return (1L << (c + 1)) - 1;
11}
12
13Console.WriteLine(SumPowersOfTwo(0)); // 1
14Console.WriteLine(SumPowersOfTwo(3)); // 15
15Console.WriteLine(SumPowersOfTwo(5)); // 63

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:

csharp
1using System;
2using System.Collections.Generic;
3
4static List<int> PowersOfTwoInNumber(int value)
5{
6    if (value < 0)
7    {
8        throw new ArgumentOutOfRangeException(nameof(value));
9    }
10
11    var powers = new List<int>();
12    int bit = 0;
13
14    while (value > 0)
15    {
16        if ((value & 1) == 1)
17        {
18            powers.Add(1 << bit);
19        }
20
21        value >>= 1;
22        bit++;
23    }
24
25    return powers;
26}
27
28var powers = PowersOfTwoInNumber(13);
29Console.WriteLine(string.Join(" + ", powers)); // 1 + 4 + 8

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 = 3 as an exponent, then 2^0 + 2^1 + 2^2 + 2^3 = 15
  • If c = 3 as a value, then 3 = 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:

csharp
1using System;
2
3static long SumPowersOfTwoWithLoop(int c)
4{
5    long sum = 0;
6    long current = 1;
7
8    for (int i = 0; i <= c; i++)
9    {
10        sum += current;
11        current *= 2;
12    }
13
14    return sum;
15}
16
17Console.WriteLine(SumPowersOfTwoWithLoop(4)); // 31

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 integer c into powers of two."
  • Using int when the shift result can overflow. long is safer for larger exponents.
  • Forgetting that 1 << n uses int arithmetic unless you start with 1L.
  • 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 c is the highest exponent, use 2^(c + 1) - 1.
  • In C#, (1L << (c + 1)) - 1 is a compact way to compute that sum.
  • If c is 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 c means is the most important part of solving the problem correctly.

Course
Intermediate
27 lessons
15 hours
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 course
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.

Practice algorithms