classSolution{public:intbinaryGap(intn){intans=0;// D := distance between any two 1's// Initialized to a reasonable small valuefor(intd=-32;n;n/=2,++d)if(n&1){ans=max(ans,d);d=0;}returnans;}};
classSolution{publicintbinaryGap(intn){intans=0;// D := distance between any two 1's// Initialized to a reasonable small valuefor(intd=-32;n>0;n/=2,++d)if((n&1)==1){ans=Math.max(ans,d);d=0;}returnans;}}
classSolution:defbinaryGap(self,n:int)->int:ans=0d=-32# Distance between any two 1's, initialized to a reasonable small valuewhilen:ifn&1:ans=max(ans,d)d=0n//=2d+=1returnans