在 C 中打印 64 位值

printing a 64bit value in C

我正在使用 ubuntu x86_64 机器并尝试设置与字符串中字符对应的位位置。

字符可以是 a-z 或 A-Z,所以我保留了一个 64 位向量。

long unsigned int vector = 0x0;
char *getunqchar(char a[]) {
    char str[30];
    int i;
    long unsigned int t, test = 0;
    for (i = 0; i < strlen(a) - 1; i++) {
        t = (long unsigned int)(a[i]) - 65;
        printf("t is %ld", t);
        test = (long unsigned int)(1 << t);
        vector = (vector ) | test;

        printf("vec is %ld %ld \n", (long unsigned int)vector, (long unsigned int)test);
    }
}

int main() {
    int i = 0;
    char name[30], *temp;
    int cnt[52], t;

    memset(cnt, 0, sizeof(cnt));
    printf("vec is %lx", vector);
    printf("Enter the string name: ");
    fgets(name, sizeof(name), stdin);

    temp = getunqchar(name);
}

当我输入如下:

Enter the string name: mAn
t is 44vec is 4096 4096
t is 0vec is 4097 1
t is 45vec is 12289 8192

t 值为 44,我希望输出为 2^44,但我得到的是 2^124432 + 12。由于 64 位,这似乎是一些问题。但我没有得到。感谢任何帮助。

1 << t计算为int,用1UL << t计算为unsigned long.