binary: initial commit
This commit is contained in:
parent
da6af4a9b8
commit
e20c1ea2f2
2 changed files with 42 additions and 0 deletions
31
cpp/binary/binary.cpp
Normal file
31
cpp/binary/binary.cpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#include "binary.h"
|
||||
|
||||
namespace binary {
|
||||
|
||||
unsigned convert(const std::string &input)
|
||||
{
|
||||
unsigned result = 0;
|
||||
unsigned factor = 1;
|
||||
|
||||
for (auto i = input.crbegin(); i != input.crend(); ++i) {
|
||||
|
||||
switch (*i) {
|
||||
|
||||
case '0':
|
||||
break;
|
||||
|
||||
case '1':
|
||||
result += factor;
|
||||
break;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
factor <<= 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue