C left shift on 64 bits fail

后端 未结 2 1841
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 13:46

I have this code in C (it\'s for study only):

    char x;
    uint64_t total = 0;

    for(x = 20; x < 30; x++){
        total = (((((1 << x) * x) /         


        
2条回答
  •  萌比男神i
    2020-11-30 14:43

    I guess your problem is, you calculate with 32bit and assign it later to a 64 bit value

    division by 64 is the same as not shift 6 bit

    char x;
    uint64_t one = 1;
    uint64_t total = 0;
    
    for(x = 20; x < 30; x++){
        total = ((((one << (x - 6)) * x) + 1) * sizeof(uint64_t));
        printf("%d - %llu\n", x, total);        
    }
    

    not compiled yet

提交回复
热议问题