[Question]
  [
*inspired by [Count down from infinity](https://codegolf.stackexchange.com/q/98274/45941)*
Given a non-negative integer `N`, output the number of repetitions of the following steps it takes to reach 0:
1. Convert `N` to binary (`4812390 -> 10010010110111001100110`)
2. Flip each bit (`10010010110111001100110 -> 01101101001000110011001`)
3. Trim leading zeroes (`01101101001000110011001 -> 1101101001000110011001`)
4. Convert back to decimal (`1101101001000110011001 -> 3576217`)
## Rules
* Input and output may be in any unambiguous, consistent format
* The input will be within the native representable integer range for your language (if your language supports arbitrarily-large integers, there is no bound)
## Test Cases
```
0 -> 0
1 -> 1
42 -> 6
97 -> 3
170 -> 8
255 -> 1
682 -> 10
8675309 -> 11
4812390 -> 14
178956970 -> 28
2863311530 -> 32
```
---
This sequence is [A005811](http://oeis.org/A005811) in the OEIS.
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~6~~ 4 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
^HBS
```
[Try it online!](http://jelly.tryitonline.net/#code=XkhCUw&input=&args=Mjg2MzMxMTUzMA) or [verify all test cases](http://jelly.tryitonline.net/#code=XkhCUwrFvMOH4oKsRw&input=&args=MCwgMSwgNDIsIDk3LCAxNzAsIDI1NSwgNjgyLCA4Njc1MzA5LCA0ODEyMzkwLCAxNzg5NTY5NzAsIDI4NjMzMTE1MzA).
### Background
Let **n** be a non-negative integer.
Steps 2 and 3 of the process described in the spec can alternatively be stated as removing all leading **1**'s and toggling the remaining bits. 
This means that we'll remove exactly one group of adjacent and equal binary digits in each iteration, so the Binary Countdown Length of **n** is just the number of these groups in the binary representation of **n**. For the purposes of this challenge, think of **0** as having no digits.
For **n = 8675309**, the process looks as follows in binary.
```
100001000101111111101101
 11110111010000000010010
     1000101111111101101
      111010000000010010
         101111111101101
          10000000010010
           1111111101101
                   10010
                    1101
                      10
                       1
                       0
```
Instead of counting these groups (which would fail for edge case **0**), we do the following.
**n** and **n:2** have the following binary representations.
```
n   = 8675309 = 100001000101111111101101_2
n:2 = 4337654 =  10000100010111111110110_2
```
Note that **n:2**'s binary representation is simply **n**'s, shifted one bit to the left.
If we XOR **n** and **n:2**, we'll obtain a **1** (MSB), and an additional **1** for each pair of different adjacent digits. The number of groups is thus equal to the number of set bits in **n ⊻ n:2**.
### How it works
```
^HBS  Main link. Argument: n
 H    Halve; yield n:2.
^     XOR n with n:2.
  B   Convert the result to binary.
   S  Compute the sum of the resulting binary digits.
```
[Answer]
# Python 2, 30 bytes
```
lambda n:bin(n^n/2).count('1')
```
Test it on [Ideone](http://ideone.com/Mj7cu7).
### Background
Let **n** be a non-negative integer.
Steps 2 and 3 of the process described in the spec can alternatively be stated as removing all leading **1**'s and toggling the remaining bits. 
This means that we'll remove exactly one group of adjacent and equal binary digits in each iteration, so the Binary Countdown Length of **n** is just the number of these groups in the binary representation of **n**. For the purposes of this challenge, think of **0** as having no digits.
For **n = 8675309**, the process looks as follows in binary.
```
100001000101111111101101
 11110111010000000010010
     1000101111111101101
      111010000000010010
         101111111101101
          10000000010010
           1111111101101
                   10010
                    1101
                      10
                       1
                       0
```
Instead of counting these groups (which would fail for edge case **0**), we do the following.
**n** and **n:2** have the following binary representations.
```
n   = 8675309 = 100001000101111111101101_2
n:2 = 4337654 =  10000100010111111110110_2
```
Note that **n:2**'s binary representation is simply **n**'s, shifted one bit to the left.
If we XOR **n** and **n:2**, we'll obtain a **1** (MSB), and an additional **1** for each pair of different adjacent digits. The number of groups is thus equal to the number of set bits in **n ⊻ n:2**.
[Answer]
## Python 2, 29 bytes
```
f=lambda n:n and-n%4/2+f(n/2)
```
Counts the number of alternations between 0 and 1 in the binary expansion, counting the leading 1 as an alternation. Does so by checking whether the last two binary digits are different, then recursing onto the number with the last digit removed. The last two digits are different exactly if `n%4` is 1 or 2, which can be checked as `-n%4/2`.
[Answer]
# Haskell, 34 bytes
```
b 0=0
b n|x<-b$div n 2=x+mod(x+n)2
```
[Answer]
## JavaScript (ES6), 26 bytes
```
f=n=>n&&(n^(n>>=1))%2+f(n)
```
Works by counting the transitions between 0 and 1. Only works up to 31 bits. 29 bytes to support 53 bits:
```
f=n=>1<=n&&(n%2^n/2%2)+f(n/2)
```
[Answer]
# [05AB1E](http://github.com/Adriandmen/05AB1E), ~~7~~ 5 bytes
Saved 2 bytes thanks to *Dennis*.
```
b0ÛÔg
```
Without the edge case of *0* this could be 3 bytes `bÔg`.
[Try it online!](http://05ab1e.tryitonline.net/#code=YjDDm8OUZw&input=Mjg2MzMxMTUzMA) or as a [Test suite](http://05ab1e.tryitonline.net/#code=fHZ5YjDDm8OUZyw&input=MAoxCjQyCjk3CjE3MAoyNTUKNjgyCjg2NzUzMDkKNDgxMjM5MAoxNzg5NTY5NzAKMjg2MzMxMTUzMA)
**Explanation**
```
b      # convert to binary
 0Û    # trim off leading zeroes
   Ô   # remove adjacent duplicates
    g  # length
```
[Answer]
# [CJam](http://sourceforge.net/projects/cjam/), 14 bytes
```
ri0{2b:!2bj)}j
```
[Try it online!](http://cjam.tryitonline.net/#code=cmkwezJiOiEyYmopfWo&input=Mjg2MzMxMTUzMA)
```
ri      e# read integer
0       e# value for terminal case
{       e# recursive function
  2b    e#   create binary representation with no leading zeros
  :!    e#   flip bits
  2b    e#   convert binary back to integer
  j     e#   recursive call
  )     e#   increment from 0 on the way up
}j      e# end
```
Basically a knock off of [my answer](https://codegolf.stackexchange.com/a/98324/46756) to the other question.
[Answer]
# Java 7,~~112 108 100 90~~ 73 bytes
```
int c(int i){int l=1,j=i;for(;(j=j/2)>0;l*=2);return i<1?0:1+c(2*l-1-i);}
```
### 
Basic idea
```
 Lets take an no 10110(21)
 then you do set all bits in no 21 and you will get 11111
 and after that you would subtract the original number from 11111.
 You will get 01001 and loop it until you will get 0
```
[Answer]
# J, 14 bytes
```
**1+/@,2~:/\#:
```
Counts the number of runs in the binary digits of *n* with the special case returning 0 for *n* = 0.
## Usage
```
   f =: **1+/@,2~:/\#:
   (,.f"0) 0 1 42 97 170 255 682 8675309 4812390 178956970 2863311530
         0  0
         1  1
        42  6
        97  3
       170  8
       255  1
       682 10
   8675309 11
   4812390 14
 178956970 28
2863311530 32
```
## Explanation
```
**1+/@,2~:/\#:  Input: integer n
            #:  Get the binary digits of n
       2   \    For each overlapping sublist of size 2
        ~:/       Reduce by not-equals
  1   ,         Prepend a 1
   +/@          Reduce by addition
*               Sign(n), returns 0 for n = 0 else 1
 *              Multiply with the previous sum and return
```
[Answer]
# [CJam](http://sourceforge.net/projects/cjam/), ~~11~~ 10 bytes
*Thanks to @Dennis for saving one byte!*
```
ri_2be`,e&
```
[Try it online!](http://cjam.tryitonline.net/#code=cmlfMmJlYCxlJg&input=Mjg2MzMxMTUzMA)
### Explanation
```
ri            #e Read as integer
              #e STACK: 97
  _           #e Duplicate
              #e STACK: 97, 97
   2b         #e Convert to binary
              #e STACK: 97, [1 1 0 0 0 0 1]
     e`       #e Run-length encoding
              #e STACK: 97, [[2 1] [4 0] [1 1]]
       ,      #e Length
              #e STACK: 97, 3
        e&    #e Return first value if 0, or else the second value
              #e STACK: 3
```
[Answer]
## Racket 349 bytes
```
(define(h s)(apply string(map(λ(x)(if(eq? x #\0)#\1 #\0))(string->list s))))(define(g s)(let*
((l(string-length s))(k(for/list((i s)(n l)#:final(not(equal? i #\0)))n)))(substring s(last k))))
(define(f n)(if(= 0 n)0(begin(let loop((n n)(c 1))(define m(string->number(string-append "#b"
(g(h(number->string n 2))))))(if(> m 0)(loop m(add1 c))c))))
```
Ungolfed: 
```
(define (invertBinary s)
  (apply string
         (map
          (λ(x)(if(eq? x #\0)#\1 #\0))
          (string->list s))))
(define (trimLeading0s s)
  (let* ((l (string-length s))
         (k (for/list ((i s)
                       (n l)
                       #:final (not(equal? i #\0)))
              n)))
    (substring s (last k))))
(define (f n)
  (if (= 0 n) 0
      (begin
        (let loop ((n n)
                   (c 1))
          (define m 
            (string->number
             (string-append
              "#b"
              (trimLeading0s
               (invertBinary
                (number->string n 2))))))
          (if (> m 0)
              (loop m (add1 c))
              c)))))
```
Testing: 
```
(f 0)
(f 1)
(f 42)
(f 97)
(f 170)
(f 255)
(f 682)
(f 8675309)
(f 4812390)
(f 178956970)
(f 2863311530)
```
Output: 
```
0
1
6
3
8
1
10
11
14
28
32
```
[Answer]
# [MATL](http://github.com/lmendo/MATL), 7 bytes
```
BY'nwa*
```
[Try it online!](http://matl.tryitonline.net/#code=QlknbndhKg&input=Mjg2MzMxMTUzMA)
### Explanation
```
          % Implicit input, for example 97
          % STACK: 97
B         % Convert to binary
          % STACK: [1 1 0 0 0 0 1]
 Y'       % Run-length encoding
          % STACK: [1 0 1], [2 4 1]
   n      % Number of elements
          % STACK: [1 0 1], 3
    w     % Swap
          % STACK: 3, [1 0 1]
     a    % Any: gives 1 if any element is nonzero
          % STACK: 3, 1
      *   % Multiply
          % STACK: 3
          % Implicit display
```
[Answer]
# Vim, ~~62~~ 59 bytes
-3 bytes thanks to DJMcMayhem
```
C0
<C-r>=pri<Tab>'%b',<C-r>")
<Esc>0qqC<C-r>=tr(@",'01','10')
<Esc>:s/^0\+
k<C-a>j@qq@q
```
Here is the xxd output with the unprintable characters intact:
```
0000000: 4330 0d12 3d70 7269 0927 2562 272c 1222  C0..=pri.'%b',."
0000010: 290d 1b30 7171 4312 3d74 7228 4022 2c27  )..0qqC.=tr(@",'
0000020: 3031 272c 2731 3027 290d 1b3a 732f 5e30  01','10')..:s/^0
0000030: 5c2b 0d6b 016a 4071 7140 71              \+.k.j@qq@q
```
[Try it online!](http://v.tryitonline.net/#code=QzAKEj1wcmkJJyViJywSIikKGzBxcUMSPXRyKEAiLCcwMScsJzEwJykKGzpzL14wXCsKawFqQHFxQHE&input=Mjg2MzMxMTUzMA)
## Explanation
```
C                                   " Delete the number (it goes in @")
0<CR>                               " Print 0 (our counter) and a carriage return
<C-r>=pri<Tab>'%b',<C-r>")<CR><Esc> " Use `printf()` to insert the number as base 2
0qq                                 " Return to column 0, start recording a macro
  C<C-r>=tr(@",'01','10')<CR><Esc>  "   Replace 0s with 1s and vice versa
  :s/^0\+<CR>                       "   Delete leading 0s
  k<C-a>                            "   Increment the number on the line above
  j                                 "   Return to the previous line
  @q                                "   Invoke macro recursively
q@q                                 " Stop recording and invoke macro
```
[Answer]
# [Burlesque](https://github.com/FMNSSun/Burlesque), 8 bytes
```
rib2gnL[
```
[Try it online!](https://tio.run/##SyotykktLixN/f@/KDPJKD3PJ/r/fwszc1NjA0sA "Burlesque – Try It Online")
```
ri   # Read as integer
b2   # Convert to binary
gn   # Collapse like bits
L[   # Find length
```
# 29 bytes
```
ri0j{j+.j2dg)n!2ug}{^^nz}w!vv
```
[Try it online!](https://tio.run/##SyotykktLixN/f@/KNMgqzpLWy8ryagoMyJCM0/RqDS9tjouLq@qtlyxrOz/fwszc1NjA0sA "Burlesque – Try It Online")
Actually doing the calculation
```
ri       #Read val as int
0j       #Push a 0 (counter) to the bottom of the stack
{
 j+.j    # Increment counter and put back
 2dg     # Get the digits of binary val
 )n!     # Not each digit
 2ug     # Convert back to decimal
}
{^^nz}w! # While not zero
vv       # Drop final zero leaving counter
```
[Answer]
# Ruby, 26 bytes
```
f=->n{n<1?0:-n%4/2+f[n/2]}
```
Inspired by xnor's Python answer.
[Answer]
# [Vyxal 3](https://github.com/Vyxal/Vyxal/tree/version-3) `s`, 3 bytes
```
½⊻b
```
[Try it Online!](https://vyxal.github.io/latest.html#WyJzIiwiIiwiwr3iirtiIiwiIiwiMjg2MzMxMTUzMCIsIjMuNC4xIl0=)
port of the jelly answer
[Answer]
# [Go](https://go.dev), 172 bytes
```
import(."fmt";."strings")
func f(n uint)(i int){for;n>0;i++{Sscanf(Map(func(r rune)rune{if r<'1'{return'1'}else{return'0'}},TrimLeft(Sprintf("%b",n),"0")),"%b",&n)}
return}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=TVHBatwwEKVXfcVUkKxEpotlZ712Nw4U0lsTQta3thTXkRaTrNbIcmkQ_pJelkJu-aHkayKtGuhlnmZ4j3nz9OfvZrd_6pv2rtlI2Dadfhyt-lA8P3Xbfmcsm1O1tXQ1p4M1nd4MlBM16hYU0zB22nLWQQCndmalz5NVd3Li1kPbaMUum54FMjNgRi15KK5TYM5mYuaMtKPR_jXJ-0G-tclsmrA23faLVJate7_UKkaPflLUHGlCua-hO9Z8IlE0Rc8v767sQy-hBu91bK2LDlcgf_fB40TIr8aAlYMdoIKv32tHXILJhMQJFAFOU8wDlkvMDuNlgkV4pItFZORFiuIgKfLlIktKFFFZiDQrExSnUVeUi7z06jTKizzLhPB8zFI_8FYOKYbAGQdHrsOd95rRqqrW9aeb2mOIemfgB1r4WIFptP-haN6Rpj0MFbNzzYnPNAzeV2Dn4VgH12-53UJ1Dr6yo1v-TVP0DI2BjZHLYfJu_l__-eoiLv8X634f8RU)
Implements the algorithm more or less verbatim from the OP.
[Answer]
# PHP, 64 bytes
based on [my countdown solution](http:///codegolf.stackexchange.com/a/98279#98279)
```
for($n=$argv[1];$n;print 1)$n=bindec(strtr(decbin($n),"01",10));
```
prints `1` character `k` times, where `k` is the number of iterations.
---
+4 bytes for integer output: (empty output for `0`)
```
for($n=$argv[1];$n;$i++)$n=bindec(strtr(decbin($n),"01",10));echo$i;
```
[Answer]
# JavaScript (ES6), 44
Recursive function
Limited to javascript positive integer, 31 bits:
```
f=(a,s=0)=>a?f((-1>>>Math.clz32(a))-a,s+1):s
```
Managing double precision number up to 53 significant bits - **59** bytes:
```
F=(a,s=0)=>a?F('0b'+a.toString(2).replace(/./g,1)-a,s+1):s
```
In another way: using the amazing algorithm by @Dennis, non recursive function managing up 53 bits, **43** bytes:
```
a=>a&&a.toString(2).match(/(.)\1*/g).length
```
[Answer]
# PHP, 51 bytes
```
<?=preg_match_all('/(1+|0+)/',decbin($argv[1])?:o);
```
Uses a regex to count the number of runs of 1 or 0. Unfortunately this needs a special case for input of `0` that requires 3 additional bytes (and gives a notice).
[Answer]
# Java 7, 71 bytes
`int b(Long a){return a==0?0:1+b(~a&-1L>>>64-a.toString(a,2).length());}`
I know this is beaten by [Geobits' `split` solution](http://chat.stackexchange.com/transcript/240?m=33270508#33270508) (which will eventually be posted) but this was still fun to write
[Answer]
# Octave, 47 bytes
```
@(x)(sum(dec2bin(bitxor(x,idivide(x,2)))=='1'))
```
According to the OEIS entry, the value we are looking for as the solution to this challenge is *also* equal to the number of `1`s in the Gray code for the given integer.
Wikipedia tells me the Gray code can be calculated as x ^ (x >> 1), so in the above function I calculate the Gray code as such, convert it to a binary string, and count how many digits of that string are `1`.
[Answer]
## Java 7, 64 bytes
```
long g(Long n){return n.toString(n,2).split("0+").length*2-n%2;}
```
I know this could be beaten by a port of one of the better answers, but I came up with it in chat, and I can't *not* post it after [Poke said something about it](https://codegolf.stackexchange.com/a/98444/14215) :)
[Answer]
# Bash, 57 bytes
Packages: Core Utililities, grep, sed, vim (for `xxd`)
Assume the number is given in binary format. Any length is acceptable :)
```
xxd -b -c1|cut -d" " -f2|sed s/^0*//|grep -o .|uniq|wc -l
```
[Answer]
# [Perl 5](https://www.perl.org/), 31 + 1 (`p`) = 32 bytes
```
$_=(sprintf'%b',$_^$_/2)=~y/1//
```
[Try it online!](https://tio.run/##K0gtyjH9/18l3lajuKAoM68kTV01SV1HJT5OJV7fSNO2rlLfUF///39L83/5BSWZ@XnF/3V9TfUMDA3@6xYAAA "Perl 5 – Try It Online")
Using @Dennis's method.
[Answer]
# [Tcl](http://tcl.tk/), 119 bytes
```
proc D n i\ 0 {
while \$n {set n [regsub ^0+(?=.) [string map {0 1 1 0} [format %b $n]] ""]
incr i
scan $n %b n}
set i}
```
[Try it online!](https://tio.run/##FY3dCoIwHMXv/09xCIMiiE1zbhfRTW9hBiZmA52yLbqQPbttnLvzOx@@G7dtsXOHOwz0Awwr/T567PHIDFbX@@jXth/c94UnOx1u1/MRtfNWmwFTu2Bl4FEsoH7Pdmo99i9kpmmw2zWkTWehyXWtiWZCJlBa1WEbUz2eEANx0CUnVRGvGOVlSULmJEVVFkxFJHleqBSrpCqFShkpioLzyCnEieXrHep7Oqaw/QE "Tcl – Try It Online")
**Still very ungolfed for my taste**
[Answer]
# [PHP](https://php.net/), ~~55~~ 52 bytes
```
for($i=$argn;$i;$c++,$i^=(1<<log($i,2)+1)-1);echo$c;
```
[Try it online!](https://tio.run/##K8go@G9jXwAk0/KLNFQybVUSi9LzrFUyrVWStbV1VDLjbDUMbWxy8tOBkjpGmtqGmrqGmtapyRn5KsnW//@bWBgaGVsa/MsvKMnMzyv@r@sGAA "PHP – Try It Online")
All done arithmetically/bitwise (no strings). Am stuck getting it smaller than the [51 byte answer](https://codegolf.stackexchange.com/a/98420/84624) though...
[Answer]
# APL, 13 characters/bytes
```
+/2≠/0,2⊥⍣¯1⊢
```
base 10 to base 2 conversion with all necessary digits
```
2⊥⍣¯1⊢
2⊥⍣¯1⊢4812390
---> 1 0 0 1 0 0 1 0 1 1 0 1 1 1 0 0 1 1 0 0 1 1 0
```
add a leading 0
```
0,
0,1 0 0 1 0 0 1 0 1 1 0 1 1 1 0 0 1 1 0 0 1 1 0
---> 0 1 0 0 1 0 0 1 0 1 1 0 1 1 1 0 0 1 1 0 0 1 1 0
```
pair-wise reduction that gives 1 if consecutive elements are not equal
```
2≠/
2≠/0 1 0 0 1 0 0 1 0 1 1 0 1 1 1 0 0 1 1 0 0 1 1 0
---> 1 1 0 1 1 0 1 1 1 0 1 1 0 0 1 0 1 0 1 0 1 0 1
```
sum
```
+/
+/1 1 0 1 1 0 1 1 1 0 1 1 0 0 1 0 1 0 1 0 1 0 1
---> 14
```
[Answer]
# [PHP](https://php.net/), 50 bytes
```
<?=array_sum(str_split(decbin($argn&-2^$argn*2)));
```
[Try it online!](https://tio.run/##K8go@P/fxt42sagosTK@uDRXo7ikKL64ICezRCMlNTkpM09DJbEoPU9N1ygOzNAy0tTUtP7/38jCzNjY0NDU2OBffkFJZn5e8X9dNwA "PHP – Try It Online")
Unfortunately, really don't think I can get it any more golfed than this!
The idea is to count transitions 1->0 or 0->1.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 76 Bytes, 72 Bytes (thanks to celingcat)
```
unsigned n,m,i;f(x){for(i=0;n=x;x^=m-1,i++)for(m=2;n/=2;m+=m);return i;}
```
[Try it online!](https://tio.run/##bcxBCoMwEEDRfU8hQiFDItVQkTLkJqVQopFZzLSkCgHx7GldN5u/eIvvm9n7nFf50CzTWIlhQxhUgi28oiLXoriE6eG46QxpDQezsyiXX1g7BozTskapCPfMTxIF2@kdSZag6vN4l9oE1QLgH3YlvNqS3obiYCh@bd8fvOcv "C (gcc) – Try It Online")
]