Skip to content

Instantly share code, notes, and snippets.

@dluciano
Last active September 23, 2022 20:16
Show Gist options
  • Save dluciano/822e6e875a95774844490288283cd91c to your computer and use it in GitHub Desktop.
Save dluciano/822e6e875a95774844490288283cd91c to your computer and use it in GitHub Desktop.
1680. Concatenation of Consecutive Binary Numbers
public class Solution {
const int Mod = 1_000_000_007;
public int ConcatenatedBinary(int n) {
long converted = 0;
var msbPosition = 0;
for(var i = 1; i <= n; ++i){
if((i & (i - 1)) == 0)
msbPosition++;
converted = ((converted << msbPosition) | i) % Mod;
}
return (int)converted;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment