Skip to content

Instantly share code, notes, and snippets.

@kbenzie
Created September 27, 2013 13:41
Show Gist options
  • Save kbenzie/6728753 to your computer and use it in GitHub Desktop.
Save kbenzie/6728753 to your computer and use it in GitHub Desktop.
#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