Created
September 27, 2013 13:41
-
-
Save kbenzie/6728753 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <iostream> | |
#include <cassert> | |
unsigned operator"" _b(const char *str) | |
{ | |
unsigned ret = 0; | |
for(size_t i = 0; i < str[i] != '\0'; ++i) { | |
char digit = str[i]; | |
assert(digit == '0' || digit == '1' && "Binary literal's only support 0 or 1"); | |
ret = ret * 2 + (digit - '0'); | |
} | |
return ret; | |
} | |
int main() { | |
auto binary_literal = 0101_b; | |
std::cout << binary_literal << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment