[Question]
  [
A positive integer can be *diluted* by inserting a `0` between two bits in its binary expansion. This means that an `n`-bit number has `n-1` dilutions, which are not necessarily all distinct.
For example, for `12` (or `1100` in binary), the dilutions are
```
11000 = 24
   ^
11000 = 24
  ^
10100 = 20
 ^
```
In this challenge, we're going to be taking the sum of all the dilutions, exclusive of the original number. For `12`, taking the sum of `24, 24, 20` results in `68`, so `68` should be the output for `12`.
### Challenge
Given a positive integer `n > 1` as input, output/return the diluted sum as explained above.
### Examples
```
in    out
---   ---
2       4
3       5
7      24
12     68
333  5128
512  9216
```
### Rules
* The input and output can be assumed to fit in your language's native integer type.
* The input and output can be given in [any convenient format](http://meta.codegolf.stackexchange.com/q/2447/42963).
* Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
* [Standard loopholes](http://meta.codegolf.stackexchange.com/q/1061/42963) are forbidden.
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so all usual golfing rules apply, and the shortest code (in bytes) wins.
  
[Answer]
# [Python 2](https://docs.python.org/2/), ~~43~~ 39 bytes
```
f=lambda n,i=2:n/i and n*2-n%i+f(n,i*2)
```
[Try it online!](https://tio.run/##FcjBCkBAEAbgu6f4L5tdRjKb1JaHWWkzxRAunn5x@/qO51525ZzTuMZtmiOUZOSgrSDqDK24USN1st9X7HLaTwhEwQRPGAjdL/@57zgUwHGK3iiNvwLMVRorhGTFufwC "Python 2 – Try It Online")
---
## How?
Each call of the recursive function calculates a single dilution. The position of the inserted `0` is `log2(i)`. The function recurses until `i` gets bigger than `n` and the insertion would be on the left of the number. If `i>n`, `n/i` evaluates to `0`, which is a falsy value in Python.
`n*2` shifts the entire number one binary digit left, `n%i` or `n % 2**(position of insertion)` calculates the value of the part that should not be shifted left. This value gets subtracted from the shifted number.
## Example (n=7)
```
call       n/i          bin(n)  n*2     n%i   dilution       return value
f(7, i=2)  3 => truthy  0b111   0b1110  0b1   0b1101 = 13    13 + f(7, 2*2) = 13 + 11 = 24
f(7, i=4)  1 => truthy  0b111   0b1110  0b11  0b1011 = 11    11 + f(7, 4*2) = 11 + 0 = 11
f(7, i=8)  0 => falsy                                        0
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 11 bytes
```
BJṖ2*ɓdḅḤ}S
```
[Try it online!](https://tio.run/##y0rNyan8/9/J6@HOaUZaJyenPNzR@nDHktrg/0f3HG5/1LTG/f9/Ix0FYx0Fcx0FQxDLGMg2NTQCAA "Jelly – Try It Online")
### How it works
```
BJṖ2*ɓdḅḤ}S  Main link. Argument: n (integer)
B            Binary; convert n to base 2. This yields a digit array A.
 J           Indices; yield [1, ..., len(A)].
  Ṗ          Pop; remove the last element, yielding [1, 2, ..., len(A)-1].
   2*        Elevate 2 to these powers, yielding [2, 4, ..., 2**(len(A)-1)].
             Let's call the result B.
     ɓ       Begin a new, dyadic chain, with left argument n and right argument B.
      d      Divmod; yield [n/b, n%b], for each b in B.
        Ḥ}   Unhalve right; yield 2b for each b in B, i.e., [4, 8, ..., 2**len(A)].
       ḅ     Unbase; convert each [n/b, n%b] from base 2b to integer, yielding
             (2b)(n/b) + (n%b).
          S  Take the sum.
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 13 bytes
```
tZl:W&\5ME*+s
```
Try it at [MATL Online!](https://matl.io/?code=tZl%3AW%26%5C5ME%2a%2Bs&inputs=333&version=20.6.0) Or [verify all test cases](https://matl.suever.net/?code=jU%22%40%0AtZl%3AW%26%5C5ME%2a%2Bs&inputs=2+3+7+12+333+512&version=20.6.0).
### Explanation
Consider input `12` as an example.
```
t     % Implicit input. Duplicate
      % STACK: 12, 12
Zl    % Binary logarithm
      % STACK: 12, 3.584962500721156
:     % Range (rounds down)
      % STACK: 12, [1 2 3]
W     % Power with base 2, element-wise
      % STACK: 12, [2 4 8]
&\    % 2-output modulus, element-wise: pushes remainders and quotients
      % STACK: [0 0 4], [6 3 1]
5M    % Push array of powers of 2, again
      % STACK: [0 0 4], [6 3 1], [2 4 8]
E     % Multiply by 2
      % STACK: [0 0 4], [6 3 1], [4 8 16]
*     % Multiply, element-wise
      % STACK: [0 0 4], [24 24 16]
+     % Add, element-wise
      % STACK: [24 24 20]
s     % Sum of array. Implicit display
      % STACK: 68
```
[Answer]
# C, ~~58~~ 56 bytes
*Thanks to @Dennis for saving two bytes!*
```
s,k;f(n){for(s=0,k=2;k<=n;k*=2)s+=n/k*k*2+n%k;return s;}
```
[Try it online!](https://tio.run/##fYxBCsIwEEX3PUUoFGbSiJoiLsa5iRuJRsrgKEldlZ49Ftwa//I/3gubewilZCcUQXGOzwSZd07Yk5xYSSx7zD3rVqxY32snlG7TO6nJtJRRJ/O4jArYzI1Z90rrFaHtrmdtnYngEek3GarkWCX7P7mhHjx8vaV8AA)
# [C (gcc)](https://gcc.gnu.org/), 50 bytes
```
s,k;f(n){for(s=0,k=2;k<=n;)s+=n%k+n/k*(k+=k);k=s;}
```
Returning by `k=s;` is undefined behaviour, but works with gcc when optimizations are disabled. Also, `n%k+n/k*(k+=k)` has [unspecified behaviour](https://en.wikipedia.org/wiki/Unspecified_behavior), but seems to work fine with gcc.
[Try it online!](https://tio.run/##fYxBCsIwEEX3PUUoFGZMRG2RLsa5STcSiYTBUZruSs8eA90a//I/3vPHp/c5JycUQHEN7xkSn51wT3JjJUyWtROrJzmAWBYk4URbjrqY1z0qYLM2puwzlytA2z0mbZ0J0CPSbzJUyVgllz@5oR687t6Wvw)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~9~~ 8 bytes
```
BḊḄÐƤạḤS
```
[Try it online!](https://tio.run/##y0rNyan8/9/p4Y6uhztaDk84tuThroUPdywJ/n90z@H2R01r3P//N9JRMNZRMNdRMASxjIFsU0MjAA)
```
B                        to binary          42 -> 1 0 1 0 1 0
 Ḋ                       drop first                 0 1 0 1 0
  ḄÐƤ                    each suffix to decimal   10 10 2 2 0
      Ḥ                  double the input                  84
     ạ                   absolute difference   74 74 82 82 84
       S                 add them up                      396
```
Vice versa, `B¹ƤṖ+BḄS`: get prefixes, drop last, add them to input, and sum.
[Answer]
# [J](http://jsoftware.com/), ~~20 15~~ 14 bytes
```
+/@}:@:+[\&.#:
```
[Try it online.](https://tio.run/##y/r/P81WT0Fb36HWysFKOzpGTU/Z6n9qcka@QppCallqUaWCkYKxgrmCIZAyNlYwNTT6DwA)
### 15 bytes
```
1#.-,+:-#.\.@#:
```
[Try it online!](https://tio.run/##y/r/P81WT8FQWU9XR9tKV1kvRs9B2ep/anJGvkKaQmpZalGlgpGCsYK5giGQMjZWMDU0@g8A)
```
     +:             Input×2
       -            Subtract
        #.\.@#:     The list of binary suffixes of input (in decimal)
   -,               Append negative input
1#.                 Add them up
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~12~~ 11 bytes
```
¢¬£¢iYTÃÅxÍ
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.5&code=oqyjomlZVMPFeM0=&input=ODg4)
---
## Explanation
```
                 :Implicit input of integer U
¢                :Convert to base-2 string
 ¬               :Split to an array of individual characters/digits
  £    Ã         :Map over the elements, with Y being the current 0-based index
   ¢             :  Convert U to a base-2 string
    iYT          :  Insert a 0 in that string at index Y
        Å        :Slice off the first element of the array
          Í      :Convert each element to a base-10 integer
         x       :Reduce by addition
```
[Answer]
# JavaScript (ES6), ~~41~~ 40 bytes
*Saved 1 byte thanks to Mr.Xcoder*
```
f=(n,k=1)=>k<n&&(n&k)+2*(n&~k)+f(n,k-~k)
```
### Test cases
```
f=(n,k=1)=>k<n&&(n&k)+2*(n&~k)+f(n,k-~k)
console.log(f(2  )) //    4
console.log(f(3  )) //    5
console.log(f(7  )) //   24
console.log(f(12 )) //   68
console.log(f(333)) // 5128
console.log(f(512)) // 9216
```
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), ~~53~~ ~~50~~ 47 bytes
```
.+
*
+`(_+)\1
$1O
O_
_
L$`\B
$`O$'
+%`\B
¶$`¶
_
```
[Try it online!](https://tio.run/##K0otycxLNPyvquGe8F9Pm0uLSztBI15bM8aQS8XQn8s/niuey0clIcaJSyXBX0WdS1sVxD60TSXh0Dau@P//jbiMucy5DIGUsTGXqaERAA "Retina – Try It Online") Link includes test cases. Edit: Saved 3 bytes thanks to @MartinEnder. Explanation:
```
.+
*
+`(_+)\1
$1O
O_
_
```
Convert from decimal to binary, but using O to represent 0, as it's not a digit, and \_ to represent 1, as it's the default repetition character in Retina 1.
```
L$`\B
$`O$'
```
Insert an O between each pair of digits, and collect the results as a list.
```
+%`\B
¶$`¶
```
Convert from binary to unary. (This conversion produces extra `O`s, but we don't care.)
```
_
```
Sum and convert to decimal.
[Answer]
# [Pyth](https://pyth.readthedocs.io), 13 bytes
```
smiXd.BQZ2Ssl
```
**[Try it here!](https://pyth.herokuapp.com/?code=smiXd.BQZ2Ssl&input=333&debug=0)**
### Explanation
```
smiXd.BQZ2Ssl | Full program.
           sl | The base-2 logarithm of the input, floored to an integer.
          S   | Create the integer range [1 ... the floored logarithm].
 m            | And map a function over it.
------------+-+-----------------------------------------------------
  iXd.BQZ2  | The function to be mapped (uses a variable d).
     .BQ    | In the binary representation of the input...
   X    Z   | ... Insert a zero...
    d       | ... At index d.
  i      2  | And convert the result from base 2 to an integer.
------------+-+-----------------------------------------------------
s             | Sum the resulting list.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 bytes
```
BµLḤ_J’×µḄ
```
[Try it online!](https://tio.run/##AR8A4P9qZWxsef//QsK1TOG4pF9K4oCZw5fCteG4hP///zEy "Jelly – Try It Online")
Not the shortest currently, but it could be if there's a way around `Bµ µḄ`...
### Explanation
```
BµLḤ_J’×µḄ    Main link. Argument: n (integer)
B             Binary; convert n to an binary of binary digits. Call this A.
 µ            Start a new monadic link with argument A.
  L           Length; yield len(A). We'll call this l.
   Ḥ          Unhalve; yield l * 2.
     J        Length range; yield [1, 2, ..., l].
    _         Subtract; yield [l*2 - 1, l*2 - 2, ..., l].
      ’       Decrement; subtract one from each item.
       ×      Multiply each item by the corresponding item in A. Call this B.
        µ     Start a new monadic link with argument B.
         Ḅ    Unbinary; convert from a binary array to a decimal.
```
Basically, this works by multiplying each binary digit by a magic number. I can't explain it without visualizing it, so here's the binary number we'll be working with:
```
1111
```
As explained by the challenge, he output we want is the sum of these binary numbers:
```
10111  = 2^4 + 2^2 + 2^1 + 2^0
11011  = 2^4 + 2^3 + 2^1 + 2^0
11101  = 2^4 + 2^3 + 2^2 + 2^0
```
However, we don't actually have to insert zeroes: Jelly's "unbinary" atom will accept numbers other than just `0` and `1`. When we allow ourselves to use `2`, this pattern gets simpler:
```
2111   = 2*2^3 + 1*2^2 + 1*2^1 + 1*2^0
2211   = 2*2^3 + 2*2^2 + 1*2^1 + 1*2^0
2221   = 2*2^3 + 2*2^2 + 2*2^1 + 1*2^0
```
When we sum up the digits in each column, we get
```
6543   = 6*2^3 + 5*2^2 + 4*2^1 + 3*2^0 = 48 + 20 + 8 + 3 = 79.
```
The trick this answer uses is to generate this pattern, and multiply each digit by the corresponding digit in the original to cancel out the necessary columns. 12, for example, would be represented as
```
 1100
×6543
=6500  = 6*2^3 + 5*2^2 + 0*2^1 + 0*2^0 = 48 + 20 + 0 + 0 = 68.
```
[Answer]
# Java 8, 55 bytes
```
n->{int r=0,i=2;for(;i<=n;r+=n%i+n/i*(i*=2));return r;}
```
Port of [Steadybox' C answer](https://codegolf.stackexchange.com/a/154310/52210), and golfed 3 bytes in the process.
[Try it online.](https://tio.run/##hY67DsIwDEV3vsILUsqjvIQYQvgDujAihpCmyKW4VZpWQlW@vaQ8ViLZlnyvda5z2cp5WWnK03uvClnXcJRI3QgAyWqTSaUhGda3AIoNkyLuFefbV22lRQUJEAjoaX7ohhMjljMUa56VhnHcC@JmKmiMU1rghOFErKOIG20bQ2C46/kHVjXXwsO@zLbEFB7@H3ayBul2vsjo88vpWVv9iMvGxpV3bEGMYsUG6D9/E/B3AX8VDNiEIrY/hhu5/gU)
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~13~~ 12 bytes
-1 byte thanks to @Mr. Xcoder!
```
ṁḋ§z·+Θḣotṫḋ
```
[Try it online!](https://tio.run/##yygtzv7//@HOxoc7ug8trzq0XfvcjIc7FueXPNy5Gij0//9/Y2NjAA "Husk – Try It Online")
### Explanation
```
ṁḋ§z·+Θḣ(tṫ)ḋ  -- example input: 6
            ḋ  -- convert to binary: [1,1,0]
  §            -- fork argument
        (tṫ)   -- | tail of tails: [[1,0],[0]]
       ḣ       -- | heads: [[1],[1,1],[1,1,0]]
   z           -- and zipWith the following (example with [1,0] [1])
    · Θ        -- | prepend 0 to second argument: [0,1]
     +         -- | concatenate: [1,0,0,1]
               -- : [[1,0,1,0],[1,1,0,0]]
ṁ              -- map the following (example with [1,0,1,0]) and sum
 ḋ             -- | convert from binary: 10
               -- : 22
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 14 bytes
```
.²LoDŠ‰ø`r·*+O
```
[Try it online!](https://tio.run/##MzBNTDJM/f9f79Amn3yXowseNWw4vCOh6NB2LW3///8NjQA "05AB1E – Try It Online")
[Answer]
# [Pip](https://github.com/dloscutoff/pip), ~~21~~ 18 bytes
```
2*a-a%2**_MS1,#TBa
```
[Try it online!](https://tio.run/##K8gs@F/930grUTdR1UhLK9432FBHOcQp8X@tgi9XNJeCEZeCMZeCOZeCIYhlDGSbAlmx///r5gEA "Pip – Try It Online")
### Explanation
Call our input number `a`. For each binary index `i` at which we want to insert a zero, we can calculate the bits left of the insertion point as `a // 2**i` (where `//` is integer division and `**` is exponentiation), the bits right of the insertion point as `a % 2**i`, and therefore the diluted integer as `2 * (a // 2**i) * 2**i + (a % 2**i)`. But `(a // 2**i) * 2**i` is equal to `a - (a % 2**i)`, and so we can rearrange to a shorter formula: `2 * (a - a % 2**i) + a % 2**i` = `2 * a - a % 2**i`.
```
2*a-a%2**_MS1,#TBa
                       a is 1st command-line argument (implicit)
               TBa     Convert a to binary
              #        Length of the binary expansion
            1,         Range from 1 up to (but not including) that number
          MS           Map this function to the range and sum the results:
2*a-a%2**_              The above formula, where _ is the argument of the function
                       The final result is autoprinted
```
[Answer]
# [R](https://www.r-project.org/), ~~141~~ 48 bytes
```
function(n,l=2^(1:log2(n)))sum(n%%l+(n%/%l*2*l))
```
[Try it online!](https://tio.run/##DcQxCoAwDADAva9wKSS1ok23ol8RRIgIMRWrg6@v3nBX5WbsKj@63ntWUC8TzRCS5I1AEbE8B6i10v73Vhw5QawMgdAwxBjRlOU85QVKYfCM9QM "R – Try It Online")
~~Either I'm doing something really wrong or R is just terrible at bit manipulation.~~ Porting [Luis Mendo's approach](https://codegolf.stackexchange.com/a/154311/67312) is easy, correct, and golfy.
But if you really just want to muck around with bit operations, [MickyT](https://codegolf.stackexchange.com/users/31347) suggested the following 105 byter:
```
function(i)sum(sapply(1:max(which(b<-intToBits(i)>0)),function(x)packBits(head(append(b,F,x),-1),"i")))-i
```
[Try it online!](https://tio.run/##PY1BCoMwEEX3PUVxNQMTMIZuRLrooifwAjEaMrTG0EQaT59qKV19Pv89/qvYcyeKXb1JvHhgjOsMUYfw3EC2s87wdmwcDJ1gn/rlxinu1LVGpL@VMWjz@E5u0iPs@uRHGOhOGUlIpIorRBRcLMgGTxaUUkdcjva7a1pZk8XyAQ "R – Try It Online")
[Answer]
# Python 3, ~~92~~ ~~80~~ 78 bytes
```
def b(x):x=f'{x:b}';return sum(int(x[:i]+'0'+x[i:],2)for i in range(1,len(x)))
```
[Try It Online](https://tio.run/##K6gsycjPM/7/PyU1TSFJo0LTqsI2Tb26wiqpVt26KLWktChPobg0VyMzr0SjItoqM1Zb3UBduyI60ypWx0gzLb9IIVMhM0@hKDEvPVXDUCcnNQ9ohqbm/4IikI4kDSNNTS4Y2xiJbY7ENkRRZIyszBQk9x8A)
Thanks to [Mr.XCoder](https://codegolf.stackexchange.com/users/59487/mr-xcoder) and [ovs](https://codegolf.stackexchange.com/users/64121/ovs) for -12 bytes and -2 bytes respectively.
[Answer]
## Batch, ~~92~~ 77 bytes
```
@set/an=2,t=0
:l
@if %1 geq %n% set/at+=%1*2-(%1%%n),n*=2&goto l
@echo %t%
```
Edit: Switched to the same formula everyone else is using.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 14 bytes
```
BLḊṬœṗ¥€Bj€0ḄS
```
[Try it online!](https://tio.run/##y0rNyan8/9/J5@GOroc71xyd/HDn9ENLHzWtccoCEgYPd7QE////39TQCAA "Jelly – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/), 36 + 1 (`-p`) = 37 bytes
```
$\+=$_*2-($_&$.-1)while($.*=2)<=$_}{
```
[Try it online!](https://tio.run/##K0gtyjH9/18lRttWJV7LSFdDJV5NRU/XULM8IzMnVUNFT8vWSNMGKFdb/f@/sbHxv/yCksz8vOL/ugUA "Perl 5 – Try It Online")
[Answer]
# [Attache](https://github.com/ConorOBrien-Foxx/attache), 57 bytes
```
Sum##UnBin=>{Join[Join=>_,"0"]}=>SplitAt#1&`:@{#_-1}##Bin
```
[Try it online!](https://tio.run/##SywpSUzOSP3vkpqWmZcarZKm8z@4NFdZOTTPKTPP1q7aKz8zLxpE2NrF6ygZKMXW2toFF@RkljiWKBuqJVg5VCvH6xrWKisDlf@P5QooyswrUbC1UwBpUVNS0LVTUAJxw5TTQFS0kY6CsY6CuY6CIYhlDGSbGhrF/gcA "Attache – Try It Online")
I thought I'd approach the problem from a non-bit manipulation approach, as such an approach is impractical in Attache. I have to investigate some of the parts of this approach for alternatives.
## Explanation
Here is an expanded version:
```
Define[$joinByZero, {Join[Join=>_,"0"]}]
Define[$insertionPoints,
    SplitAt#1&`:@{#_-1}
]
Define[$f,
Sum##UnBin=>joinByZero=>insertionPoints##Bin
]
```
This simply takes the binary representation of the number, splits it at certain points, inserts zeroes there, converts back to decimal, and sums them together.
[Answer]
# [J](http://jsoftware.com/), 33 bytes
```
1#.[:}:#.@(<\;@(,0;])"0<@}.\.)@#:
```
Most probably there is much room for further golfing.
## How?
`@#:` convert to binary and
`<@}.\.` - find all suffices, drop the first digit from each and box
`<\` - find all prefixes and box them
`(,0;])"0` - to each prefix append 0 then append the corresponding beheaded suffix
`;@` raze (unbox)
`1#.[:}:#.@` - convert to decimal, curtail and sum
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/DZX1oq1qrZT1HDRsYqwdNHQMrGM1lQxsHGr1YvQ0HZSt/mtyKXClJmfkK6QppJalFlUqGCkYK5grGAIpY2MFU0Oj/wA "J – Try It Online")
]