// Sumar números enteros sin signo almacenados en cadenas
// Septiembre de 2024 Con Clase, Salvador Pozo
#include <iostream>
#include <cstring>
using namespace std;
const unsigned int cadmax = 8; // 8 bytes equivale a long long int
typedef unsigned char numero[cadmax];
bool Sumar(numero, numero, numero);
long long Mostrar(numero);
int main()
{
numero n1= {0xff, 0xff, 0x63, 0xa7, 0xb3, 0xb6, 0xe0, 0x0d}; // "999999999999999999"
numero n2= {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // "1"
numero suma;
Sumar(n1, n2, suma);
cout << Mostrar(n1) << " + " << Mostrar(n2) << " = " << Mostrar(suma) << endl;
return 0;
}
bool Sumar(numero n1, numero n2, numero r) {
int acarreo = 0;
int c;
for(unsigned int i = 0; i < cadmax; i++) {
c = acarreo+n1[i]+n2[i];
if(c > 0xff) { c-=256; acarreo=true; }
else acarreo=false;
r[i] = c;
}
return !acarreo;
}
long long Mostrar(numero n) {
long long int* x = (long long int*)(n);
return *x;
}