简体   繁体   English

uint64_t类型的问题

[英]Issue with uint64_t type

say I have the the following union : 说我有以下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 : 我想在保留的所有64位中将1位移位,这是我尝试过的:

   ...........................
    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 . 输出结果很好地告诉了15th ,这不是printf参数的问题,该值肯定是错误的= 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. 顺便说一句,我正在使用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). 另一个问题除了格式化所指出的Sourav,是值1int ,这是不可能的, int是64位(其实,在我看来,关于你的平台int只有16位)。

You need to use eg 1ULL to get an unsigned long long that can be shifted 64 bits. 您需要使用例如1ULL来获得可以移位64位的unsigned long long

I believe, you're facing issues with the format specifier used. 我相信,您在使用格式说明符时遇到了问题。 %d expects an int which vektor.u64 is not. %d期望一个不vektor.u64int

To print a uint64_t type safely, you should be using PRIu64 as the format specifier. 为了安全地打印uint64_t类型,您应该使用PRIu64作为格式说明符。 It is available in inttypes.h header file. inttypes.h头文件中可用。

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 . 正如@Joachim Pileborg先生在另一个答案中提到的那样 ,您需要将整数文字 1更改为1ULL ,以确保将其解释为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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