Search code examples
cbitwise-operatorsavrbit-shiftunsigned-integer

Issue with uint64_t type


say I have the the following union :

    typedef union
{
    char array[8];
    uint64_t u64;
} my_type ;

I want to shift one bit 1 through all the 64 bits the reserved, here is what I've tried :

   ...........................
    my_type  vector  ={0};
    short index =0; 
    for ( index=0 ; index <64;index++){
            printf("  u64 :   %d\n", vektor.u64);
            vektor.u64 = (1<<index) ;
        }

the output is fine tell the 15th, and it's not the a problem with the printf parameters, the value is definitly wrong = 0 . here is the output :

     u64 :   0
  u64 :   1
  u64 :   2
  u64 :   4
  u64 :   8
  u64 :   16
  u64 :   32
  u64 :   64
  u64 :   128
  u64 :   256
  u64 :   512
  u64 :   1024
  u64 :   2048
  u64 :   4096
  u64 :   8192
  u64 :   16384
  u64 :   -32768
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0
  u64 :   0

So my question is, what I'm doing wrong ? By the way I'm using ATmelStudio6.2.


Solution

  • Another problem besides the formatting pointed out by Sourav, is that the value 1 is an int, and it's unlikely that int is 64 bits (in fact, it seems to me that on your platform int is only 16 bits).

    You need to use e.g. 1ULL to get an unsigned long long that can be shifted 64 bits.