Given a number **N**, the task is to find the sum of Hamming difference of consecutive numbers from 0 to N.

Hamming Distance between two integers is the number of bits which are different at the same position in both numbers.

**Examples:**

Input:5Output:8Explanation:

Difference between (0, 1) = 1, (1, 2) = 2,

(2, 3) = 1, (3, 4) = 3, (4, 5) = 1.

So the total sum is 1 + 2 + 1 + 3 + 1 = 8Input:9Output:16

**Naive and Logarithmic Approach:** Refer to Sum of bit differences for numbers from 0 to N for the naive approach and a logarithmic approach to calculate sum based on checking whether **N** as powers of **2** or not.

**Approach:** In this article, an approach based on observing the number of changes occurring in all the bits starting from the LSB to the MSB is discussed.

Follow the steps below to solve the problem:

- It can be observed that the
**Least Significant Bit**will change**N**times. The 2nd Least significant bit will change**floor(N/2)**times (i.e**(N – 1)/2**if**N**is odd and N/2 if N is even). Hence, the i^{th}bit will change floor(N/2^{i}) times.

- Hence, to solve this problem we can simply store the sum

N + floor(N/2) + … floor(N/2

^{i})

- until the term does not become zero.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate and ` `// return the hamming distance ` `// between all consecutive ` `// numbers from 0 to N ` `int` `TotalHammingDistance(` `int` `n) ` `{ ` ` ` `int` `i = 1, sum = 0; ` ` ` `while` `(n / i > 0) { ` ` ` `sum = sum + n / i; ` ` ` `i = i * 2; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 9; ` ` ` `cout << TotalHammingDistance(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement the ` `// above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to calculate and ` `// return the hamming distance ` `// between all consecutive ` `// numbers from 0 to N ` `static` `int` `TotalHammingDistance(` `int` `n) ` `{ ` ` ` `int` `i = ` `1` `, sum = ` `0` `; ` ` ` `while` `(n / i > ` `0` `) ` ` ` `{ ` ` ` `sum = sum + n / i; ` ` ` `i = i * ` `2` `; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `9` `; ` ` ` ` ` `System.out.println(TotalHammingDistance(N)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to calculate and ` `# return the hamming distance ` `# between all consecutive ` `# numbers from 0 to N ` `def` `TotalHammingDistance(n): ` ` ` ` ` `i ` `=` `1` ` ` `sum` `=` `0` ` ` ` ` `while` `(n ` `/` `/` `i > ` `0` `): ` ` ` `sum` `=` `sum` `+` `n ` `/` `/` `i ` ` ` `i ` `=` `i ` `*` `2` ` ` ` ` `return` `sum` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `9` ` ` ` ` `print` `(TotalHammingDistance(N)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to implement ` `// the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to calculate and ` `// return the hamming distance ` `// between all consecutive ` `// numbers from 0 to N ` `static` `int` `TotalHammingDistance(` `int` `n) ` `{ ` ` ` `int` `i = 1, sum = 0; ` ` ` `while` `(n / i > 0) ` ` ` `{ ` ` ` `sum = sum + n / i; ` ` ` `i = i * 2; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 9; ` ` ` `Console.Write(TotalHammingDistance(N)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

16

**Time Complexity:** O(logN) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
- Check if a number can be expressed as a sum of consecutive numbers
- Find the prime numbers which can written as sum of most consecutive primes
- Express a number as sum of consecutive numbers
- Count ways to express a number as sum of consecutive numbers
- Expressing factorial n as sum of consecutive numbers
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
- Expressing a number as sum of consecutive | Set 2 (Using odd factors)
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of series formed by difference between product and sum of N natural numbers
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- 1 to n bit numbers with no consecutive 1s in binary representation.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.