Binary Countdown Length
Jelly, 6 4 bytes
^HBS
Try it online! or verify all test cases.
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.
Python 2, 30 bytes
lambda n:bin(n^n/2).count('1')
Test it on Ideone.
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.
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
.