Page 1 of 1

Trouble with 64 bit integers in gcc

Posted: Sun Oct 25, 2009 9:59 am
by zity
Hello
I'm developing a 64-bit operating system, but I've some trouble handing 64-integers in gcc..
Take this code as an example:

Code: Select all

  uint64_t frames = some_value;
   for(j = 0; j < 64; j++)
   {
      uint64_t bit = (1 << j);
      if(!(frames & bit))
      {
         return j;
      }
   }
When j exceeds 32-bit, the statement in if fails.. The value given to bit is also incorrect for j larger than 32 (this can be fixed by using 1UL, but the statement in if still fails)

Why can't gcc handle 64-bit integers? And more important, can I get it working?

Re: Trouble with 64 bit integers in gcc

Posted: Sun Oct 25, 2009 10:55 am
by Cognition
The problem is with your (1 << j) line. Basically the constant 1 is going to be a 32 bit integer and the operations upon restricted as such. Generally using a suffix of "ul" should force the compiler to recognize that constant as an unsigned 64-bit integer.

Code: Select all

     uint64_t bit = (1ul << j);
Alternatively you can simply break the operation into 2 lines to get the same result.

Code: Select all

     uint64_t bit = 1;
     bit <<= j;

Re: Trouble with 64 bit integers in gcc

Posted: Sun Oct 25, 2009 11:53 pm
by zity
I've got it working now :) I had another function with a (1 << j) line which caused trouble too :) Thanks..