using uint64_t with GCC

R

Richard Cavell

uint64_t i64_BitMask = (uint64_t) (1 << 33) - 1 ;
int a = (i64_BitMask >> 32);
printf ("BitMask %x\n", 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)
 
J

jacob navia

Richard said:
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
 
R

Ron Natalie

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.
 
J

jacob navia

Ron said:
jacob navia wrote:
=



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
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top