C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

using uint64_t with GCC

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Richard Cavell
Guest





PostPosted: Sun Feb 27, 2005 12:13 pm    Post subject: using uint64_t with GCC Reply with quote




uint64_t i64_BitMask = (uint64_t) (1 << 33) - 1 ;
int a = (i64_BitMask >> 32);
printf ("BitMask %xn", a);

This produces ffffffff. I'm pretty sure, watching the stages in
debugging, that 1 << 33 is resolving to zero, since it's being shifted
off the MSB of a 32 bit integer.

I am in general having problems with uint64_t with GCC. In particular,
I can't send one to printf and get a sensible output. The shift
operators appear to only work with 32-bit integers. Also, if I have
intermediate steps, it always seems to want to truncate the output to
32-bits. Putting in casts to uint64_t sometimes fixes it, but not
always. What do I do?

(using XCode, Apple's stdint.h)
Back to top
jacob navia
Guest





PostPosted: Sun Feb 27, 2005 1:22 pm    Post subject: Re: using uint64_t with GCC Reply with quote



Richard Cavell wrote:
Quote:

uint64_t i64_BitMask = (uint64_t) (1 << 33) - 1 ;


The problem is that the expression
(1 << 33)
is calculated (as it should) in integer mode, i.e. 32 bits.

The result (zero) is *THEN* cast to uint64_t.

Fix:

uint64_t i64_BitMask = (uint64_t) ((uint64_t)1 << 33) - 1 ;

jacob

Back to top
Ron Natalie
Guest





PostPosted: Sun Feb 27, 2005 2:55 pm    Post subject: Re: using uint64_t with GCC Reply with quote



jacob navia wrote:
=
Quote:

uint64_t i64_BitMask = (uint64_t) ((uint64_t)1 << 33) - 1 ;

You have an unneeded cast there. The left-most cast serves no
purpose.

Back to top
jacob navia
Guest





PostPosted: Sun Feb 27, 2005 5:05 pm    Post subject: Re: using uint64_t with GCC Reply with quote

Ron Natalie wrote:
Quote:
jacob navia wrote:
=


uint64_t i64_BitMask = (uint64_t) ((uint64_t)1 << 33) - 1 ;


You have an unneeded cast there. The left-most cast serves no
purpose.

True.

uint64_t i64_BitMask = ((uint64_t)1 << 33) - 1 ;

is better.

Also good is

uint64_t i64_BitMask = (1LL << 33) - 1 ;

jacob

Back to top
bachelor
Guest





PostPosted: Sun Feb 27, 2005 5:35 pm    Post subject: Re: using uint64_t with GCC Reply with quote

i don't know why you need shifts, in some cases <bitset> is more
elegant IMHO

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.