| Bytes | Lang | Time | Link |
|---|---|---|---|
| 028 | Python 3 | 241008T204519Z | jdt |
| 004 | 05AB1E | 241010T144549Z | Kevin Cr |
| 023 | JavaScript ES6 | 241008T224008Z | Arnauld |
| 003 | Vyxal | 241008T230652Z | emanresu |
| 005 | Uiua | 241008T195211Z | nyxbird |
Python 3, 28 bytes
lambda n:bin(n).count('1')+n
Python 3.10, 24 bytes
lambda n:n.bit_count()+n
As suggested by @squareroot12621
05AB1E, 4 bytes
Three different minor alternatives:
bSO+- Try it online or verify multiple test cases at once;b1ö+- Try it online or verify multiple test cases at once;bIªO- Try it online or verify multiple test cases at once.
Explanation:
b # Convert the (implicit) input-integer to a binary-string
S # Convert it to a list of bits
O # Sum those bits
+ # Add it to the (implicit) input-integer
# (after which the result is output implicitly)
b # Convert the (implicit) input-integer to a binary-string
1ö # Convert it to base-1, which sums its digits
+ # Add it to the (implicit) input-integer
# (after which the result is output implicitly)
b # Convert the (implicit) input-integer to a binary-string
Iª # Convert the binary-string to a list of bits, and append the input-integer
O # Sum this list of bits with appended integer together
# (after which the result is output implicitly)
JavaScript (ES6), 23 bytes
f=n=>n&&f(n^(n&=-n))-~n
Explanation
Recursion seems like the most code-golf-friendly way of doing this in JS. However, counting the set bits and adding n to the total is not the shortest method because we need to either use n before the recursion starts or remember its original value for the final step.
For example:
n=>n+(g=k=>k&&-~g(k&k-1))(n) // 28 bytes
f=(n,k=n)=>n?-~f(n&n-1,k):k // 27 bytes
Hence the idea of doing both tasks at the same time in the recursion each time a set bit is encountered, as described below.
Commented
f = n => // f is a recursive function taking n
n && // stop if n = 0
f( // otherwise, do a recursive call with:
n ^ // n without
(n &= -n) // its least significant set bit
// which is saved in n
// (e.g. 6 AND -6 --> 2, 6 XOR 2 --> 4)
) // end of recursive call
- ~n // add 1 + the value of the extracted bit