Binary Gap LeetCode solution in C++
1class Solution {2public:3int binaryGap(int n) {4int res = 0;5int s = 0, i = 0;6while (n) {7if (n & 1) {8s = i;9break;10}11i++;12n = n >> 1;13}14while (n) {15if (n & 1) {16res = max(res, (i - s));17s = i;18}19i++;20n = n >> 1;21}22return res;23}24};