1316. Number of 1-Bits in All Numbers from 0 to N

EasyBit ManipulationBit ManipulationCountingMath

Given a non-negative integer n, return the total number of set bits across all integers from 0 to n inclusive.

Input: A JSON object {"n": <non-negative integer>}.

Output: Return the total count of 1-bits in 0,1,...,n.

Examples

Example 1
Input: {"n":5}
Output: 7
Explanation: Set bits of 0..5 are 0,1,1,2,1,2 summing to 7.
Example 2
Input: {"n":0}
Output: 0
Explanation: Only 0, which has no set bits -> 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →