Manish Kumar
Manish Kumar

Reputation: 1479

printing a 64bit value in C

I am using ubuntu x86_64 machine and trying to set the bit position corresponding to the character in string.

Character can a-z or A-Z, so I have kept a 64 bit vector.

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);
}

when I input like below:

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

t value is 44, I expect output as 2^44 but I am getting 2^12. 44 is 32 + 12. It seems to be some issue because of 64 bit. But I am not getting. Any help is appreciated.

Upvotes: 0

Views: 162

Answers (1)

chqrlie
chqrlie

Reputation: 145277

1 << t is evaluated as int, use 1UL << t to evaluate as unsigned long.

Upvotes: 2

Related Questions