简体   繁体   中英

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.

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 eg 1ULL to get an unsigned long long that can be shifted 64 bits.

I believe, you're facing issues with the format specifier used. %d expects an int which vektor.u64 is not.

To print a uint64_t type safely, you should be using PRIu64 as the format specifier. It is available in inttypes.h header file.

You can change

 printf("  u64 :   %d\n", vektor.u64);

to

 printf("  u64 :"   PRIu64 "\n", vektor.u64);

EDIT:

AS mentioned in the other answer by Mr. @Joachim Pileborg , you need to change the integer literal 1 to 1ULL to make sure that gets interpreted as unsigned long long .

below is a slightly modified version of your code.

It is simplified to remove the clutter.

however, it does show the correct way to perform the desired operation.

#include <stdio.h>

int main( void )
{
    unsigned long long u64Bits;
    short index =0;

    for ( index=0 ; index <64;index++)
    {
            printf("  u64 :   %llu\n", u64Bits );
            u64Bits = ((unsigned long long)1<<index) ;
    }
    return(0);
}

here is the output of the above code:

 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 :   65536
  u64 :   131072
  u64 :   262144
  u64 :   524288
  u64 :   1048576
  u64 :   2097152
  u64 :   4194304
  u64 :   8388608
  u64 :   16777216
  u64 :   33554432
  u64 :   67108864
  u64 :   134217728
  u64 :   268435456
  u64 :   536870912
  u64 :   1073741824
  u64 :   2147483648
  u64 :   4294967296
  u64 :   8589934592
  u64 :   17179869184
  u64 :   34359738368
  u64 :   68719476736
  u64 :   137438953472
  u64 :   274877906944
  u64 :   549755813888
  u64 :   1099511627776
  u64 :   2199023255552
  u64 :   4398046511104
  u64 :   8796093022208
  u64 :   17592186044416
  u64 :   35184372088832
  u64 :   70368744177664
  u64 :   140737488355328
  u64 :   281474976710656
  u64 :   562949953421312
  u64 :   1125899906842624
  u64 :   2251799813685248
  u64 :   4503599627370496
  u64 :   9007199254740992
  u64 :   18014398509481984
  u64 :   36028797018963968
  u64 :   72057594037927936
  u64 :   144115188075855872
  u64 :   288230376151711744
  u64 :   576460752303423488
  u64 :   1152921504606846976
  u64 :   2305843009213693952
  u64 :   4611686018427387904

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM