Brainfuck, 425425 421 bytes
-4 bytes thanks to @Jiří
,()9;{[YIMm-/?>?;+*:>?;+*(8<<=}]_^~>?;{[YIMm-/?>>?;+*(8<<=?;{[YIMm-/?>>?=-/?>?;+*(8<=}]_^~>>?;{[YIMm-/?>>>>?;+*(8<<=}]}|<<<<=}]_^~>>>?;{[YIMm-=<<<=?;+*:>>>?=}]}|<<>>>>>|>>>?;{[YIMm-=}]}|<<<<<<<=-/?;{[[Z^~>>>>>>?;{[Z^~>>>?=}]_OKk++;{[YIMm-=<<<=}]}|<<<=-=}]_^~>?=}]_^~>>>?;{[ZXx|<=}]_^~>?;{[[Z^~>?;{[Z^~>?=-=<=-=}]_^~>?;{[ZXx|<<<<<<=?;+*:>>>>>>?;{[YIMm-=}]]_^~>?=}]_OKk+;{[ZXx|<=?;{[ZXx|<<<=?;++*:>>>?=-=}]}|<<=}]_^~>>?=}]}|<<<=?/.
Attempt This Online!Attempt This Online!
Thanks to this answer for the XOR algorithm and to this answer for the division by 2.
Expanded code:
,
duplicate the input in two cells
[->+>+<<]
>
divide n by two
[ 0
-
>>+ set marker 2
<< 0
[->>->+<] dec marker inc n/2
>> 2 or 4
[->>>>+<<]
<<<<
]
>>>
[-<<<+>>>]
<<
set temp cell to zero
>>>>>[>>>[-]
<<<<<<<
xor
-[[>>>>>>[>>>]++[-<<<]<<<-]>]>>>[<]>[[>[>-<-]>[<<<<<<+>>>>>>[-]]>]+[<[<<<++>>>-]<<]>>]
<<<.
The code has been made safe with this program. I invite anyone who's better at golfing in Brainkfuck than me to use it to make shorter solutions!