Exemple #1
0
constexpr inline intmax_t integer(const char *s) {
  return (
    *s == '+' ? +decimal_(s + 1) :
    *s == '-' ? -decimal_(s + 1) :
    decimal_(s)
  );
}
Exemple #2
0
constexpr inline intmax_t numeral(const char *s) {
  return (
    *s == '0' && *(s + 1) == 'X' ? hex_(s + 2) :
    *s == '0' && *(s + 1) == 'x' ? hex_(s + 2) :
    *s == '0' && *(s + 1) == 'B' ? binary_(s + 2) :
    *s == '0' && *(s + 1) == 'b' ? binary_(s + 2) :
    *s == '0' ? octal_(s + 1) :
    *s == '+' ? +decimal_(s + 1) :
    *s == '-' ? -decimal_(s + 1) :
    decimal_(s)
  );
}
Exemple #3
0
Integer& Integer::operator-= (Integer b)
{
  bool carry = 0;
  std::vector<bool> result;
  std::vector<bool> vec_a = bits_;
  std::vector<bool> vec_b = b.bits_;
  
  //If b is larger than a, swap, so that this function only returns absolute value of result
  if (decimal_() < b.decimal_()) {
    std::swap(vec_a, vec_b);
  }
  
  //Equalize size of vectors by adding leading zeroes
  equalize_(vec_a, vec_b);
  
  for (int i = static_cast<int>(vec_b.size()) - 1; i >= 0; --i) {
    //Compare digits:
    //0 - 0 = 0 (!carry) -
    //0 - 0 = 1 (carry) -
    //0 - 1 = 1 (!carry)
    //0 - 1 = 0 (carry)
    //1 - 0 = 1 (!carry)
    //1 - 0 = 0 (carry)
    //1 - 1 = 0 (!carry) -
    //1 - 1 = 1 (carry) -
    result.insert(result.begin(), (((vec_a[i] == vec_b[i]) == carry) || ((!vec_a[i] && vec_b[i]) && !carry) || ((vec_a[i] && !vec_b[i]) && !carry)));
    
    //Carry if digits equal AND carry from last digit, or carry if 0 - 1 and no carry
    carry = (((vec_a[i] == vec_b[i]) && carry) || (!vec_a[i] && vec_b[i]));
  }
  if (carry) {
    result.insert(result.begin(), 1);
  }
  bits_ = result;
  return *this;
}
Exemple #4
0
constexpr inline uintmax_t decimal(const char *s) {
  return (
    decimal_(s)
  );
}
Exemple #5
0
constexpr inline uintmax_t decimal_(const char *s, uintmax_t sum = 0) {
  return (
    *s >= '0' && *s <= '9' ? decimal_(s + 1, (sum * 10) + *s - '0') :
    sum
  );
}
Exemple #6
0
std::string Integer::decimal_string()
{
  return std::to_string(decimal_());
}