Need 256 bit Integer

Now I'm not the most advanced C++ programmer by a long shot. All I need is an .h file that I can use which will give me a 256 bit integer that I can actually use. Every one I've found either has some sort of error with compiling or won't actually accept a 256 bit integer when I declare it.

Example:
 
uint256_t p = 2^256;

errors out as it claims its "too large"....even though its a freaking 256 bit integer.

Also, ideally, one I can do "cout" with so I can actually send the value to the screen so I can actually see the result of equations.

Tried Bouncy Castle (errors upon compiling with some error with the .h file), and uint256 doesn't seem to even accept 256 bit variables, won't tolerate "cout" and has errors with more than one declaration of how to handle an overload (I don't even really understand overloads).

I'm just looking for something easy and simple to use, I don't care if its slightly slow, just as long as I can use it and when I cout it's value, it shows all the numbers (not 1.9873 x10 ^ 74 or something like that).
http://www.boost.org/doc/libs/1_53_0/libs/multiprecision/doc/html/boost_multiprecision/tut/mixed.html


> errors out as it claims its "too large"
http://www.cplusplus.com/forum/articles/40071/#msg216270
Also, ¿where that data type come from?

> even though its a freaking 256 bit integer.
I'm going to assume that you actually mean 1<<255
'p' is a 256 bit integer
1 is not.
Last edited on
 
uint256_t p = 115792089237316195423570985008687907853269984665640564039457584007908834671663;


Produces the first error, the rest of the errors are from the following:

1
2
3
4
5
inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; }
inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; }
inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; }
inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; }
inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; }


and
1
2
3
4
5
inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; }
inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }


Though I show only a few errors, there are 11 total. 10 of them reference each of the 10 lines of code with the "inline const" I have shown above and are virtually identical (IE: each one shows "^=" or "+=" or "-=" and so on.


Error 1 error C2177: constant too big c:\users\dalydir\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\consoleapplication3.cpp 25 1 ConsoleApplication3
2 IntelliSense: more than one operator "^=" matches these operands:
function template "T operator^=(const T &lhs, const uint256_t &rhs)"
function "base_uint<BITS>::operator^=(const base_uint<BITS> &b) [with BITS=160U]"
operand types are: uint160 ^= const base_uint160 c:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\uint256.h 481 98 ConsoleApplication3
3 IntelliSense: more than one operator "&=" matches these operands:
function template "T operator&=(const T &lhs, const uint256_t &rhs)"
function "base_uint<BITS>::operator&=(const base_uint<BITS> &b) [with BITS=160U]"
operand types are: uint160 &= const base_uint160 c:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\uint256.h 482 98 ConsoleApplication3


P in my original post was 2 to the 256th power. I think that I might be able to make sense of that "boost library" but I'm not counting on it being too soon.

The uint256_t is from:
https://github.com/calccrypto/uint256_t

Perhaps I'm getting my hopes up that there's an easy way to accomplish the whole 256 bit integer thing. If there was a built in 256 bit integer like just "int", I wouldn't even be asking this question.
If you want to use large numbers, you can always use GMP: https://gmplib.org/

You could also use the boost library that ne555 linked to.
1) you cannot fit 2256 in 256 bit integer: it has range [0, 2256 - 1]
2) Integer literals are always of type int. So compiler tries to fit huge number into int type and fails.
Mats: Do either of those work as simple as "int256 = 99328473284230423", as I'm finding they're much more complex than that.

Miiinipaa: Thank you, that makes sense, though the variable I put in my second post is less than 2^256 - 1 and still doesn't work.
though the variable I put in my second post is less than 2^256 - 1 and still doesn't work.
And that brings the second point of my previous post.

To make assigment work correctly, you should use C++11 user defined literals and make your own conversion from character literal function.

Or you can assign your value in several parts:
by math:
1
2
3
4
5
6
7
8
9
uint256_t p = 1157920892373161954ull;
p *= 10000000000000000000ull;
p += 2357098500868790785ull;
p *= 10000000000000000000ull;
p += 3269984665640564039ull;
p *= 10000000000000000000ull;
p += 4575840079088346716ull;
p *= 100;
p += 63;
or by separating your value in 4 64 bits segments and use bit shifting to move them in place.
The uint256_t type from calccrypto seems to work alright ...

1
2
uint256_t p = uint256_t(1) << 255;
std::cout << p << std::endl;

Outputs: 57896044618658097711785492504343953926634992332820282019728792003956564819968

... problem is to handle integer literals of that size. Using user defined literals like MiiNiPaa suggested would probably be the most convenient way but you would have to implement the code for it to work.

Without actually adding anything you could assign the number 115792089237316195423570985008687907853269984665640564039457584007908834671663 by splitting it up. uint256_t has a constructor that takes two arguments, the upper and lower part of the number. uint128_t has a similar constructor so by splitting the number in four parts you can make it work. It's easier if you first convert the number into hexadecimal notation.
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
and then split it up in four parts (each being 8 bytes).
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFEFFFFFC2F
1
2
3
4
uint256_t p(uint128_t(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), 
            uint128_t(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFEFFFFFC2F));

std::cout << p << std::endl;

Outputs: 115792089237316195423570985008687907853269984665640564039457584007908834671663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
// http://www.boost.org/doc/libs/1_55_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/cpp_int.html

int main()
{
    using namespace boost::multiprecision ;
    
    // unsigned integer, 256-bit fixed precision, unchecked
    uint512_t i( "115792089237316195423570985008687907853269984665640564039457584007908834671663" ) ;
    std::cout << i << "\n\n" ;

    // unsigned integer, 512-bit fixed precision, checked
    checked_uint512_t j = checked_uint512_t(1U) << 511 ;
    std::cout << j << "\n\n" ;
    
    try { j <<= 1 ; }
    catch( const std::exception& e ) { std::cerr << "*** error: " << e.what() << "\n\n" ; }
    
    // signed integer, arbitrary precision
    cpp_int k = cpp_int(i) * j ;
    std::cout << k << "\n\n" ;
}

g++-4.8 -std=c++11 -O2 -Wall -Wextra -pedantic-errors main.cpp && ./a.out
115792089237316195423570985008687907853269984665640564039457584007908834671663

6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042048

*** error: Unable to allocate sufficient storage for the value of the result: value overflows the maximum allowable magnitude.

776259046150354467574489744231251277628443008558348305569526019012996683288353652534812258284732137479723543453734734133274561021374731669947764386459621011043446991870133134520497546362912253667817564325212680459456048838763085824

http://coliru.stacked-crooked.com/a/20f95245f862de17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <functional>
#include <algorithm>
#include <uint256.h>
#include <tchar.h>
#include <urlmon.h>
#include <dos.h>
#include <windows.h>
#include <uint256_t.h>
#include <uint128_t.h>


using namespace std;
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "user32.lib")


char b;


int main()
{
	uint256_t p = uint256_t(1) << 255;
std::cout << p << std::endl;
	cin >> b;
	return 0;
	
}


This is my full program, MSVS Express 2012 errors upon compile request, states
Error 1 error C2666: 'operator -' : 5 overloads have similar conversions c:\program files (x86)\microsoft visual studio 11.0\vc\include\uint256.h 219 1 ConsoleApplication3


I literally copy and pasted the stuff from your guys' stuff (except the boost one so far) and the error remains.
That is an awful program, filled with outdated header files. I don't know why you're not using the Boost one when that is clearly going to work. Also, you used GMP and it didn't work?!
Mats, I can't believe you just said that. MSVS Express 2012 is the best c++ compiler out there.
....
....
I say that because its really the only one I've ever used. ....
Guess I'll have to try Boost.

I think I've tried the GMP header but I don't recall. I'm trying to avoid anything that'll make bricks come out of me and that Boost header example that JLBorges showed me made that .h file feel like it would produce such results for me, but I guess I'm going to at least try their compiler.
I'm pretty sure he was referring to the source you pasted, which includes dos.h for no apparent reason, doesn't even bother with the C++ equivalents of C headers, and pragmas in some libraries for good measure.
> that Boost header example ... I guess I'm going to at least try their compiler.

Boost is a collection of C++ libraries.
Download it: http://www.boost.org/users/download/#live

boost::multiprecision::cpp_int is header-only, so just put the boost directory in the include path and compile with your current compiler.
My first few lines of my previous post was meant as sarcasm. The dos.h is there for no apparent reason. I periodically take big vacations from trying programming and so I just have a list of include files that I add so I don't have to go through and try to find the proper .h file every time.

I get what you mean helios...so....then I'm guessing I can't use the .h file I was using and that the suggestion is moreso that I use the full boost library instead of the uint256_t.h file.
boost/multiprecision/cpp_int.hpp produces the following errors (well, these are some of them):

Error 1 error C2039: 'int_least8_t' : is not a member of '`global namespace'' c:\program files (x86)\microsoft visual studio 11.0\vc\include\boost\cstdint.hpp 107 1 ConsoleApplication3
Error 2 error C2873: 'int_least8_t' : symbol cannot be used in a using-declaration c:\program files (x86)\microsoft visual studio 11.0\vc\include\boost\cstdint.hpp 107 1 ConsoleApplication3
Error 3 error C2039: 'int_fast8_t' : is not a member of '`global namespace'' c:\program files (x86)\microsoft visual studio 11.0\vc\include\boost\cstdint.hpp 108 1 ConsoleApplication3
Error 4 error C2873: 'int_fast8_t' : symbol cannot be used in a using-declaration c:\program files (x86)\microsoft visual studio 11.0\vc\include\boost\cstdint.hpp 108 1 ConsoleApplication3
Error 5 error C2039: 'uint_least8_t' : is not a member of '`global namespace'' c:\program files (x86)\microsoft visual studio 11.0\vc\include\boost\cstdint.hpp 110 1 ConsoleApplication3
Error 6 error C2873: 'uint_least8_t' : symbol cannot be used in a using-declaration c:\program files (x86)\microsoft visual studio 11.0\vc\include\boost\cstdint.hpp 110 1 ConsoleApplication3


There's a total of 101 of them 8(
JLBorges.....I think I love that website you linked (with the compiler)....it actually works. When I cut and paste into MVSE it won't work. How do I get the file from their website to be compiled to an exe or a compiler that is basically the one they have?
You can try update to the Visual Studio 2013.

If you want to switch compilers, Coliru uses GCC compiler toolchain, there is several IDE for it avaliable on Windows: Code::Blocks, Orwell Dev-C++, QTCreator (if you want to easily work with QT library). Also you can try Eclipse and Netbeans (requires manual configuration)
Boy do I feel dumb, updated to VS 2013 worked, thanks MiiNipaa
Topic archived. No new replies allowed.