Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

const uint64_t initialization problem

0 views
Skip to first unread message

Mike - EMAIL IGNORED

unread,
Dec 7, 2006, 12:26:17 PM12/7/06
to help-gp...@gnu.org
On FC4 with g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8):

unsigned long long int mask = 0x003fffffffffffff;
const uint64_t mask = 0x003fffffffffffff;
const uint64_t mask = static_cast<uint64_t>(0x003fffffffffffff);

all result in:

error: integer constant is too large for 'long' type

I workaround with:

const uint64_t mask = (0x003fffff << 32) | 0xffffffff;

Is this as expected? Is it fixed in a later version?

Thanks,
Mike.

Mike - EMAIL IGNORED

unread,
Dec 7, 2006, 1:01:19 PM12/7/06
to help-gp...@gnu.org

Correction. I need:

const uint64_t mask =
((static_cast<uint64_t>(0x003fffff) << 32) | 0xffffffff;

Mike.

Ulrich Eckhardt

unread,
Dec 7, 2006, 1:54:18 PM12/7/06
to
Mike - EMAIL IGNORED wrote:
> On FC4 with g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8):
>
> unsigned long long mask = 0x003fffffffffffff;

> const uint64_t mask = 0x003fffffffffffff;
> const uint64_t mask = static_cast<uint64_t>(0x003fffffffffffff);
>
> all result in:
>
> error: integer constant is too large for 'long' type

Try to explicitly say that it's a 'long long':

unsigned long long const foo = 0x0123456789abcdefull;

Note the 'ull' suffix for an unsigned long long.

There might be a way to construct a 64bit integer in stdint.h, where the
uint64_t also comes from, probably a macro. If you specifically want 64
bits you should use that.

> I workaround with:
>
> const uint64_t mask = (0x003fffff << 32) | 0xffffffff;

^^^^^^^^^^^^^^^^
You noticed it in your second posting, this pushes the bits out at the top.

Also, IIRC the argument for a shift operation must be smaller than the size
in bits, otherwise invoking undefined behaviour. There are compilers that
take this argument modulo the number of bits and those compilers are
standard-conformant! At least I seem to remember such a discussion
somewhere on the Usenet...

> Is this as expected?

Yes.

> Is it fixed in a later version?

Not a bug, nothing to fix.

Uli

--
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/

Mike - EMAIL IGNORED

unread,
Dec 7, 2006, 3:14:01 PM12/7/06
to help-gp...@gnu.org
On Thu, 07 Dec 2006 19:54:18 +0100, Ulrich Eckhardt wrote:
[...]

>
> unsigned long long const foo = 0x0123456789abcdefull;
>
> Note the 'ull' suffix for an unsigned long long.
>
I slightly reorganized the code since my original post,
but, following your suggestion:

MyClass::MyClass()
: mask_(0x003fffffffffffffull),
sign_(0x0080000000000000ull)
{
}

works, where without the 'ull' it failed.
BTW uint64_t is a typedef on my system.

Thanks for your help.
Mike.


Maxim Yegorushkin

unread,
Dec 14, 2006, 3:51:30 AM12/14/06
to

Mike - EMAIL IGNORED wrote:

> On FC4 with g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8):
>
> unsigned long long int mask = 0x003fffffffffffff;
> const uint64_t mask = 0x003fffffffffffff;
> const uint64_t mask = static_cast<uint64_t>(0x003fffffffffffff);
>
> all result in:
>
> error: integer constant is too large for 'long' type
>
> I workaround with:
>
> const uint64_t mask = (0x003fffff << 32) | 0xffffffff;
>
> Is this as expected?

The workaround may not work as expected. The expected result of
(0x003fffff << 32) is int(0).

> Is it fixed in a later version?

You could use a suffix for your long constant as you know already, or
use a portable macro from <stdint.h>

/* Unsigned. */
# define UINT8_C(c) c
# define UINT16_C(c) c
# define UINT32_C(c) c ## U
# if __WORDSIZE == 64
# define UINT64_C(c) c ## UL
# else
# define UINT64_C(c) c ## ULL
# endif

http://www.opengroup.org/onlinepubs/000095399/basedefs/stdint.h.html#tag_13_48_03_04

Maxim Yegorushkin

unread,
Dec 14, 2006, 3:51:59 AM12/14/06
to

Mike - EMAIL IGNORED wrote:

> On FC4 with g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8):
>
> unsigned long long int mask = 0x003fffffffffffff;
> const uint64_t mask = 0x003fffffffffffff;
> const uint64_t mask = static_cast<uint64_t>(0x003fffffffffffff);
>
> all result in:
>
> error: integer constant is too large for 'long' type
>
> I workaround with:
>
> const uint64_t mask = (0x003fffff << 32) | 0xffffffff;
>
> Is this as expected?

The workaround may not work as expected. The expected result of
(0x003fffff << 32) is int(0).

> Is it fixed in a later version?

You could use a suffix for your long constant as you know already, or

0 new messages