-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bba108
commit 16352d0
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
int getBinaryGap(int N) | ||
{ | ||
auto max_gap = 0; | ||
bool current_state = false; | ||
bool last_state = false; | ||
auto current_gap_length = 0; | ||
while(N > 0) | ||
{ | ||
current_state = N%2; | ||
if (current_state == last_state) | ||
{ | ||
current_gap_length++; | ||
} | ||
else | ||
{ | ||
if (current_gap_length > max_gap) | ||
{ | ||
max_gap = current_gap_length; | ||
} | ||
current_gap_length = 0; | ||
} | ||
N >>= 1; | ||
} | ||
return max_gap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "src/binary_gap.cpp" | ||
#include <gtest/gtest.h> | ||
#include <thread> | ||
#include <iostream> | ||
|
||
TEST (BinaryGapAllOnes, Test1) | ||
{ | ||
uint8_t b = 0b11111111; | ||
ASSERT_EQ(getBinaryGap(b), 0); | ||
} | ||
|
||
|
||
TEST (BinaryGapAllZeros, Test2) | ||
{ | ||
uint8_t b = 0b00000000; | ||
ASSERT_EQ(getBinaryGap(b), 0); | ||
} | ||
|
||
TEST (BinaryGap1, Test3) | ||
{ | ||
uint8_t b = 0b10001001; | ||
ASSERT_EQ(getBinaryGap(b), 3); | ||
} | ||
|
||
TEST (BinaryGap2, Test4) | ||
{ | ||
uint8_t b = 0b10000001; | ||
ASSERT_EQ(getBinaryGap(b), 6); | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} | ||
|