literals for an int128 class

C

cps

I am considering writing an int128 class as a programming exercise.
Before I start (and I have a fair idea of how to do it) I was
wondering what I can do about literals and the assignment operator. I
would like to be able to use integer literals with the assignment and
other operators with literals that are sized at 128 bits.

E.g. I'd like to be able to do:

uint128 myInt128 = 184467440737095516160;


Note that the foregoing literal would produce an error (correctly
so) if I attempted to assign it to an uint64 because it is (2^64) *
10.

Is there any way I can set it up to deal with large literals?

Or am I going to have to use strings and parse them?
 
V

Victor Bazarov

I am considering writing an int128 class as a programming exercise.
Before I start (and I have a fair idea of how to do it) I was
wondering what I can do about literals and the assignment operator.

Judging from the excerpt below, you're talking about the symbol '=' used
for initialization of objects at the time of their definition, and *NOT*
about the assignment operator.
I
would like to be able to use integer literals with the assignment and
other operators with literals that are sized at 128 bits.

E.g. I'd like to be able to do:

uint128 myInt128 = 184467440737095516160;

That is called initialization.
Note that the foregoing literal would produce an error (correctly
so) if I attempted to assign it to an uint64 because it is (2^64) *
10.

Is there any way I can set it up to deal with large literals?

Not portably. Literals are defined by the standard/compiler and unless
your compiler already provides a 128 bit signed or unsigned integer
literal, you can't introduce it. If 'uint128' is totally your own
custom class, then your best bet is to introduce a constructor from a
character string (uint128::uint128(const char*)) and surround those
digits with double quotes:

uint128 myInt128("184467440737095516160");

*Some* compilers can offer built-in integral types of that size and with
those they usually offer a way to specify a literal by means of some
suffix. For instance, MS used to have i64 suffix before 64 bit targets
became common:

int64_t ms64bitInteger = 1234567890i64; // if memory serves
Or am I going to have to use strings and parse them?

Most likely.

T
 
I

Ian Collins

I am considering writing an int128 class as a programming exercise.
Before I start (and I have a fair idea of how to do it) I was
wondering what I can do about literals and the assignment operator. I
would like to be able to use integer literals with the assignment and
other operators with literals that are sized at 128 bits.

E.g. I'd like to be able to do:

uint128 myInt128 = 184467440737095516160;


Note that the foregoing literal would produce an error (correctly
so) if I attempted to assign it to an uint64 because it is (2^64) *
10.

Is there any way I can set it up to deal with large literals?

Or am I going to have to use strings and parse them?

The only way a system could have 128 bit literals would be if it had a
128 bit type!

You'll have to settle for strings.
 
M

Michael Doubez

I am considering writing an int128 class as a programming exercise.
Before I start (and I have a fair idea of how to do it) I was
wondering what I can do about literals and the assignment operator.  I
would like to be able to use integer literals with the assignment and
other operators with literals that are sized at 128 bits.

E.g. I'd like to be able to do:

     uint128 myInt128 = 184467440737095516160;

     Note that the foregoing literal would produce an error (correctly
so) if I attempted to assign it to an uint64 because it is (2^64) *
10.

     Is there any way I can set it up to deal with large literals?

    Or am I going to have to use strings and parse them?

Parsing it from string is however a good idea.

With C++0x, you will be able to write:

uint128 operator "" ulll(const char * string_values, size_t
num_chars)
{
return uint128::parse(string_values,num_chars);
}

uint128 myInt128 = 184467440737095516160ulll;
 
I

Ian Collins

Parsing it from string is however a good idea.

With C++0x, you will be able to write:

uint128 operator "" ulll(const char * string_values, size_t
num_chars)
{
return uint128::parse(string_values,num_chars);
}

uint128 myInt128 = 184467440737095516160ulll;

Neat. In which section of the standard is this described?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top