t_bdCPP.cpp (original) (raw)
`` /* Id:tbdCPP.cId: t_bdCPP.c Id:tbdCPP.c */
/*
- Copyright (C) 2001-26 David Ireland, D.I. Management Services Pty Limited
- https://di-mgt.com.au/contact/ https://di-mgt.com.au/bigdigits.html
- SPDX-License-Identifier: MPL-2.0
- Last updated:
- Date:2026−03−2905:11:00Date: 2026-03-29 05:11:00 Date:2026−03−2905:11:00
- Revision:2.7.0Revision: 2.7.0 Revision:2.7.0
- Author:daiAuthor: dai Author:dai */
/* Using bigdigits in a C++ program.
This file t_bdCPP.cpp and all other C++ modules should be compiled as C++ code (/TC in MSVC),
but supporting files (bigd.c, bigdigits.c) must be compiled as C code.
Do not change the extensions of the core source code files to .cpp.
With MSVC this works if you leave Properties > Configuration Properties > C/C++ > Advanced > Compile As > Default
To compile with gcc use
g++ -std=c++11 -o t_bdCPP t_bdCPP.cpp bigdigits.o bigd.o bigdigitsRand.o bigdRand.o
having compiled bigd.c, bigdigits.c, etc. as C files earlier
gcc -c bigdigits.c bigd.c bigdigitsRand.c bigdRand.c
*/
#include "bigd.h" #include "bigdRand.h"
#include using namespace std; int main() { int ver; ver = bdVersion(); cout << "bdVersion = " << ver << "\n";
BIGD u, v, w;
/* Create new BIGD objects */
u = bdNew();
v = bdNew();
w = bdNew();
/* Compute 2 * 0xdeadbeefface */
bdSetShort(u, 2);
bdConvFromHex(v, "deadbeefface");
bdMultiply(w, u, v);
/* Display the result */
bdPrintHex("", u, " * ");
bdPrintHex("0x", v, " = ");
bdPrintHex("0x", w, "\n");
/* and again in decimal format */
bdPrintDecimal("", u, " * ");
bdPrintDecimal("", v, " = ");
bdPrintDecimal("", w, "\n");
/* Add with digit overflow */
/* ffffffff+ffffffff=00000001 fffffffe */
bdSetShort(u, 0xffffffff);
bdSetShort(v, 0xffffffff);
bdAdd(w, u, v);
bdPrintHex("", u, "+");
bdPrintHex("", v, "=");
bdPrintHex("", w, "\n");
/* Generate some random digits using internal RNG */
cout << "Some random numbers:\n";
bdSetShort(u, bdRandDigit());
bdPrintHex("short: ", u, "\n");
bdRandomBits(u, 512);
bdPrintHex("512 bits: ", u, "\n");
/* Free all objects we made */
bdFree(&u);
bdFree(&v);
bdFree(&w);
return 0;} ``