Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
BinaryGap, C++ - Find longest sequence of zeros in binary representation of an integer.
#include <iostream> #include <cmath> #include <bitset> int binaryGap(int num); int main() { unsigned long long theNumber = 85; std::cout<<"Number: "<<theNumber<<"\n"; int bGap = binaryGap(theNumber); std::cout<<"Gap Higher: "<<bGap; return 0; } int binaryGap(int num) { int gapCurrently = 0; int gapHigher = 0; std::string binary = std::bitset<64>(num).to_string().substr(std::bitset<64>(num).to_string().find("1")); //to binary size_t theZeros = std::count(binary.begin(), binary.end(), '0'); for(int count=0;count<binary.length();count++) { if (binary[count] == '0') { gapCurrently += 1; } else { if (gapCurrently > gapHigher) gapHigher = gapCurrently; gapCurrently = 0; } if (gapCurrently > gapHigher) gapHigher = gapCurrently; } std::cout<<"Binary: "<<binary<<"\n"; std::cout<<"Sequence of zeros: "<<theZeros<<"\n"; return gapHigher; }
run
|
edit
|
history
|
help
0
std::function ambiguity clang
std::reference_wrapper
why
function returning a function demo
std::set custom ordering with composition
6 7
Test titlu
Graphs Iteration2 Directed Graphs
Struct memory ordering
hw 1 os